edittext-Android - 无法从EditText Control读取文本

问题描述

Android - 无法从EditText Control读取文本
程序中有一个AlertDialog对话框,使用了一个自定义布局。在OnClick事件中我想读出用户输入的信息。使用了以下的代码:

//Login Dialog            AlertDialog.Builder builder = new AlertDialog.Builder(this);            // Get the layout inflater            LayoutInflater inflater = this.getLayoutInflater();            // Inflate and set the layout for the dialog            // Pass null as the parent view because its going in the dialog layout            builder.setView(inflater.inflate(R.layout.dialog_signin null))            .setPositiveButton(R.string.tvLoginTitle new DialogInterface.OnClickListener() {                   @Override                   public void onClick(DialogInterface dialog int id) {                       String username = ((EditText)findViewById(R.id.loginUsername)).getText().toString();                       String password = ((EditText)findViewById(R.id.loginPassword)).getText().toString();                       System.out.println(username);                       System.out.println(password);                       ConnectionID.loginToServer(cMainActivity username password);                   }               })               .setNegativeButton(R.string.btnExit new DialogInterface.OnClickListener() {                   public void onClick(DialogInterface dialog int id) {                       System.exit(0);                   }               }).show(); 

但是当我想要读出信息的时候,获得一个来自dialog_signin的布局中NullPointerException异常。

<EditText    android:id=""@+id/loginUsername""    android:inputType=""textEmailAddress""    android:layout_width=""match_parent""    android:layout_height=""wrap_content""    android:layout_marginTop=""16dp""    android:layout_marginLeft=""4dp""    android:layout_marginRight=""4dp""    android:layout_marginBottom=""4dp""    android:hint=""@string/tvUsername"" /><EditText    android:id=""@+id/loginPassword""    android:inputType=""textPassword""    android:layout_width=""match_parent""    android:layout_height=""wrap_content""    android:layout_marginTop=""4dp""    android:layout_marginLeft=""4dp""    android:layout_marginRight=""4dp""    android:layout_marginBottom=""16dp""    android:fontFamily=""sans-serif""    android:hint=""@string/tvPassword""/>

解决方案

如果用户名和密码是对话框中的,你应该使用以下代码:

View v = inflater.inflate(R.layout.dialog_signin null); builder.setView(v);String username = ((EditText)v.findViewById(R.id.loginUsername)).getText().toString();String password = ((EditText)v.findViewById(R.id.loginPassword)).getText().toString();

解决方案二:

//Login Dialog        AlertDialog.Builder builder = new AlertDialog.Builder(this);        // Get the layout inflater        LayoutInflater inflater = this.getLayoutInflater();        // Inflate and set the layout for the dialog        // Pass null as the parent view because its going in the dialog layout        View view = inflater.inflate(R.layout.dialog_signin null);        final EditText txtuser = (EditText)findViewById(R.id.loginUsername);        final EditText txtpass = (EditText)findViewById(R.id.loginPassword);        builder.setView(view)        .setPositiveButton(R.string.tvLoginTitle new DialogInterface.OnClickListener() {               @Override               public void onClick(DialogInterface dialog int id) {                   String username = txtuser.getText().toString();                   String password = txtpass.getText().toString();                   System.out.println(username);                   System.out.println(password);                   ConnectionID.loginToServer(cMainActivity username password);               }           })           .setNegativeButton(R.string.btnExit new DialogInterface.OnClickListener() {               public void onClick(DialogInterface dialog int id) {                   // System.exit(0); // don't do this in Android               }           }).show(); 

解决方案三:
我使用以下的对话框后能很好的实现。

Dialog builder = new Dialog(this);builder.setContentView(R.layout.dialog_signin);String username = ((EditText)builder.findViewById(R.id.loginUsername)).getText().toString();String password = ((EditText)builder.findViewById(R.id.loginPassword)).getText().toString();//你的按钮builder.show();
时间: 2024-08-03 08:53:04

edittext-Android - 无法从EditText Control读取文本的相关文章

软键盘-Android webview的edittext

问题描述 Android webview的edittext Android webview的edittext 未输入文本时被软键盘遮挡,输入文本后软键盘能把输入框顶上去,怎么做到点一下输入框直接把输入框顶上去,就是不被软键盘遮挡,设置adjustpan和adjustresize都没用 解决方案 Android 软键盘盖住输入框的问题 解决方案二: 不希望遮挡设置activity属性android:windowSoftInputMode="adjustPan" 希望动态调整高度andro

全面解析Android中对EditText输入实现监听的方法_Android

在 Android design support 包中提供了一种在输入不合适字符时一直显示的提示方式来显示,现在已经开始在更多的应用上被使用了:这些 Android app 在显示他们的错误提示时采用的不同的方式常常让人感觉非常的不和谐. 即这个一直显示的错误消息是在 TextInputLayout 中的 EditText 周围的.这也是,作为一个奖励,提供了材料设计风格中,活泼的浮动标签在一个 APP 的用户体验中常常是最无聊的部分. 这里来讨论如何在你的输入表单上去创建一个通用的.可重用的组

Android如何自定义EditText下划线?_Android

曾经做过一个项目,其中登录界面的交互令人印象深刻.交互设计师给出了一个非常作的设计,要求做出包含根据情况可变色的下划线,左侧有可变图标,右侧有可变删除标志的输入框,如图 记录制作过程: 第一版本 public class LineEditText extends EditText { private Paint mPaint; private int color; public static final int STATUS_FOCUSED = 1; public static final in

Android编程中EditText限制文字输入的方法_Android

本文实例讲述了Android编程中EditText限制文字输入的方法.分享给大家供大家参考,具体如下: Android的编辑框控件EditText在平常编程时会经常用到,有时候会对编辑框增加某些限制,如限制只能输入数字,最大输入的文字个数,不能输入一些非法字符等,这些需求有些可以使用android控件属性直接写在布局xml文件里,比如android:numeric="integer"(只允许输入数字): 对于一些需求,如非法字符限制(例如不允许输入#号,如果输入了#给出错误提示),做成

Android编程实现EditText字数监听并显示的方法

本文实例讲述了Android编程实现EditText字数监听并显示的方法.分享给大家供大家参考,具体如下: 在开发应用的时候,经常会限制用户输入的字数,比如发表评论或者其它什么的,下面来个简单的demo EditText et_content;//定义一个文本输入框 TextView tv_num;// 用来显示剩余字数 int num = 10;//限制的最大字数 et_content = (EditText) findViewById(R.id.et_content); tv_num = (

Android编程中EditText限制文字输入的方法

本文实例讲述了Android编程中EditText限制文字输入的方法.分享给大家供大家参考,具体如下: Android的编辑框控件EditText在平常编程时会经常用到,有时候会对编辑框增加某些限制,如限制只能输入数字,最大输入的文字个数,不能输入一些非法字符等,这些需求有些可以使用android控件属性直接写在布局xml文件里,比如android:numeric="integer"(只允许输入数字): 对于一些需求,如非法字符限制(例如不允许输入#号,如果输入了#给出错误提示),做成

Android编程之EditText常见操作示例

本文实例讲述了Android编程之EditText常见操作.分享给大家供大家参考,具体如下: 1.获取光标选中的文字 EditText view = (EditText)findViewById(R.id.edt); int start = view.getSelectionStart(); int end = view.getSelectionEnd(); //由于选择的位置和你开始选择文字的顺序有关,所以最好重新判断整理一下顺序,免得出错 if (start>end) { start = s

android如何将EditText上输入的文字显示在webview上

问题描述 android如何将EditText上输入的文字显示在webview上 android如何将EditText上输入的文字显示在webview上 解决方案 Android:EditText限制文字输入Android:EditText限制文字输入Android 关于EditText文字的显示问题 和属性 解决方案二: 没读懂你的问题,你是希望EditText上输入的文字以什么样的形式显示在webview上? 能否具体描述下? 解决方案三: 用webview有方法可以操作里边的内容.

android中的edittext获得光标就报这样的错误,求大神指点怎么解决啊

问题描述 android中的edittext获得光标就报这样的错误,求大神指点怎么解决啊 5C 04-29 00:37:49.491 2548-2548/jt.com.shoppingcar W/Resources: Drawable android:drawable/text_cursor_material has unresolved theme attributes! Consider using Resources.getDrawable(int Theme) or Context.ge