【发布时间】:2017-09-22 16:45:42
【问题描述】:
这是我需要从AlertDialog的EditText获取文本的方法
public void checkButton() {
leggspillere = (Button) findViewById(R.id.leggspillere);
final LayoutInflater inflater = this.getLayoutInflater();
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setView(inflater.inflate(R.layout.create_user,null));
builder.setMessage("Lag en spiller");
builder.setCancelable(true);
builder.setPositiveButton("Ok",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// trying to get the EditText-string inside alertdialogbox input here.
final String Name = UserName.
Person person = new Person(Name, 0);
dialog.dismiss();
spillere.setText(person.getName() + " " + person.getScore());
}
}
);
//sette andre knappen "cancel"
builder.setNegativeButton("no",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
}
);
AlertDialog alert = builder.create();
alert.show();
}
XML:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:layout_width="match_parent"
android:layout_height="64dp"
android:scaleType="center"
android:background="#FFFFBB33"
android:contentDescription="@string/app_name" />
<EditText
android:id="@+id/UserName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="4dp"
android:layout_marginLeft="4dp"
android:layout_marginRight="4dp"
android:layout_marginTop="16dp"
android:inputType="textEmailAddress" />
</LinearLayout>
尝试从EditText.xml 获取输入/字符串以创建person 对象。
始终引用 null 对象。我找不到任何解决方案,请指点我。
【问题讨论】:
-
是否将您的编辑文本溢出到您要设置文本的位置?
-
那是一个TextView,我想直观地代表所有添加的玩家。
-
“这就是问题所在”不应以代码开头,而应以您实际遇到的实际口头技术问题开头。
标签: java android xml android-edittext android-alertdialog