美文网首页
Android Studio控件技巧汇总

Android Studio控件技巧汇总

作者: 汶水一方 | 来源:发表于2017-08-10 10:30 被阅读70次

设置/限制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(); 

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控件技巧汇总

      本文链接:https://www.haomeiwen.com/subject/fcacrxtx.html