gpt4 book ai didi

java - 安卓 Java : Diaglog setText & getText issue

转载 作者:行者123 更新时间:2023-12-01 13:24:09 25 4
gpt4 key购买 nike

我有一个对话框,当选择 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/

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