设置/限制EditText只显示一行
在Layout文件中,设置android:maxLines="1"
和 android:inputType="text"
。
<EditText
android:id="@+id/searchbox"
android:maxLines="1"
android:inputType="text"
>
</EditText>
隐藏虚拟键盘
View view = this.getCurrentFocus();
if (view != null) {
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
}
参考:
https://stackoverflow.com/questions/1109022/close-hide-the-android-soft-keyboard
用代码实现单击按钮
button.performClick()
如果单击有动画的话,每一步之后还需要button.invalidate
button.performClick();
button.setPressed(true);
button.invalidate();
button.setPressed(false);
button.invalidate();
焦点变化事件(尚未测试)
editText.setOnFocusChangeListener(new OnFocusChangeListener() {
@Override
public void onFocusChange(View view, boolean hasFocus) {
if (hasFocus) {
Toast.makeText(getApplicationContext(), "Got the focus", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(getApplicationContext(), "Lost the focus", Toast.LENGTH_LONG).show();
}
}
});
网友评论