android 简书搜索,Android_SearchView

布局文件

xmlns:tools="http://schemas.android.com/tools"

android:layout_width="match_parent"

android:layout_height="match_parent"

>

android:id="@+id/search_view"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:iconifiedByDefault="true"

android:inputType="text"

android:imeOptions="actionSearch"

android:queryHint="搜索" />

重要属性如下:

android:iconifiedByDefault表示搜索图标是否在输入框内。true效果更加

android:imeOptions设置IME options,即输入法的回车键的功能,可以是搜索、下一个、发送、完成等等。这里actionSearch表示搜索

android:inputType输入框文本类型,可控制输入法键盘样式,如numberPassword即为数字密码样式android:queryHint输入框默认文本

代码

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

searchView = (SearchView) findViewById(R.id.search_view);

searchView.setIconified(false);

searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {

public boolean onQueryTextSubmit(String query) {

Toast.makeText(MainActivity.this, "begin search", Toast.LENGTH_SHORT)

.show();

return true;

}

public boolean onQueryTextChange(String newText) {

if (newText != null && newText.length() > 0) {

Log.d("", "text change");

}

return true;

}

});

// show keyboard

getWindow().setSoftInputMode(

WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE

| WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);

}

searchView.onActionViewExpanded();表示在内容为空时不显示取消的x按钮,内容不为空时显示.

searchView.setSubmitButtonEnabled(true);编辑框后显示search按钮,建议用android:imeOptions=”actionSearch”代替。

//隐藏键盘

InputMethodManager inputMethodManager;

inputMethodManager = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);

private void hideSoftInput() {

if (inputMethodManager != null) {

View v = SearchActivity.this.getCurrentFocus();

if (v == null) {

return;

}

inputMethodManager.hideSoftInputFromWindow(v.getWindowToken(),

InputMethodManager.HIDE_NOT_ALWAYS);

searchView.clearFocus();

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值