<RelativeLayout
android:id="@+id/dialogRelativeLayout"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<!-- android:imeOptions="actionGo" sets the keyboard to have a "go" key instead of a "new line" key. -->
<!-- android:inputType="textUri" disables spell check in the EditText and changes the "go" key from a check mark to an arrow. -->
<EditText
android:id="@+id/editText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:layout_marginLeft="4dp"
android:layout_marginRight="4dp"
android:layout_marginBottom="16dp"
android:imeOptions="actionGo"
android:inputType="textUri"/>
</RelativeLayout>
AlertDialog.java:
import android.app.Activity;
import android.app.Dialog;
import android.content.DialogInterface;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.DialogFragment;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatDialogFragment;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.EditText;
public class CreateDialog extends AppCompatDialogFragment {
// The public interface is used to send information back to the activity that called CreateDialog.
public interface CreateDialogListener {
void onCreateDialogCancel(DialogFragment dialog);
void onCreateDialogOK(DialogFragment dialog);
}
CreateDialogListener mListener;
// Check to make sure that the activity that called CreateDialog implements both listeners.
public void onAttach(Activity activity) {
super.onAttach(activity);
try {
mListener = (CreateDialogListener) activity;
} catch (ClassCastException e) {
throw new ClassCastException(activity.toString() + " must implement CreateDialogListener.");
}
}
// onCreateDialog requires @NonNull.
@Override
@NonNull
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(getActivity());
LayoutInflater customDialogInflater = getActivity().getLayoutInflater();
// Setup dialogBuilder.
alertDialogBuilder.setTitle(R.string.title);
alertDialogBuilder.setView(customDialogInflater.inflate(R.layout.alert_dialog, null));
alertDialogBuilder.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
mListener.onCreateDialogCancel(CreateDialog.this);
}
});
alertDialogBuilder.setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
mListener.onCreateDialogOK(CreateDialog.this);
}
});
// Assign the resulting built dialog to an AlertDialog.
final AlertDialog alertDialog = alertDialogBuilder.create();
// Show the keyboard when the dialog is displayed on the screen.
alertDialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
// We need to show alertDialog before we can setOnKeyListener below.
alertDialog.show();
EditText editText = (EditText) alertDialog.findViewById(R.id.editText);
// Allow the "enter" key on the keyboard to execute "OK".
editText.setOnKeyListener(new View.OnKeyListener() {
public boolean onKey(View v, int keyCode, KeyEvent event) {
// If the event is a key-down event on the "enter" button, select the PositiveButton "OK".
if ((event.getAction() == KeyEvent.ACTION_DOWN) && (keyCode == KeyEvent.KEYCODE_ENTER)) {
// Trigger the create listener.
mListener.onCreateDialogOK(CreateDialog.this);
// Manually dismiss alertDialog.
alertDialog.dismiss();
// Consume the event.
return true;
} else {
// If any other key was pressed, do not consume the event.
return false;
}
}
});
// onCreateDialog requires the return of an AlertDialog.
return alertDialog;
}
}
public void showKeyboard() {
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
View v = getCurrentFocus();
if (v != null)
imm.showSoftInput(v, 0);
}
Hide keyboard
public void hideKeyboard() {
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
View v = getCurrentFocus();
if (v != null)
imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
}
final AlertDialog.Builder alert = new AlertDialog.Builder(getActivity());
final EditText input = new EditText(getActivity());
alert.setView(input);
// do what you need, like setting positive and negative buttons...
final AlertDialog dialog = alert.show();
input.setOnFocusChangeListener(new OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
if(hasFocus) {
dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
}
}
});
(...)
// Create the dialog and show it
Dialog dialog = builder.create()
dialog.show();
// After show (this is important specially if you have a list, a pager or other view that uses a adapter), clear the flags and set the soft input mode
dialog.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE|WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
@Override
public void onResume() {
super.onResume();
//passwordInput.requestFocus(); <-- that doesn't work
passwordInput.postDelayed(new ShowKeyboard(), 325); //250 sometimes doesn't run if returning from LockScreen
}
ShowKeyboard 在哪里
private class ShowKeyboard implements Runnable {
@Override
public void run() {
passwordInput.setFocusableInTouchMode(true);
//passwordInput.requestFocusFromTouch(); //this gives touch event to launcher in background -_-
passwordInput.requestFocus();
getActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
((InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE)).showSoftInput(passwordInput, 0);
}
}
fun Activity.hideKeyBoard() {
val view = this.currentFocus
val methodManager = this.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
assert(view != null)
methodManager.hideSoftInputFromWindow(view!!.windowToken, InputMethodManager.HIDE_NOT_ALWAYS)
}
fun Activity.showKeyboard() {
val view = this.currentFocus
val methodManager = this.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
assert(view != null)
methodManager.showSoftInput(view, InputMethodManager.SHOW_IMPLICIT)
}
24 回答
您可以在
AlertDialog
上的EditText
上创建焦点侦听器,然后获取AlertDialog
的Window
. 从那里你可以通过调用setSoftInputMode
来制作软键盘 .用于显示键盘用途:
隐藏键盘使用:
您可以在创建对话框后立即请求软键盘(在SDK上测试 - r20)
我有同样的问题,并用以下代码解决了它 . 我不确定它在具有硬件键盘的手机上的表现如何 .
我找到了这个例子http://android-codes-examples.blogspot.com/2011/11/show-or-hide-soft-keyboard-on-opening.html . 在
alert.show()
之前添加以下代码 .要么
来自其他答案的代码片段可以正常工作,但在代码中放置它们并不总是很明显,特别是如果您使用的是
AlertDialog.Builder
并且遵循official dialog tutorial,因为它不使用final AlertDialog ...
或alertDialog.show()
.比较好
因为如果焦点从EditText切换,SOFT_INPUT_STATE_ALWAYS_VISIBLE将隐藏键盘,其中SHOW_FORCED将保持键盘显示直到它被明确解除,即使用户返回主屏幕或显示最近的应用程序 .
下面是使用自定义布局创建的AlertDialog的工作代码,其中EditText是用XML定义的 . 它还将键盘设置为具有“go”键并允许其触发正按钮 .
alert_dialog.xml:
AlertDialog.java:
嗯,这是一个相当古老的帖子,还有一些东西需要补充 .
这些是两种简单的方法,可以帮助我控制键盘,它们的工作非常完美:
Show keyboard
Hide keyboard
让我指出一些额外的信息到yuku的解决方案,因为我发现很难让这个工作!如何从AlertDialog.Builder获取AlertDialog对象?嗯,这是我
alert.show()
执行的结果:看一下处理手动隐藏和显示IME的this讨论 . 但是,我的感觉是,如果一个专注的
EditText
没有带来IME,那是因为你在你的_1151609中调用了AlertDialog.show()
或者在实际呈现屏幕之前引发的其他一些方法 . 把它移到OnPostResume()
应该修复它,我相信 .是的,你可以用
setOnFocusChangeListener
它会帮助你 .如果有人得到:
尝试将 context 添加到getSystemService调用 .
所以
最初的问题涉及Dialogs和我的EditText是常规视图 . 无论如何,我怀疑这对大多数人来说也应该有效 . 所以这对我有用(上面提出的评价最高的方法对我没有任何帮助) . 这是一个自定义的EditView来执行此操作(子类化不是必需的,但我发现它很方便我的目的,因为我想在视图变得可见时 grab 焦点) .
这实际上与tidbecks答案大致相同 . 实际上我没有注意到他的回答,因为它的票数为零 . 然后我就要评论他的帖子了,但这太长了,所以无论如何我都结束了这个帖子 . tidbeck指出,他不确定如何使用键盘设备 . 我可以确认在任何一种情况下行为似乎完全相同 . 这样,在纵向模式下,软件键盘会弹出,而在横向上则不会 . 物理键盘滑出或没有在我的手机上没有任何区别 .
因为,我个人发现我选择使用的行为有点尴尬:
InputMethodManager.SHOW_FORCED
. 这是我希望它工作的工作原理 . 无论方向如何,键盘都会变得可见,但是,如果硬件键盘滑出,至少在我的设备上它不会弹出 .问题似乎是,因为您输入文本的地方是隐藏最初(或嵌套或某事),AlertDialog自动设置标志
WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM
或WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
,以便事物不会触发显示的软输入 .解决此问题的方法是添加以下内容:
尝试并使用:
为了显示键盘,对我来说,我必须做以下事情
Android TextField : set focus + soft input programmatically
基本上解决方案如下
ShowKeyboard
在哪里输入成功后,我也确保隐藏键盘
我创建了很好的kotlin-esqe扩展功能,任何人都有兴趣
这是一个很好的样本:
为什么这个答案 - 因为上面的解决方案将显示你的键盘,但如果你点击
EditText
之外的任何地方它也不会消失 . 因此,当_1151629失去焦点时,你需要做一些事情来使keybaord消失 .您可以通过执行以下步骤来实现此目的:
这有点棘手 . 我这样做了,它起作用了 .
1.首先调用以隐藏窗口中的软输入 . 如果软键盘可见,这将隐藏软输入,如果不可见则不执行任何操作 .
2.显示你的对话
3.然后简单地调用切换软输入 .
码:
试试这个
如果(显示)
inputMethodManager.toggleSoftInput(InputMethodManager.SHOW_FORCED,0);
其他
inputMethodManager.toggleSoftInput(InputMethodManager.HIDE_IMPLICIT_ONLY,0);
}
将这些方法放在Util类中并在任何地方使用 .
科特林
Java
正如horkavlna所写,
切换
并隐藏键盘
方法工作 . 但展示变体在我的情况下不起作用 . 所以在
onCreate()
我把hideKeyboard(editText);
然后放在onStart()
我写toggleKeyboard(editText);
并在onStop()
我写hideKeyboard(editText);
.有三个问题:
1)当应用程序以关闭屏幕启动时,它将隐藏键盘,
2)每次打开屏幕,它都会显示键盘,
3)应用完成后,您可以在Android主屏幕中看到键盘 .
经过多次测试后,我删除了这些方法并在
AndroidManifest
中activity
标签写了android:windowSoftInputMode="stateVisible"
或android:windowSoftInputMode="stateAlwaysHidden"
.当我进入Activity时,我在onCreate()中调用它来自动显示键盘 .