Android Studio控件技巧匯總

設(shè)置/限制EditText只顯示一行

在Layout文件中,設(shè)置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

用代碼實(shí)現(xiàn)單擊按鈕

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

焦點(diǎn)變化事件(尚未測試)

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();
        }
    }
});
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • afinalAfinal是一個android的ioc,orm框架 https://github.com/yangf...
    passiontim閱讀 15,879評論 2 45
  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 179,111評論 25 709
  • 做,才是根本。 行動才是一切的前提。 想了那么多,說了那么多,不去做,并無亂用。 最近,我對此又有了深切的感受。 ...
    虛舟老師閱讀 463評論 2 3
  • “文化是民族的靈魂,教育是民族的生機(jī)。 千教萬教,教人求真;千學(xué)萬學(xué),學(xué)做真人。” 我們要如何“讓”自己做真人? ...
    和睦楚閱讀 369評論 0 0
  • 文|陌上花km 1. 兩周前周末的上午,妹妹打來電話,說她回家了,讓我過去一起吃飯。 說實(shí)話,我是很想回去的,可是...
    陌上花km閱讀 476評論 5 12

友情鏈接更多精彩內(nèi)容