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

今天在做一个新项目,专为律师和客户开发的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. 修改python注册表

    转自:http://blog.csdn.net/u014680513/article/details/51005650 # script to register Python 2.0 or later ...

  2. BZOJ 2406 LuoguP4194 矩阵 有上下界可行流

    分析: 这道题乍一看……卧槽这都什么玩意…… 然后发现给了个A矩阵,要求一个可行的B矩阵,使得矩阵C=A-B的每一行的和的绝对值和每一列的和的绝对值的最大值最小…… 好拗口啊…… 什么最大值最小之类的 ...

  3. Linux 内核框架图

  4. 简谈Redis

    1.为什么使用redis 分析:博主觉得在项目中使用redis,主要是从两个角度去考虑:性能和并发.当然,redis还具备可以做分布式锁等其他功能,但是如果只是为了分布式锁这些其他功能,完全还有其他中 ...

  5. 树莓派 -- 输入设备驱动 (key) 续2: 转载 Setting up a GPIO-Button “keyboard” on a Raspberry Pi

    使用device-tree (DT) overlay应该是更方便的方法: http://blog.gegg.us/2017/01/setting-up-a-gpio-button-keyboard-o ...

  6. LeetCode(30) Substring with Concatenation of All Words

    题目 You are given a string, s, and a list of words, words, that are all of the same length. Find all ...

  7. windows枚举串口

    1. 枚举键值 HKEY_LOCAL_MACHINE\HARDWARE\DEVICEMAP\SERIALCOMM 2. SETUPAPI方式 int EnumPortsWdm() { int i, d ...

  8. 集训第四周(高效算法设计)D题 (区间覆盖问题)

    原题 UVA10020  :http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=19688 经典的贪心问题,区间上贪心当然是右区间越 ...

  9. xcap发包工具的简单使用1(构造报文)

    xcap是一个免费的网络发包工具,可以构造和发送常用的网络报文,如arp,ip,icmp,udp等,支持构造报文和发送报文等. 报文隶属于报文组,每个报文组包含多个报文,因此,创建报文首先要创建报文组 ...

  10. hdu 3657 最小割(牛逼!!!!)总算理解了

    <strong></strong> 转载:http://blog.csdn.net/me4546/article/details/6662959 加颜色的太棒了!!! 在网上看 ...