前段时间仿QQ做了一个搜索框样式,个人认为还不错,留在这里给大家做个参考,希望能帮助到有需要的人。
首先上截图(图1:项目中的样式,图2:demo样式):
不多说直接上代码:
Main.axml
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, 0); //隐藏键盘 } } Toast.MakeText(this, et_search.Text, ToastLength.Short).Show(); return false; } }}
search_bcak.xml
到这里就结束了,其实现方法很简单,大多数都写了注释,如果哪里有问题可随时联系我~