`

AlertDialog中获取自定义控件

    博客分类:
  • UI
阅读更多

 

最近写了一个自定义布局的Dialog,运行效果如下:

 

 

然后我想获取到自定义布局中的EditText控件,并得到其中的字符串,但是使用send_email = (EditText) findViewById(R.id.send_email_dialog_et);并不能获取到控件对象,经测试发现send_email对象为null。。。。

没办法,只好到一个技术群里问一下别人,很幸运我找到了解决方法,关键代码如下:

Java代码 
  1. final View layout = inflater.inflate(R.layout.send_email_dialog,  
  2.                 (ViewGroup) findViewById(R.id.send_email_dialog_ll));  
  3. final EditText sendEmailCon_ET = (EditText) layout.findViewById(R.id.send_email_dialog_et);  

那位提供解决方法的网友说:想要获取自定义布局中的控件必须调用你所定义的View的findViewById方法,而不能像获得其他控件一样直接调用findViewById方法。。。。

 

最后附上dialog的自定义布局代码:

 

Html代码 
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:id="@+id/send_email_dialog_ll"  
  4.     android:orientation="vertical"  
  5.     android:layout_width="match_parent"  
  6.     android:layout_height="match_parent">  
  7.     <TextView  
  8.         android:layout_width="wrap_content"  
  9.         android:textAppearance="?android:attr/textAppearanceMedium"  
  10.         android:layout_height="wrap_content"  
  11.         android:text="To : wuxianglong098@163.com"  
  12.         android:layout_gravity="center"  
  13.         android:layout_marginTop="4dp" />  
  14.     <EditText  
  15.         android:id="@+id/send_email_dialog_et"  
  16.         android:layout_height="wrap_content"  
  17.         android:layout_width="fill_parent"  
  18.         android:inputType="textMultiLine"  
  19.         android:lines="4"  
  20.         android:hint="@string/send_email_hint"  
  21.         android:scrollbarAlwaysDrawVerticalTrack="true"  
  22.         android:layout_marginTop="9dp">  
  23.         <requestFocus></requestFocus>  
  24.     </EditText>  
  25. </LinearLayout>  

 

THE END!

分享到:
评论

相关推荐

    android 自定义AlertDialog 与Activity相互传递数据

    主要实现功能: 1、从Activity的TextView中获取字符串设置到...2、如何对自定义AlertDialog中的控件进行操作? 解答: 于fragment中的操作类似,首先要获取该layout的view,然后通过该view获取到其中控件进行操作。

    Android AlertDialog、ActionSheetDialog模仿IOS7风格对话框.rar

    主要使用了AlertDialog、ActionSheetDialog来实现,AlertDialog的方法和android中自带的用法相似,ActionSheetDialo的用法和AlertDialog差不多,基本的使用思路是:获取Dialog布局、获取自定义Dialog布局中的控件、...

    安卓源码包android web应用OCR图像识别listview相关EditText输入框Launcher 桌面45个合集

    [四次元]Android中实现Iphone样式的AlertDialog.zip [四次元]Android实现Windows风格的Dialog.zip [四次元]dlna库源代码包.zip [四次元]gridview分页效果.zip [四次元]HerilyAlertDialog完全自定义的Dialog.zip [四...

    Android 开发技巧

    9.36、从输入流中获取数据并以字节数组返回 250 9.37、通过ANDROID 客户端上传数据到服务器 251 9.38、文件下载类 255 9.39、下载文件的进度条提示 263 9.40、通过HTTPCLIENT从指定SERVER获取数据 265 9.41、通过FTP...

    Android开发资料合集-World版!

    9.36、从输入流中获取数据并以字节数组返回 250 9.37、通过ANDROID 客户端上传数据到服务器 251 9.38、文件下载类 255 9.39、下载文件的进度条提示 263 9.40、通过HTTPCLIENT从指定SERVER获取数据 265 9.41、通过FTP...

    Android典型技术模块开发详解

    8.9.1 AlertDialog警告框 8.9.2 ProgressDialog进度框 8.9.3 DatePickerDialog & TimePickerDialog(日期时间选择框) 8.10 Menu(菜单) 8.10.1 options menu 8.10.2 context menu 8.10.3 sub menu 8.11 ...

    疯狂Android讲义源码

     2.5.3 使用AlertDialog创建自定义  对话框 116  2.5.4 使用PopupWindow 121  2.5.5 使用DatePickerDialog、TimePickerDialog 123  2.5.6 使用ProgressDialog创建进度  对话框 125  2.6 消息提示 127  2.6.1...

    疯狂Android讲义.part2

    2.5.3 使用AlertDialog创建自定义 对话框 116 2.5.4 使用PopupWindow 121 2.5.5 使用DatePickerDialog、TimePickerDialog 123 2.5.6 使用ProgressDialog创建进度 对话框 125 2.6 消息提示 127 2.6.1 使用Toast显示...

    疯狂Android讲义.part1

    2.5.3 使用AlertDialog创建自定义 对话框 116 2.5.4 使用PopupWindow 121 2.5.5 使用DatePickerDialog、TimePickerDialog 123 2.5.6 使用ProgressDialog创建进度 对话框 125 2.6 消息提示 127 2.6.1 使用Toast显示...

Global site tag (gtag.js) - Google Analytics