将editText存储到变量,Android Java

时间:2014-02-19 22:25:35

标签: java android variables gettext settext

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

我有一个带有textView的常规xml页面,当单击它时会打开一个弹出对话框。该对话框包含2个editText。目前,我的代码(OnClick - Done按钮)获取两个编辑文本的值,并将它们放入单个TextView中。但是,当我再次打开弹出窗口时,而不是在其自己的editText中列出的两个字符串(其中每个字符串最初输入),存储在文本视图中的组合字符串出现在一个编辑文本中。问题是虽然我从2个不同的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();
 }
}

2 个答案:

答案 0 :(得分:0)

这很简单。

创建变量以放置你的字符串。

String inputText;

适用时,获取并设置。

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

我理解正确吗?这是你想要完成的事情吗?

答案 1 :(得分:0)

首先,你需要字符串来保存你的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();
        }

        });

喂我回来