Android開(kāi)發(fā)之軟鍵盤用法實(shí)例分析
本文實(shí)例講述了Android開(kāi)發(fā)中軟鍵盤用法。分享給大家供大家參考。具體如下:
打開(kāi)軟鍵盤,有兩個(gè)方法。一個(gè)是showSoftInput,一個(gè)是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方法在使用之前,必須先讓它的第一個(gè)參數(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兩個(gè)單詞,意思是說(shuō),如果調(diào)用了這個(gè)方法而且確實(shí)是有必要顯示鍵盤的時(shí)候,才會(huì)彈出軟鍵盤。
toggle方法可以隨意的打開(kāi)和關(guān)閉軟鍵盤。
希望本文所述對(duì)大家的Android程序設(shè)計(jì)有所幫助。
- 解析android中隱藏與顯示軟鍵盤及不自動(dòng)彈出鍵盤的實(shí)現(xiàn)方法
- Android中監(jiān)聽(tīng)軟鍵盤顯示狀態(tài)實(shí)現(xiàn)代碼
- Android 顯示和隱藏軟鍵盤的方法(手動(dòng))
- Android軟鍵盤彈出時(shí)的界面控制方法
- Android開(kāi)發(fā)軟鍵盤遮擋登陸按鈕的完美解決方案
- Android自定義View軟鍵盤實(shí)現(xiàn)搜索
- Android軟鍵盤擋住輸入框的終極解決方案
- Android軟鍵盤遮擋的四種完美解決方案
- Android編程之軟鍵盤的隱藏顯示實(shí)例詳解
- Android EditText被軟鍵盤遮蓋的處理方法
- Android App實(shí)現(xiàn)監(jiān)聽(tīng)軟鍵盤按鍵的三種方式
相關(guān)文章
Android LayoutInflater.inflate源碼分析
這篇文章主要介紹了Android LayoutInflater.inflate源碼分析的相關(guān)資料,需要的朋友可以參考下2016-12-12
Android通過(guò)代碼控制ListView上下滾動(dòng)的方法
今天小編就為大家分享一篇關(guān)于Android通過(guò)代碼控制ListView上下滾動(dòng)的方法,小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧2018-12-12
Android 8.0中一些坑以及對(duì)應(yīng)的解決方法
這篇文章主要給大家介紹了關(guān)于Android 8.0中一些坑以及對(duì)應(yīng)的解決方法的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2018-09-09
關(guān)于Kotlin委托你必須重視的幾個(gè)點(diǎn)
委托模式已經(jīng)被證明是實(shí)現(xiàn)繼承的一個(gè)很好的替代方式,下面這篇文章主要給大家介紹了關(guān)于Kotlin委托你必須重視的幾個(gè)點(diǎn),文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-01-01
Android自定義密碼樣式 黑點(diǎn)轉(zhuǎn)換成特殊字符
這篇文章主要為大家詳細(xì)介紹了Android自定義密碼樣式的制作方法,黑點(diǎn)換成¥、%等特殊字符,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-07-07
kotlin實(shí)現(xiàn)五子棋單機(jī)游戲
這篇文章主要為大家詳細(xì)介紹了kotlin實(shí)現(xiàn)五子棋單機(jī)游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-04-04

