Android无法得到的EditText的getText()的toString()在对话框 [英] Android can't get EditText getText().toString() in a Dialog
问题描述
我想创建一个自定义对话框中的一个类。开始在主要活动的对话框:
DialogLogin登录=新DialogLogin();
login.show(getFragmentManager(),DISPLAY_SERVICE);
在启动主活动开始在后台应用程序,然后在对话框开始。 在对话框有一个EditText场来接收用户输入。在pressing保存按钮的的EditText场应该读出并显示输入,但它总是空的。此外还有在LogCat中没有错误......我想同样的问题的许多解决方案,但毫无效果。我希望任何人有一个有效的解决方案! =)
进口android.app.AlertDialog;
进口android.app.Dialog;
进口android.app.DialogFragment;
进口android.content.DialogInterface;
进口android.os.Bundle;
进口android.view.LayoutInflater;
进口android.view.View;
进口android.widget.EditText;
公共类DialogLogin扩展DialogFragment {
字符串androidID;
LayoutInflater充气;
公共对话onCreateDialog(包savedInstanceState){
AlertDialog.Builder建设者=新AlertDialog.Builder(getActivity());
充气= getActivity()getLayoutInflater()。
builder.setMessage(R.string.loginMessage)
.setTitle(R.string.login)
.setView(inflater.inflate(R.layout.loginlayout,NULL))
.setPositiveButton(R.string.speichern,
新DialogInterface.OnClickListener(){
公共无效的onClick(DialogInterface对话框,INT ID){
的System.out.println(登录);
视图V = inflater.inflate(R.layout.loginlayout,NULL);
文字的EditText =(EditText上)v.findViewById(R.id.loginEdit);
的System.out.println(text.getText()的toString()); //显示什么
的System.out.println(text.length()); //为0
}
});
返回builder.create();
}
}
而loginlayout.xml:
< XML版本=1.0编码=UTF-8&GT?;
< LinearLayout中的xmlns:机器人=http://schemas.android.com/apk/res/android
机器人:layout_width =match_parent
机器人:layout_height =match_parent
机器人:方向=垂直>
<的EditText
机器人:ID =@ + ID / loginEdit
机器人:inputType =文本
机器人:layout_width =match_parent
机器人:layout_height =WRAP_CONTENT
机器人:EMS =10
机器人:提示=@字符串/ EDITTEXT>
< /的EditText>
< / LinearLayout中>
正在膨胀的一个新的布局,其中的EditText
中没有任何文字。你只需要一次吹你的布局,并保持对它的引用。
最终视图中查看= inflater.inflate(R.layout.loginlayout,NULL);
/ * ... * /
.setView(视图)
/ * ... * /
文字的EditText =(EditText上)view.findViewById(R.id.loginEdit);
I'm trying to create a custom Dialog in an individual class. The dialog is started in the main activity:
DialogLogin login = new DialogLogin();
login.show(getFragmentManager(), DISPLAY_SERVICE);
On starting the application the main activity starts in the background and then the dialog starts. In the dialog there is an EditText-field to receive user-input. On pressing the save-button the EditText-field should be read-out and the input displayed, but it's always empty. Moreover there are no errors in the LogCat... I tried many solutions of the same problem, but nothing worked. I hope anyone has a working solution ! =)
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.DialogFragment;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
public class DialogLogin extends DialogFragment {
String androidID;
LayoutInflater inflater;
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
inflater = getActivity().getLayoutInflater();
builder.setMessage(R.string.loginMessage)
.setTitle(R.string.login)
.setView(inflater.inflate(R.layout.loginlayout, null))
.setPositiveButton(R.string.speichern,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
System.out.println("LOGIN");
View v = inflater.inflate(R.layout.loginlayout, null);
EditText text = (EditText) v.findViewById(R.id.loginEdit);
System.out.println(text.getText().toString()); //Displays nothing
System.out.println(text.length()); //is 0
}
});
return builder.create();
}
}
And the loginlayout.xml:
<?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:orientation="vertical" >
<EditText
android:id="@+id/loginEdit"
android:inputType="text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="@string/editText" >
</EditText>
</LinearLayout>
You are inflating a new layout, where the EditText
has no text in it. You'll need to only once inflate your layout, and keep a reference to it.
final View view = inflater.inflate(R.layout.loginlayout, null);
/* ... */
.setView(view)
/* ... */
EditText text = (EditText) view.findViewById(R.id.loginEdit);
这篇关于Android无法得到的EditText的getText()的toString()在对话框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!