键盘不会出现在Android自定义对话框中
键盘不会出现在自定义对话框中。 我尝试了以下各种方法,但没有成功键盘不会出现在Android自定义对话框中,android,Android,键盘不会出现在自定义对话框中。 我尝试了以下各种方法,但没有成功 passwdDialog.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM); passwdDialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ST
passwdDialog.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
passwdDialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
edtTitle.requestFocus();
InputMethodManager imm = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
imm.showSoftInput(edtTitle, 0);
有没有办法在自定义对话框的EditText
中使用键盘
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final PasswdDialog passwdDialog = new PasswdDialog(MainActivity.this);
passwdDialog.setCanceledOnTouchOutside(false);
passwdDialog.show();
Button btnConfirm = passwdDialog.findViewById(R.id.btn_confirm);
Button btnCancel = passwdDialog.findViewById(R.id.btn_cancel);
final EditText edtTitle = passwdDialog.findViewById(R.id.edt_title);
}
}
public class PasswdDialog extends Dialog
{
private Context context;
public PasswdDialog(Context context) {
super(context);
this.context = context;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.dialog_passwd);
getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
EditText edtTitle = findViewById(R.id.edt_title);
}
}
布局有可能是错的吗
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/dialog_warning">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="vertical"
android:paddingLeft="30dp"
android:paddingTop="15dp"
android:paddingRight="30dp"
android:paddingBottom="15dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:text="text1"
android:textColor="#898989"
android:textSize="22sp"
android:textStyle="normal" />
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_marginTop="15dp"
android:background="#dcdcdc" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="15dp"
android:layout_marginBottom="5dp"
android:text="text2"
android:textColor="#747474"
android:textSize="15sp"
android:textStyle="normal" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:background="@drawable/dialog_border">
<EditText
android:id="@+id/edt_title"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="10dp"
android:background="@null"
android:gravity="top"
android:inputType="numberPassword"
android:singleLine="true"
android:textSize="15sp" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="15dp"
android:layout_marginBottom="5dp"
android:orientation="horizontal">
<Button
android:id="@+id/btn_cancel"
style="?android:attr/borderlessButtonStyle"
android:layout_width="70dp"
android:layout_height="40dp"
android:layout_marginRight="15dp"
android:background="@drawable/button_add"
android:text="text3"
android:textColor="#747474"
android:textSize="12sp" />
<Button
android:id="@+id/btn_confirm"
style="?android:attr/borderlessButtonStyle"
android:layout_width="70dp"
android:layout_height="40dp"
android:background="@drawable/button_add"
android:text="text4"
android:textColor="#747474"
android:textSize="12sp" />
</LinearLayout>
</LinearLayout>
</RelativeLayout>
如果您想在打开自定义对话框时显示键盘,如下图所示
InputMethodManager ime = null;
ime = (InputMethodManager)Category_Edit.this.getSystemService(Context.INPUT_METHOD_SERVICE);
ime.showSoftInputFromInputMethod(((EditText)dialog.findViewById(R.id.edt)).getWindowToken(), InputMethodManager.SHOW_FORCED);
dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
尝试在editText xml文件中添加以下代码
android:selectAllOnFocus="true"
更新:其他方式
在对话框中添加lisener怎么样
passwdDialog.setOnShowListener(new DialogInterface.OnShowListener() {
@Override
public void onShow(DialogInterface dialog) {
InputMethodManager imm = (InputMethodManager) mContext.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(mFullPopUpEditBox, 0);
}
});
非常感谢您的回答,但它仍然不起作用。不仅在某些平板电脑上,而且在其他设备上,我认为这是平板电脑的问题。非常感谢你。