Xamarin.Android 制作搜索框

  前段时间仿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, 0);       //隐藏键盘
                }
            }
            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>

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

原文地址:https://www.cnblogs.com/swjian/p/10566766.html

时间: 2024-08-06 23:58:59

Xamarin.Android 制作搜索框的相关文章

Xamarin Android 的搜索框 : Search Dialog

Android 的搜索有两种可用方式: Search Dialog它是一个 UI Component , 被激活的时候, 会在顶部显示一个浮动的搜索框. SearchView  可以被布局到任何地方.在 Android 3.0 (Level 11) 中提供. SearchView 简单,随意使用,这里主要说说 Search Dialog  的基本用法, 因为 Xamarin 的处理方式稍稍和 原生 Android 有些不同. 效果: 源码: https://github.com/gruan01/

Android actionbar 搜索框

就是实现在顶部这样的搜索框. 一.这个搜索框是actionbar上的menu上的一个item.叫SearchView.我们可以先在menu选项里定义好: bmap_menu.xml: <?xml version="1.0" encoding="utf-8"?> <menu xmlns:android="http://schemas.android.com/apk/res/android" > <item androi

android浮动搜索框

android浮动搜索框的配置比较繁琐,需要配置好xml文件才能实现onSearchRequest()方法. 1.配置搜索的XML配置文件?,新建文件searchable.xml,保存在res/xml文件夹中. 1 <?xml version="1.0" encoding="UTF-8"?> 2 3 <searchable android:label="@string/search_label" 4 android:search

Android学习笔记_79_ Android 使用 搜索框

1.在资源文件夹下创建xml文件夹,并创建一个searchable.xml: <?xml version="1.0" encoding="utf-8"?> <searchable xmlns:android="http://schemas.android.com/apk/res/android" android:label="@string/sms_search" android:hint="@st

bootstrap制作搜索框及添加回车搜索事件

下面是开发中用bootstrap制作的一个搜索框,以及给搜索框添加回车搜索事件的一个小案例. bootstrap制作搜索框及添加回车搜索事件 下面是功能实现的代码: 1 <!DOCTYPE html> 2 <html> 3 <head> 4 <meta charset="UTF-8"> 5 <title>bootstrap制作搜索框及添加回车搜索事件</title> 6 7 <link rel="s

Android实现搜索框内自动完成文本框

Android实现搜索框内自动完成文本框 xml文件代码如下: 1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout 3 xmlns:android="http://schemas.android.com/apk/res/android" 4 android:orientation="vertical" 5 android:layout_width=

android浮动搜索框的使用

引言 在我们的应用程序中经常需要提供搜索服务,比如搜索联系人, 搜索商品信息等等.我们可以自己在布局中自定义我们的搜索框,实现我们的搜索逻辑.但是还有一种更简单的方法:使用android系统给我们提供的搜索功能框架. 在android中,提供两种实现搜索功能的方式:search dialog 和 searchView. search dialog类似于普通的dialog,悬浮于我们的窗体之上.示例图如下: searchView通常被嵌套在我们的布局之中,最典型的案例就是在actionBar中使用

Android自定义搜索框

搜索框里的虚拟键盘 xml如下 <EditText android:id="@+id/et_shopshow_search" android:layout_width="144dp" android:layout_height="40dp" android:singleLine="true" android:paddingLeft="8dp" android:paddingRight="8d

网页中制作搜索框中遇到一些小问题

当你在制作一个搜索框加按钮的时候,比如<input class="header-text"/><input type="button" class="header-button"/>这样放置,文本框和按钮不管你怎么调样式也不在同一个位置,这个时候解决方式有两种: 1.外边包一层div,让文本框和按钮float:left,此时文本框和按钮都会紧贴div,也就会出现在同一水平线上的效果 2.待搜索