Android開發(fā)之軟鍵盤用法實例分析
本文實例講述了Android開發(fā)中軟鍵盤用法。分享給大家供大家參考。具體如下:
打開軟鍵盤,有兩個方法。一個是showSoftInput,一個是toggleSoftInput。
package com.example.dd; import android.app.Activity; import android.content.Context; import android.os.Bundle; import android.view.Menu; import android.view.View; import android.view.View.OnClickListener; import android.view.inputmethod.InputMethodManager; import android.widget.Button; import android.widget.EditText; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); final EditText ed2 = (EditText) findViewById(R.id.editText2); Button b1 = (Button) findViewById(R.id.button1); b1.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { ed2.requestFocus(); show(ed2); } }); Button b2 = (Button) findViewById(R.id.button2); b2.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { toggle(); } }); } private void show(EditText ed2) { InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); imm.showSoftInput(ed2, InputMethodManager.SHOW_IMPLICIT); } private void toggle() { InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); imm.toggleSoftInput(0, 0); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } }
其中show方法在使用之前,必須先讓它的第一個參數(shù)requestFocus??梢钥磗how方法的注釋:
Synonym for showSoftInput(View, int, ResultReceiver) without a result receiver: explicitly request that the current input method's soft input area be shown to the user, if needed.
最后的 if needed兩個單詞,意思是說,如果調(diào)用了這個方法而且確實是有必要顯示鍵盤的時候,才會彈出軟鍵盤。
toggle方法可以隨意的打開和關(guān)閉軟鍵盤。
希望本文所述對大家的Android程序設(shè)計有所幫助。
- 解析android中隱藏與顯示軟鍵盤及不自動彈出鍵盤的實現(xiàn)方法
- Android中監(jiān)聽軟鍵盤顯示狀態(tài)實現(xiàn)代碼
- Android 顯示和隱藏軟鍵盤的方法(手動)
- Android軟鍵盤彈出時的界面控制方法
- Android開發(fā)軟鍵盤遮擋登陸按鈕的完美解決方案
- Android自定義View軟鍵盤實現(xiàn)搜索
- Android軟鍵盤擋住輸入框的終極解決方案
- Android軟鍵盤遮擋的四種完美解決方案
- Android編程之軟鍵盤的隱藏顯示實例詳解
- Android EditText被軟鍵盤遮蓋的處理方法
- Android App實現(xiàn)監(jiān)聽軟鍵盤按鍵的三種方式
相關(guān)文章
Android LayoutInflater.inflate源碼分析
這篇文章主要介紹了Android LayoutInflater.inflate源碼分析的相關(guān)資料,需要的朋友可以參考下2016-12-12Android 8.0中一些坑以及對應(yīng)的解決方法
這篇文章主要給大家介紹了關(guān)于Android 8.0中一些坑以及對應(yīng)的解決方法的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2018-09-09Android自定義密碼樣式 黑點轉(zhuǎn)換成特殊字符
這篇文章主要為大家詳細介紹了Android自定義密碼樣式的制作方法,黑點換成¥、%等特殊字符,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-07-07