近期我独立开发的项目《全医会》已经在内測其中了。非常快将会上架到各大应用市场。之前开发的几个项目都由于一些原因没有上架还是比較遗憾的。所以,近期我心情格外的好。

今天在做一个新项目,专为律师和客户开发的APP。其中有一个自己定义对话框的需求。这个知识点事实上非常easy,就是下图这个效果:



但是当我悠闲的写完以后才发现。自己定义对话框里面嵌套的EditText根本无法获取焦点。无法弹出软键盘,郁闷,曾经开发的软件里面没有EditText的时候一切正常。没有发现这个隐藏的坑。下图是我之前写的一个自己定义对话框:


以下来解决问题:

1、对话框的布局文件:

<?

xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center" > <LinearLayout
android:layout_width="match_parent"
android:layout_height="260dp"
android:orientation="horizontal" > <LinearLayout
android:layout_width="match_parent"
android:layout_height="240dp"
android:layout_marginLeft="30dp"
android:layout_marginRight="30dp"
android:layout_marginTop="20dp"
android:background="@drawable/round_white_corner"
android:gravity="center_horizontal"
android:orientation="vertical"
android:paddingLeft="15dp"
android:paddingRight="15dp" > <EditText
android:id="@+id/et_obj"
style="@style/DarkTextViewTheme"
android:layout_width="match_parent"
android:layout_height="35dp"
android:layout_marginTop="30dp"
android:background="@drawable/round_textview_corner"
android:gravity="center_vertical"
android:hint="输入标的"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:textSize="14sp" /> <EditText
android:id="@+id/et_name"
style="@style/DarkTextViewTheme"
android:layout_width="match_parent"
android:layout_height="35dp"
android:layout_marginTop="12dp"
android:background="@drawable/round_textview_corner"
android:gravity="center_vertical"
android:hint="联系人姓名"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:textSize="14sp" /> <EditText
android:id="@+id/et_phone"
style="@style/DarkTextViewTheme"
android:layout_width="match_parent"
android:layout_height="35dp"
android:layout_marginTop="12dp"
android:background="@drawable/round_textview_corner"
android:gravity="center_vertical"
android:hint="联系人电话"
android:inputType="phone"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:textSize="14sp" /> <TextView
android:id="@+id/tv_go"
android:layout_width="110dp"
android:layout_height="35dp"
android:layout_marginBottom="20dp"
android:layout_marginTop="25dp"
android:background="@drawable/round_blue_corner"
android:gravity="center"
android:text="确认托付"
android:textColor="@color/white"
android:textSize="16sp" >
</TextView>
</LinearLayout> <ImageView
android:id="@+id/iv_close"
android:layout_width="34dp"
android:layout_height="34dp"
android:layout_marginLeft="-50dp"
android:layout_marginTop="5dp"
android:src="@drawable/law_dialog_close" />
</LinearLayout> </LinearLayout>

2、自己定义dialog的代码


// 弹出自己定义dialog
LayoutInflater inflater = LayoutInflater.from(OnlineActivity.this);
View view = inflater.inflate(R.layout.hl_law_dialog, null); // 对话框
final Dialog dialog = new Dialog(OnlineActivity.this);
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(0));
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.show(); // 设置宽度为屏幕的宽度
WindowManager windowManager = getWindowManager();
Display display = windowManager.getDefaultDisplay();
WindowManager.LayoutParams lp = dialog.getWindow().getAttributes();
lp.width = (int) (display.getWidth()); // 设置宽度
dialog.getWindow().setAttributes(lp);
dialog.getWindow().setContentView(view); final EditText et_obj = (EditText) view.findViewById(R.id.et_obj);// 标的
final EditText et_name = (EditText) view.findViewById(R.id.et_name);// 姓名
final EditText et_phone = (EditText) view.findViewById(R.id.et_phone);// 电话
TextView tv_go = (TextView) view.findViewById(R.id.tv_go);// 确认托付
final ImageView iv_close = (ImageView) view.findViewById(R.id.iv_close);// 确认托付

以上就是正确的写法,那么我错在哪里了呢?

之前我的写法是

// 对话框
final Dialog dialog = new AlertDialog.Builder(OnlineActivity.this).create();
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(0));
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.show();

因此仅仅要将AlertDialog换成Dialog就可以解决问题。

试想:

1、假设不加这段代码是什么效果?

dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);



如图。出现了丑陋的标题栏。

2、假设不加这段代码是什么效果?

WindowManager windowManager = getWindowManager();
Display display = windowManager.getDefaultDisplay();
WindowManager.LayoutParams lp = dialog.getWindow().getAttributes();
lp.width = (int) (display.getWidth()); // 设置宽度
dialog.getWindow().setAttributes(lp);



如图。对话框变得这么窄。

Android自己定义dialog中的EditText无法弹出键盘的解决的更多相关文章

  1. android中自定义的dialog中的EditText无法弹出输入法解决方案

    1.解决无法弹出输入法: 在show()方法调用之前,用dialog.setView(new EditText(context))添加一个空的EditText,由于是自定义的AlertDialog,有 ...

  2. Android中使EditText失去焦点,edittext禁止弹出键盘[转]

    转自http://www.cnblogs.com/yejiurui/archive/2013/01/02/2841945.html 在我们的应用中,有时候一进入一个页面, EditText默认就会自动 ...

  3. Android-------- AlertDialog中EditText无法弹出输入法的解决

    文章转自:http://21jhf.iteye.com/blog/2007375: 如果AlertDialog中有编辑录入框(newMainLayout里面动态创建了EditText控件),show后 ...

  4. 关于Android中EditText自动获取焦点并弹出键盘的相关设置

    在android开发中,关于EditText自动获取焦点弹出键盘,我们可能又是会有让键盘自动弹出的需求,有时可能又会有不想让键盘自动弹出的需求,下面是我所总结的两种方法: 需求:EditText自动获 ...

  5. 46.Android 自己定义Dialog

    46.Android 自己定义Dialog Android 自己定义Dialog 前言 提示Dialog 提示Dialog 效果图 菜单Dialog 菜单Dialog 效果图 DialogActivi ...

  6. Android EditText 不弹出输入法

    当第一次进入一个activity的时候  一般是第一个edittext是默认选中的,但是该死的软键盘也一起弹出来了 那是相当的不美观哈!(#‵′)凸.为此, 本大人就去寻找在刚进入这个activity ...

  7. Android EditText不弹出输入法焦点问题的总结

    转自:http://mobile.51cto.com/aprogram-403138.htm 看一个manifest中Activity的配置,如果这个页面有EditText,并且我们想要进入这个页面的 ...

  8. android 闹钟提醒并且在锁屏下弹出Dialog对话框并播放铃声和震动

    android 闹钟提醒并且在锁屏下弹出Dialog对话框并播放铃声和震动            1.先简单设置一个闹钟提醒事件: //设置闹钟 mSetting.setOnClickListener ...

  9. Android EditText不弹出输入法总结,焦点问题的总结

    看一个manifest中Activity的配置,如果这个页面有EditText,并且我们想要进入这个页面的时候默认弹出输入法,可以这样设置这个属相:android:windowSoftInputMod ...

随机推荐

  1. 「 Luogu P3137 」X 「 USACO16FEB 」 圆形谷仓

    # 题目大意 管理大大给修下 $\text{Markdown}$ 吧,严重影响做题体验啊. 这道题的意思很简单就是给你一个长度是 $n$ 的环,这个环上不均匀的分布着 $n$ 头奶牛.一头奶牛移动要花 ...

  2. [Python3网络爬虫开发实战] 1.5.1-PyMySQL的安装

    在Python 3中,如果想要将数据存储到MySQL中,就需要借助PyMySQL来操作,本节中我们介绍一下它的安装方式. 1. 相关链接 GitHub:https://github.com/PyMyS ...

  3. buf.fill()

    buf.fill(value[, offset[, end]][, encoding]) value {String} | {Buffer} | {Number} offset {Number} 默认 ...

  4. 创建Tensor

    目录 创建Tensor numpy, list numpy list zeros, ones, fill zeros ones fill random 打乱idx后,a和b的索引不变 constant ...

  5. 将一个list中的元素的某一属性取出来单独放到一个list里面

    有很多时候我们会遇到这样的场景,就是要将一个list中的某一个元素中的某一属性单独拿出来放在一个新的list里面,这中时候,我们就可以用以下的方法来进行实现: List<DTO> item ...

  6. 多光源 MultipleLight

    使用2个Pass增加光照效果: 第一个Pass是基础光源,一般是第一个平行光:Tags{"LightMode" = "ForwardBase"} 第二个光源是增 ...

  7. String replaceAll-正则匹配-截取以指定字符开头,以指定字符结尾的字符串

    scala代码块 截取以某个字符开头,以某个字符结尾的字符串 def main(args: Array[String]): Unit = { val s = "{{a61,a2,a3},{b ...

  8. 【(待重做)树状数组+dp+离散化】Counting Sequences

    https://www.bnuoj.com/v3/contest_show.php?cid=9149#problem/G [题意] 给定一个数组a,问这个数组有多少个子序列,满足子序列中任意两个相邻数 ...

  9. codeforces 363A

    #include<stdio.h>//这题挺有意思小学学的算盘 int main() { int n,i,m; while(scanf("%d",&n)!=EO ...

  10. Eclipse4.5在线安装Aptana插件及配置代码提示教程

    一.Aptana插件官网地址         我在网上试过登陆到aptana官网后点击下载,选择下载eclipse插件版,然后页面给出一串地址:http://download.aptana.com/s ...