Xamarin.Android 制作搜索框
2024-10-10 11:04:28
前段时间仿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>
到这里就结束了,其实现方法很简单,大多数都写了注释,如果哪里有问题可随时联系我~
最新文章
- 用Kotlin实现Android定制视图(KAD 06)
- RedisUtil 工具类
- Verilog学习笔记简单功能实现(二)...............全加器
- 高性能网站架构设计之缓存篇(5)- Redis 集群(上)
- DebugView 调试工具
- gridview 字段没有绑定由于column visible= false
- MongoDB ‘conn’Mongo 对象远程代码执行漏洞
- <;ROS>; message_filters 对齐多种传感器数据的时间戳
- 第54节:Java当中的IO流(中)
- java8中的时间处理
- js post下载相当于 location.href
- 昕有灵犀-xyFS私有文件云存储OSS服务
- 04单例模式Singleton
- MFC函数—— CFrameWnd::OnCreateClient
- 第7章 Ajax数据爬取
- EF Code First学习笔记 初识Code First(转)
- Table-Driven Design 表驱动设计
- 【翻译】HOG, Histogram of Oriented Gradients / 方向梯度直方图 介绍
- 【bzoj3122】 Sdoi2013—随机数生成器
- Kafka 简单实验一(安装Kafka)