国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統(tǒng) > Android > 正文

Android開發(fā)之軟鍵盤用法實例分析

2020-04-11 11:32:21
字體:
供稿:網(wǎng)友

本文實例講述了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。可以看show方法的注釋:

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è)計有所幫助。

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 英德市| 临城县| 宜黄县| 栖霞市| 福建省| 色达县| 青海省| 奉新县| 钟山县| 琼海市| 行唐县| 吉林省| 临洮县| 通河县| 阿鲁科尔沁旗| 邓州市| 年辖:市辖区| 格尔木市| 伊宁县| 遂昌县| 石泉县| 阿合奇县| 洪雅县| 松溪县| 尼木县| 石家庄市| 潮安县| 平果县| 富川| 儋州市| 应城市| 盐源县| 灵山县| 吉水县| 新河县| 肥东县| 揭阳市| 新丰县| 望都县| 香格里拉县| 嘉兴市|