Android Studio控件技巧汇总

设置/限制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();
        }
    }
});
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • afinalAfinal是一个android的ioc,orm框架 https://github.com/yangf...
    passiontim阅读 15,877评论 2 45
  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 179,062评论 25 709
  • 做,才是根本。 行动才是一切的前提。 想了那么多,说了那么多,不去做,并无乱用。 最近,我对此又有了深切的感受。 ...
    虚舟老师阅读 463评论 2 3
  • “文化是民族的灵魂,教育是民族的生机。 千教万教,教人求真;千学万学,学做真人。” 我们要如何“让”自己做真人? ...
    和睦楚阅读 369评论 0 0
  • 文|陌上花km 1. 两周前周末的上午,妹妹打来电话,说她回家了,让我过去一起吃饭。 说实话,我是很想回去的,可是...
    陌上花km阅读 475评论 5 12

友情链接更多精彩内容