InputMethodManager是一个用于控制显示或隐藏输入法面板的类(当然还有其他作用)。
获取InPutMethodManager的方法很简单。
1
|
InputMethodManager
imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
|
分别介绍其使用方法。
1.showSoftInput(Viewview,
int flags)
03
|
import android.app.Activity;
|
04
|
import android.content.Context;
|
05
|
import android.os.Bundle;
|
06
|
import android.os.IBinder;
|
07
|
import android.view.View;
|
08
|
import android.view.ViewGroup;
|
09
|
import android.view.inputmethod.InputMethodManager;
|
10
|
import android.widget.Button;
|
11
|
import android.widget.EditText;
|
13
|
public class TokenActivity extends Activity
{
|
14
|
/**
Called when the activity is first created. */
|
16
|
InputMethodManager
imm = null ;
|
20
|
public void onCreate(Bundle
savedInstanceState) {
|
21
|
super .onCreate(savedInstanceState);
|
22
|
setContentView(R.layout.main);
|
23
|
et
= (EditText)findViewById(R.id.edit);
|
24
|
bt
= (Button)findViewById(R.id.button);
|
26
|
imm
= (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
|
29
|
<span
style= "color:#e53333;" >et.requestFocus();</span>
|
30
|
<span
style= "color:#e53333;" >imm.showSoftInput(et, 0 );</span>
|
这个方法的两个参数,showSoftInput(Viewview,
int flags)。view是要在哪个view的基础上显示输入面板,同时再使用该方法之前,
view需要获得焦点,可以通过requestFocus()方法来设定。
2.hideSoftInputFromWindow(IBinderwindowToken,
int flags)
代码如下:
2
|
ib
= et.getWindowToken();
|
3
|
imm.showSoftInput(bt,
0);
|
4
|
imm.hideSoftInputFromWindow(bt.getWindowToken(),
0);
|
这里隐藏输入框中的两个参数前一个参数也可以写成et.getWindowToken()。
转自http://my.oschina.net/jbcao/blog/61035