前段时间仿QQ做了一个搜索框样式,个人认为还不错,留在这里给大家做个参考,希望能帮助到有需要的人。

首先上截图(图1:项目中的样式,图2:demo样式):

不多说直接上代码:

Main.axml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#000000">
<LinearLayout
android:layout_width="match_parent"
android:layout_weight="1"
android:layout_height="match_parent"
android:orientation="horizontal">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="25dp"
android:focusable="true"
android:focusableInTouchMode="true"
android:layout_marginTop="10dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:background="@drawable/search_back">
<ImageView
android:layout_width="23dp"
android:layout_marginTop="2dp"
android:layout_height="23dp"
android:layout_marginLeft="5dp"
android:padding="2dp"
android:layout_marginBottom="2dp"
android:src="@drawable/icon_query" />
<EditText
android:id="@+id/et_search"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="30dp"
android:padding="2dp"
android:background="@null"
android:hint="搜索"
android:layout_gravity="center_vertical"
android:singleLine="true"
android:textSize="17sp"
android:textColor="#000000" />
</RelativeLayout>
</LinearLayout>
</LinearLayout>

MainActivity.cs

using Android.App;
using Android.Widget;
using Android.OS;
using Android.Views;
using Android.Runtime;
using System;
using Android.Views.InputMethods; namespace SearchDemo
{
[Activity(Label = "SearchDemo", MainLauncher = true)]
public class MainActivity : Activity, View.IOnKeyListener
{
EditText et_search; protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState); // Set our view from the "main" layout resource
SetContentView(Resource.Layout.Main); et_search = (EditText)FindViewById(Resource.Id.et_search);
et_search.ImeOptions = Android.Views.InputMethods.ImeAction.Search; //修改键盘按钮
et_search.SetOnKeyListener(this);
} public bool OnKey(View v, [GeneratedEnum] Keycode keyCode, KeyEvent e)
{
if (keyCode == Keycode.Enter || keyCode == Keycode.Search)
{
InputMethodManager imm = (InputMethodManager)this.GetSystemService(InputMethodService);
if (imm != null)
{
imm.HideSoftInputFromWindow(Window.DecorView.WindowToken, ); //隐藏键盘
}
}
Toast.MakeText(this, et_search.Text, ToastLength.Short).Show();
return false;
}
}
}

search_bcak.xml

<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#F5F5F5" />
<corners
android:topLeftRadius="4dp"
android:topRightRadius="4dp"
android:bottomRightRadius="4dp"
android:bottomLeftRadius="4dp" />
<!--<stroke
android:width="1dp"
/>-->
</shape>

到这里就结束了,其实现方法很简单,大多数都写了注释,如果哪里有问题可随时联系我~

最新文章

  1. 用Kotlin实现Android定制视图(KAD 06)
  2. RedisUtil 工具类
  3. Verilog学习笔记简单功能实现(二)...............全加器
  4. 高性能网站架构设计之缓存篇(5)- Redis 集群(上)
  5. DebugView 调试工具
  6. gridview 字段没有绑定由于column visible= false
  7. MongoDB ‘conn’Mongo 对象远程代码执行漏洞
  8. &lt;ROS&gt; message_filters 对齐多种传感器数据的时间戳
  9. 第54节:Java当中的IO流(中)
  10. java8中的时间处理
  11. js post下载相当于 location.href
  12. 昕有灵犀-xyFS私有文件云存储OSS服务
  13. 04单例模式Singleton
  14. MFC函数—— CFrameWnd::OnCreateClient
  15. 第7章 Ajax数据爬取
  16. EF Code First学习笔记 初识Code First(转)
  17. Table-Driven Design 表驱动设计
  18. 【翻译】HOG, Histogram of Oriented Gradients / 方向梯度直方图 介绍
  19. 【bzoj3122】 Sdoi2013—随机数生成器
  20. Kafka 简单实验一(安装Kafka)

热门文章

  1. python 打包成 windows .EXE
  2. HttpWebRequest 自定义header,Post发送请求,请求形式是json,坑爹的代码
  3. AtCoder Regular Contest 090
  4. python 模块——os 模块
  5. $(&quot;&quot;).append无反应
  6. 洛谷P1443 马的遍历
  7. .Net QQ互联教程
  8. 12-Python操作json
  9. spring 单元测试方法及其错误整理
  10. VS Code 调试 Angular 和 TypeScript 的配置