android 自定义的dialog,edit text 不能获得焦点,弹出软键盘。

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了android 自定义的dialog,edit text 不能获得焦点,弹出软键盘。相关的知识,希望对你有一定的参考价值。

代码如下: View view = LayoutInflater.from(mContext).inflater(R.layout.custom_dialog, null); AlerDialog dialog = new AlerDialog.builder(mContext).create(); dialog.show(); Window window = dialog.getWindow(); window.setContentView(view); custom_dialog.xml 里面包含一个EditText, 可是打开dialog ,没有自己弹出软键盘。 求求大侠们帮帮忙,急用!!!

1、首先自定义布局
2、把自定义布局放入dialog中显示
3、通过自定义布局查找对应的edittext组件
final View DialogView = a .inflate ( R.layout.loand, null);//1、自定义布局
//创建对话框
AlertDialog dlg = new AlertDialog.Builder(loand.this)
.setTitle("登录框")
.setView(DialogView)//设置自定义对话框的样式,2、自定义布局放入dialog中显示
.setPositiveButton("登陆", //设置"确定"按钮
new DialogInterface.OnClickListener() //设置事件监听
public void onClick(DialogInterface dialog, int whichButton)editText1 =(EditText) DialogView.findViewById(R.id.editText1);
editText2 =(EditText) DialogView.findViewById(R.id.editText2);//3、过自定义布局查找对应的edittext组件
String id = editText1.getText().toString();
String password = editText2.getText().toString();
参考技术A 原因1:应该是因为你的dialog没有获取焦点
解决方案:inputDialog.setFocusable(true)
//inputDialog是你的alertdialog

原因2:手机反应延迟,或者是手机输入法的问题
解决方案:换一部手机试试看,或者另外装一个手机输入法

原因3;可能确实是edittext出问题了
解决方案:
Layout layout=new android.widget.LinearLayout(ctx)
layout.setOrientation(1)
EditText Etext=new android.widget.EditText(ctx)
Etext.setHint("提示")
Etext.setText("内容")
layout.addView(Etext)
//将Etext添加到线性布局layout中

//ctx是上下文

原因4:dialog出问题了(没写好)

ScrollView sv=new android.widget.ScrollView(ctx)
AlertDialog inputDialog=new android.app.AlertDialog.Builder(ctx).setView(sv).setTitle(title).setNegativeButton("确定",new android.content.DialogInterface.OnClickListener()
onClick:function(mp)
//执行某些事情

inputDialog.dismiss()
//确认后对话框消失


)
.create()

inputDialog.setCanceledOnTouchOutside(false)
//这个是触碰外面对话框不会消失

inputDialog.show()
//显示对话框,必须要有!不然不会显示

//ctx是上下文
参考技术B ((InputMethodManager) getSystemService(INPUT_METHOD_SERVICE))
.toggleSoftInput(0,
InputMethodManager.HIDE_NOT_ALWAYS);这是让软键盘弹出的代码。如果是对话框的话,需要延迟一些时间在执行,不然出现之后,又消失了。 Timer timer = new Timer();
timer.schedule(new TimerTask() @Override
public void run()
((InputMethodManager) getSystemService(INPUT_METHOD_SERVICE))
.toggleSoftInput(0,
InputMethodManager.HIDE_NOT_ALWAYS);

, 1000);
参考技术C android:state_selected是选中

android:state_focused是获得焦点

android:state_pressed是点击

android:state_enabled是设置是否响应事件,指所有事件
选中 = 获取焦点 + 点击

【Android】自定义全屏dialog

参考技术A 一、在themes.xml中添加自定义dialog的样式

二、创建dialog基类

三、创建自定义dialog的布局

四、创建自定义dialog

五、在activity中使用自定义dialog

以上是关于android 自定义的dialog,edit text 不能获得焦点,弹出软键盘。的主要内容,如果未能解决你的问题,请参考以下文章

如何自定义Android Dialog的样式?

android自定义的dialog怎么设置view

自定义dialog可动态更新吗

android怎样自定义dialog

优化自定义Dialog的弹出速度

android Dialog怎样自定义属性?