java - 安卓 Java : Diaglog setText & getText issue

标签 java android eclipse

我有一个对话框,当选择 TextView 时会打开该对话框。该对话框包含一个 editText。 我想要做的是能够在 editText 中输入一个字符串,当按下“完成”按钮时,它将把 editText 中的字符串存储到一个变量中,可以通过原始 textView 查看该变量。 根据eclipse我的源代码是正确的。但是当我在 Android 手机上运行它时,它崩溃了。我知道这是 OnClick Listner 的原因。

下面是我的源代码:

public class MainActivity extends Activity {
/** Called when the activity is first created. */

TextView showPopUpButton;//NEW
EditText getInput;//NEW

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    showPopUpButton = (TextView) findViewById(R.id.buttonShowPopUp);//NEW
    getInput = (EditText) findViewById(R.id.editText1);//NEW

    showPopUpButton.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            showPopUp3();              }
        });
}


private void showPopUp3() {
    AlertDialog.Builder helpBuilder = new AlertDialog.Builder(this);
    helpBuilder.setTitle("Enter your string");

    LayoutInflater inflater = getLayoutInflater();
    View checkboxLayout = inflater.inflate(R.layout.popuplayout, null);
    helpBuilder.setView(checkboxLayout);

    helpBuilder.setPositiveButton("Done", new DialogInterface.OnClickListener() {
        TextView showPopUpButton;//NEW
        EditText getInput;//NEW
        public void onClick(DialogInterface dialog, int which) {
            // THIS IS SUPPOSED TO STORE THE VALUE OF THE EDIT-TEXT AND OUTPUT IT IN THE TEXTVIEW
                showPopUpButton = (TextView) findViewById(R.id.buttonShowPopUp);//NEW
            showPopUpButton.setText(getInput.getText());//NEW**
            }
        });

    AlertDialog helpDialog = helpBuilder.create();
    helpDialog.show();
}

}

谢谢大家,我知道这个解决方案可能很简单,我只是缺少一些小东西。 谢谢

最佳答案

我认为您的 TextView 位于主布局中,EditText 位于 popuplayout.xml 中。 如果是这种情况,请尝试以下代码。你不能直接获取EditText的引用,你必须从它的布局(checkboxlayout)中找到它。

private void showPopUp3() {
AlertDialog.Builder helpBuilder = new AlertDialog.Builder(this);
helpBuilder.setTitle("Enter your string");

LayoutInflater inflater = getLayoutInflater();
View checkboxLayout =inflater.inflate(R.layout.popuplayout, null);
getInput = (EditText)  checkboxLayout.findViewById(R.id.editText1);
getInput.setText(YOUR_STRING);
helpBuilder.setView(checkboxLayout);

helpBuilder.setPositiveButton("Done", new DialogInterface.OnClickListener() {


    public void onClick(DialogInterface dialog, int which) {
        // THIS IS SUPPOSED TO STORE THE VALUE OF THE EDIT-TEXT AND OUTPUT IT IN THE TEXTVIEW

        showPopUpButton.setText(getInput.getText());//NEW**
        }
    });

AlertDialog helpDialog = helpBuilder.create();
helpDialog.show();
}

关于java - 安卓 Java : Diaglog setText & getText issue,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21870263/

相关文章:

当我在代码中放入 while 循环时,JavaFX GUI 不会运行,但在删除时会运行

java.lang.NoSuchMethodError : com. mongodb.connection.DefaultClusterFactory.createCluster 错误

java - 如何在 Hadoop MapReduce java API 中使用 Java 断言?

java - Android Room 插入所有问题

android - 如何使用 “sync projet with gradle files”按钮?

android - 使用集合更新应用程序小部件的问题

Android:创建持续闹钟(例如:早上闹钟、来电)

eclipse - 如何正确设置 Node.js + Eclipse + TypeScript?

java - 如何从 Eclipse 项目中的 res 文件夹中获取文件?

android - 无法更改默认输出文件夹