B T*_*B T 17 keyboard android autocompletetextview android-softkeyboard onbackpressed
使用时AutoCompleteTextView
,会出现下拉建议列表,软件键盘仍然可见.这是有道理的,因为输入随后的字符以缩小列表通常会更有效.
但是,如果用户想要浏览建议列表,则软件键盘仍处于运转状态时变得非常繁琐(当设备处于横向时,这更是一个问题).如果没有键盘占用屏幕空间,导航列表会容易得多.不幸的是,当您按下后退键时,默认行为会首先删除列表(即使在后退键的软件版本中,它显示的图像显示"按此键将隐藏键盘").
这是一个准系统示例,演示了我在说什么:
public class Main2 extends Activity {
private static final String[] items = {
"One",
"Two",
"Three",
"Four",
"Five"
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
AutoCompleteTextView actv = new AutoCompleteTextView(this);
actv.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
actv.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, items));
actv.setThreshold(1);
LinearLayout ll = new LinearLayout(this);
ll.setOrientation(LinearLayout.VERTICAL);
ll.addView(actv);
setContentView(ll);
}
}
Run Code Online (Sandbox Code Playgroud)
除了这是不直观的事实(后面的键提示暗示背压将被发送到键盘),它使得导航AutoCompleteTextView
建议非常无聊.
什么是最不具侵入性的方式(例如,onBackPressed()
在每个活动中捕获后面并相应地路由它肯定不是理想的)使第一次按下后隐藏键盘,第二次删除建议列表?
Mos*_*zar 31
您可以通过在自定义AutoCompleteTextView中覆盖onKeyPreIme来实现这一点.
public class CustomAutoCompleteTextView extends AutoCompleteTextView {
public CustomAutoCompleteTextView(Context context) {
super(context);
}
public CustomAutoCompleteTextView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public CustomAutoCompleteTextView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
public boolean onKeyPreIme(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK && isPopupShowing()) {
InputMethodManager inputManager = (InputMethodManager) getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
if(inputManager.hideSoftInputFromWindow(findFocus().getWindowToken(),
InputMethodManager.HIDE_NOT_ALWAYS)){
return true;
}
}
return super.onKeyPreIme(keyCode, event);
}
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
5893 次 |
最近记录: |