手机
当前位置:查字典教程网 >编程开发 >安卓软件开发 >Android开发之软键盘用法实例分析
Android开发之软键盘用法实例分析
摘要:本文实例讲述了Android开发中软键盘用法。分享给大家供大家参考。具体如下:打开软键盘,有两个方法。一个是showSoftInput,一个...

本文实例讲述了Android开发中软键盘用法。分享给大家供大家参考。具体如下:

打开软键盘,有两个方法。一个是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方法在使用之前,必须先让它的第一个参数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两个单词,意思是说,如果调用了这个方法而且确实是有必要显示键盘的时候,才会弹出软键盘。

toggle方法可以随意的打开和关闭软键盘。

希望本文所述对大家的Android程序设计有所帮助。

【Android开发之软键盘用法实例分析】相关文章:

android开发之方形圆角listview代码分享

android之自定义Toast使用方法

Android内存调试命令

android实现字体闪烁动画的方法

Mac 下搭建Android开发环境

Android 倒影算法的实现代码

Android中库项目的使用方法图文介绍

android平台的左右上下都能滚动的效果

Android开发笔记之:Splash的实现详解

android 弹出提示框的使用(图文实例)

精品推荐
分类导航