将editText存储到变量,Android Java



我之前发布过这个问题,但我没有很好地解释自己。我将删除另一个问题。以下是我目前的情况:

我有一个带有textView的常规xml页面,单击后会打开一个弹出对话框。此对话框包含2个editText。目前,我的代码(OnClick–Done按钮)获取两个编辑文本的值,并将它们放入单个TextView中。然而,当我再次打开弹出窗口时,存储在文本视图中的组合字符串显示在一个编辑文本中,而不是在其自己的editText中列出两个字符串(每个字符串最初输入的位置)。问题是,尽管我从两个不同的editText中获取字符串,并将它们存储在一个textView中。我无法单独取回每根绳子。我知道我可能必须将每个editText中的字符串存储到变量中,然后我可以使用这些变量来显示textView中组合的字符串(以及editText——当我再次打开弹出对话框时)。我该如何处理?感谢的帮助

代码:

public class MainActivity extends Activity {
/** Called when the activity is first created. */
TextView showPopUpButton;
EditText getInput;
EditText getInput2;
String myvalue = "";
String myvalue2 = "";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

showPopUpButton = (TextView) findViewById(R.id.buttonShowPopUp);
showPopUpButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
showPopUp3();              }
});
}


private void showPopUp3() {

AlertDialog.Builder helpBuilder = new AlertDialog.Builder(this);
helpBuilder.setTitle("Enter PU Builder");

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

getInput = (EditText)  checkboxLayout.findViewById(R.id.editText1);
getInput2 = (EditText)  checkboxLayout.findViewById(R.id.editText2);

getInput.setText(myvalue);
getInput2.setText(myvalue2);

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

@Override   
public void onClick(DialogInterface dialog, int which) 
{
myvalue =  getInput.getText().toString();
showPopUpButton.setText(myvalue + ", " + myvalue2);
}
});

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

首先,您需要字符串来保存EditText值,并像这样声明

public class MainActivity extends Activity {
/** Called when the activity is first created. */
TextView showPopUpButton; //NEW
EditText getInput; //NEW
EditText getInput2; //NEW
// declare string to save the dialog edittext
String myValue = "" ;

那么你需要在EditText中显示对话框的最后一个值,所以试试这个:

private void showPopUp3() {
AlertDialog.Builder helpBuilder = new AlertDialog.Builder(this);
helpBuilder.setTitle("Enter PU Builder");
LayoutInflater inflater = getLayoutInflater();
View checkboxLayout = inflater.inflate(R.layout.popuplayout, null);
getInput = (EditText)  checkboxLayout.findViewById(R.id.editText1); //MISTAKE
getInput2 = (EditText)  checkboxLayout.findViewById(R.id.editText2); //MISTAKE
getInput.setText(showPopUpButton.getText()); //New to keep the text in the editText when done is pressed
getInput2.setText(getInput2.getText()); //New test
// here set the my value to edit text  , note firs time will be empty 
getInput.setText(myValue) 

最后一件事是,当你点击对话框中的完成按钮时,你需要保存EditText值,如下所示:

@Override   
public void onClick(DialogInterface dialog, int which){
//showPopUpButton.setText(getInput.getText() + ", " + getInput2.getText());//NEW
//showPopUpButton.setText(value) ;
// save the edit text value into myvalue string 
myvalue =  getInput.getText().toString();
}
});

将反馈给我

这相当简单。

创建用于放置字符串的变量。

String inputText;

在适用的情况下,获取并设置。

inputText = editText.getText().toString();
textView.setText(inputText);

我理解对了吗?这就是你想要实现的吗?

最新更新