android编程软键盘,十分钟彻底搞定Android开发中软键盘的常见问题(2)

在上篇文章中,我们知道了软键盘软键盘其实是一个Dialog.InputMethodService为我们的输入法创建了一个Dialog和如何调整软键盘的显示。接下来,我们将会了解到更多关于软键盘的知识。

自动弹出软键盘

在我们修改信息或者搜索,修改密码等界面的时候,用户进入这个界面的主要目的就是输入修改/查找 某些信息,为了用户体验应该自动弹出软键盘而不是让用户主动点击输入框才弹出。

private void showKeyboard(){

InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);

inputMethodManager.showSoftInput(editText, 0);

}

对于界面比较复杂的情况的时候,软键盘可能无法正常的弹出,需要延迟加载。即在界面加载完成之后,弹出软键盘。

使用定时器schedule:

Timer timer = new Timer();

timer.schedule(new TimerTask()   {

public void run() {

InputMethodManager inputManager = (InputMethodManager)editText.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);

inputManager.showSoftInput(editText, 0);

}

}, 998);

使用handler:

可参考图1-1

d6c6e68f755aed84c4f22e47f2063fcb.png

可参考图1-2

6914f640d44875faae5ff9c302ca3fc9.png

不自动弹出软键盘

带有EditText控件的在第一次显示的时候会自动获得focus,并弹出键盘,如果不想自动弹出键盘,有两种方法:

方法一:在mainfest文件中把对应的activity设置

android:windowSoftInputMode="stateHidden" 或者android:windowSoftInputMode="stateUnchanged"。

方法二:可以在布局中放一个隐藏的TextView,然后在onCreate的时候requsetFocus。

注意TextView不要设置Visiable=gone,否则会失效,可以在布局中放一个隐藏的TextView,然后在onCreate的时候requsetFocus。

android:id="@+id/text_notuse"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:focusable="true"

android:focusableInTouchMode="true"

/>

TextView textView = (TextView)findViewById(R.id.text_notuse);

textView.requestFocus();

EditText设置ScrollView压缩背景图片解决办法

在你的Activity里加上

getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);

动态关闭软键盘

方法一:

InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);

imm.hideSoftInputFromWindow(YourView.getWindowToken(), 0);

方法二:

InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);

// 得到InputMethodManager的实例

if (imm.isActive()) {

// 如果开启

imm.toggleSoftInput(InputMethodManager.SHOW_IMPLICIT,

InputMethodManager.HIDE_NOT_ALWAYS);

// 关闭软键盘,开启方法相同,这个方法是切换开启与关闭状态的

}

软键盘界面按钮功能设置方法

在android发开发程中,有时候需要对EditText的软键盘进行监听。

当点击软键盘回车位置按键的时候,需要实现完成、前进、下一项、搜索、发送或其他功能,这就需要开发者对软键盘回车的点击事件进行捕捉。

比如在登录界面,需要用户在输入密码之后点击软键盘回车直接登录,不必再去点击屏幕上的登录按钮。我们就可以在密码使用的EditText设置 android:imeOptions=”actionDone”,然后对EditText设置OnEditorActionListener监听,当捕捉到用户点击完成时,调用登录方法即可。(IME英文全称Input Method Editors,中文名称输入法编辑器)

先看一个demo,布局文件如下:

xmlns:tools="http://schemas.android.com/tools"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:paddingLeft="@dimen/activity_horizontal_margin"

android:paddingRight="@dimen/activity_horizontal_margin"

android:paddingTop="@dimen/activity_vertical_margin"

android:paddingBottom="@dimen/activity_vertical_margin"

android:orientation="vertical"

tools:context=".MainActivity">

android:layout_width="fill_parent"

android:layout_height="fill_parent">

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:orientation="vertical">

android:id="@+id/actionDoneEditText"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:layout_marginTop="20dp"

android:inputType="text"

android:imeOptions="actionDone"

android:hint="actionDone" />

android:id="@+id/actionGoEditText"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:layout_marginTop="20dp"

android:inputType="text"

android:imeOptions="actionGo"

android:hint="actionGo" />

android:id="@+id/actionNextEditText"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:layout_marginTop="20dp"

android:inputType="text"

android:imeOptions="actionNext"

android:hint="actionNext" />

android:id="@+id/actionNoneEditText"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:layout_marginTop="20dp"

android:inputType="text"

android:imeOptions="actionNone"

android:hint="actionNone" />

android:id="@+id/actionPreviousEditText"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:layout_marginTop="20dp"

android:inputType="text"

android:imeOptions="actionPrevious"

android:hint="actionPrevious" />

android:id="@+id/actionSearchEditText"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:layout_marginTop="20dp"

android:inputType="text"

android:imeOptions="actionSearch"

android:hint="actionSearch" />

android:id="@+id/actionUnspecifiedEditText"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:layout_marginTop="20dp"

android:inputType="text"

android:imeOptions="actionUnspecified"

android:hint="actionUnspecified" />

android:id="@+id/actionSendEditText"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:layout_marginTop="20dp"

android:inputType="text"

android:imeOptions="actionSend"

android:hint="actionSend" />

注意一:

使用android:imeOptions属性的时候,一定要对EditText设置 android:inputType 或者 设置 android:singleline=”true”。

在activity_main.xml文件中,定义了8个EditText,imeOptions分别是:

actionDone 完成 对应 EditorInfo.IME_ACTION_DONE

actionGo 前进 对应 EditorInfo.IME_ACTION_GO

actionNext 下一项 对应 EditorInfo.IME_ACTION_NEXT

actionNone 无动作 对应 EditorInfo.IME_ACTION_NONE

actionPrevious 上一项 对应 EditorInfo.IME_ACTION_PREVIOUS

actionSearch 搜索 对应 EditorInfo.IME_ACTION_SEARCH

actionUnspecified 未指定 对应 EditorInfo.IME_ACTION_UNSPECIFIED

actionSend 发送 对应 EditorInfo.IME_ACTION_SEND

在MainActivity中:

public class MainActivity extends Activity implements TextView.OnEditorActionListener {

private EditText mActionDoneEditText;

private EditText mActionGoEditText;

private EditText mActionNextEditText;

private EditText mActionNoneEditText;

private EditText mActionPreviousEditText;

private EditText mActionSearchEditText;

private EditText mActionSendEditText;

private EditText mActionUnspecifiedEditText;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

initView();

}

private void initView() {

mActionDoneEditText = (EditText) findViewById(R.id.actionDoneEditText);

mActionGoEditText = (EditText) findViewById(R.id.actionGoEditText);

mActionNextEditText = (EditText) findViewById(R.id.actionNextEditText);

mActionNoneEditText = (EditText) findViewById(R.id.actionNoneEditText);

mActionPreviousEditText = (EditText) findViewById(R.id.actionPreviousEditText);

mActionSearchEditText = (EditText) findViewById(R.id.actionSearchEditText);

mActionSendEditText = (EditText) findViewById(R.id.actionSendEditText);

mActionUnspecifiedEditText = (EditText) findViewById(R.id.actionUnspecifiedEditText);

mActionDoneEditText.setOnEditorActionListener(this);

mActionGoEditText.setOnEditorActionListener(this);

mActionNextEditText.setOnEditorActionListener(this);

mActionNoneEditText.setOnEditorActionListener(this);

mActionPreviousEditText.setOnEditorActionListener(this);

mActionSearchEditText.setOnEditorActionListener(this);

mActionSendEditText.setOnEditorActionListener(this);

mActionUnspecifiedEditText.setOnEditorActionListener(this);

}

@Override

public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {

doWhichOperation(actionId);

Log.e("BALLACK", "event: " + event);

Log.e("BALLACK", "v.getImeActionId(): " + v.getImeActionId());

Log.e("BALLACK", "v.getImeOptions(): " + v.getImeOptions());

Log.e("BALLACK", "----------------------------------------------");

return true;

}

private void doWhichOperation(int actionId) {

switch (actionId) {

case EditorInfo.IME_ACTION_DONE:

Log.e("BALLACK", "IME_ACTION_DONE");

break;

case EditorInfo.IME_ACTION_GO:

Log.e("BALLACK", "IME_ACTION_GO");

break;

case EditorInfo.IME_ACTION_NEXT:

Log.e("BALLACK", "IME_ACTION_NEXT");

break;

case EditorInfo.IME_ACTION_NONE:

Log.e("BALLACK", "IME_ACTION_NONE");

break;

case EditorInfo.IME_ACTION_PREVIOUS:

Log.e("BALLACK", "IME_ACTION_PREVIOUS");

break;

case EditorInfo.IME_ACTION_SEARCH:

Log.e("BALLACK", "IME_ACTION_SEARCH");

break;

case EditorInfo.IME_ACTION_SEND:

Log.e("BALLACK", "IME_ACTION_SEND");

break;

case EditorInfo.IME_ACTION_UNSPECIFIED:

Log.e("BALLACK", "IME_ACTION_UNSPECIFIED");

break;

default:

break;

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Android的Dialog关闭软键盘需要以下步骤: 1. 首先,获取Dialog的Window对象。可以通过`dialog.getWindow()`方法来实现。 2. 接下来,调用Window的`setSoftInputMode()`方法,将其参数设置为`WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN`。这将会使得软键盘始终不会自动弹出。 3. 最后,调用Window的`setContentView()`方法加载布局文件或者视图对象。 代码示例: ```java Dialog dialog = new Dialog(context); Window window = dialog.getWindow(); window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN); window.setContentView(R.layout.dialog_layout); ``` 在Android的Dialog控制软键盘的显示与隐藏,可以通过以下方式实现: 1. 如果Dialog有EditText控件需要获取焦点时,可以使用`InputMethodManager`类来显示软键盘。可以在控件获得焦点时,调用`InputMethodManager`的`showSoftInput()`方法来显示软键盘。 代码示例: ```java EditText editText = dialog.findViewById(R.id.edit_text); editText.requestFocus(); InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); imm.showSoftInput(editText, InputMethodManager.SHOW_IMPLICIT); ``` 2. 如果需要在特定情况下隐藏软键盘,可以使用`InputMethodManager`类的`hideSoftInputFromWindow()`方法。 例如,当失去焦点或者点击Dialog外部区域时,可以调用`hideSoftInputFromWindow()`方法来隐藏软键盘。 代码示例: ```java View rootView = dialog.findViewById(R.id.root_view); rootView.setOnTouchListener(new View.OnTouchListener() { @Override public boolean onTouch(View view, MotionEvent motionEvent) { if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) { InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); imm.hideSoftInputFromWindow(view.getWindowToken(), 0); } return false; } }); ``` 通过以上步骤,可以在Android的Dialog关闭软键盘以及控制软键盘的显示与隐藏。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值