gpt4 book ai didi

具有自定义布局的 Android 对话框 : returning data

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:38:30 24 4
gpt4 key购买 nike

我在获取数据时遇到问题,用户在 dialog(使用自定义布局)中输入。看代码:

单元.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">

<EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</EditText>
</RelativeLayout>

代码:

    AlertDialog.Builder builder = new AlertDialog.Builder(this);
LayoutInflater inflater = this.getLayoutInflater();
builder.setView(inflater.inflate(R.layout.unit, null));
AlertDialog dialog = builder.create();
dialog.show();

对话框 以我创建的布局打开,但是

    builder.setPositiveButton("Add", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
EditText text = (EditText)findViewById(R.id.editText1);
Toast.makeText(getApplicationContext(), text.getText(), Toast.LENGTH_SHORT).show();
dialog.dismiss();
}

});

产生运行时错误:java.lang.NullPointerException。问题出在哪里?

最佳答案

用这个..

 builder.setPositiveButton("Add", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
EditText text = (EditText) dialog.findViewById(R.id.editText1);
Toast.makeText(getApplicationContext(), text.getText(), Toast.LENGTH_SHORT).show();
dialog.dismiss();
}

});

关于具有自定义布局的 Android 对话框 : returning data,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14102839/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com