Android Studio控件技巧汇总

设置/限制EditText只显示一行

在Layout文件中,设置android:maxLines="1"android:inputType="text"



隐藏虚拟键盘

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(); 

https://stackoverflow.com/questions/5701666/can-i-click-a-button-programmatically-for-a-predefined-intent

焦点变化事件(尚未测试)

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();
        }
    }
});

你可能感兴趣的:(Android Studio控件技巧汇总)