【问题标题】:Storing editText to variables, Android Java [closed]将editText存储到变量,Android Java [关闭]
【发布时间】:2014-02-19 22:25:34
【问题描述】:

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

我有一个带有 textView 的常规 xml 页面,单击它会打开一个弹出对话框。此对话框包含 2 个编辑文本。目前我的代码(OnClick - 完成按钮)获取两个编辑文本的值并将它们放入单个 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();
 }
}

【问题讨论】:

  • 我不明白,你想把编辑文本值保存在变量中吗,所以试试这个 String value = getInput.getText().toString() ;
  • 我试过了,但后来 onClickListener 出现了问题。我到底需要用什么来替换 onClick Listner 方法?
  • 您能发布 onClickListener 代码吗?其中有什么问题?
  • 很乱,你能用新代码编辑你的问题吗?
  • 问题在于“showPopUpButton.setText(value);”

标签: java android variables gettext settext


【解决方案1】:

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

        });

喂我回来

【讨论】:

  • 抱歉让我感到困惑。 "" 里面会写什么?我想要的是用户能够在editText中输入一个字符串,当他们选择完成按钮时,它将字符串存储在一个变量中。然后可以使用它们的变量在editText 和TextView 中显示。谢谢
  • "" 这意味着字符串在您第一次开始活动时将为空,但您将在对话框中的完成按钮之后对其进行初始化,然后如果您再次打开对话框,您将使用最后一个设置文本价值
  • 这是你需要的吗?
  • 那是正确的,我该怎么做
  • 我在上面编辑了我的答案并反馈给我\
【解决方案2】:

这很简单。

创建变量来放置您的字符串。

String inputText;

如果适用,获取并设置。

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

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

【讨论】:

  • 我想将 inputText1 存储到一个变量中。然后在editText 中查看变量。而且当我再次打开editText时,变量中的字符串也会显示出来。
  • 我很抱歉,但我仍然无法理解。 “我想将 iT1 存储到一个变量中。” inputText1 是一个 String 变量。 “在编辑文本中查看变量”。 inputText2.setText("some text");。就这么简单。
  • 基本上,当我按下“完成”按钮时。我希望编辑文本的内容存储在一个变量中。然后我可以使用这个变量来查看 editText 和 TextView 的值
  • 我很抱歉,我的代码有点混乱,但我已经修复了。创建变量以放置您的文本line 1。在完成按钮中,分配值line 2,并将其设置为另一个文本视图line 3。现在,您将拥有变量 inputText 和分配的值,以便在需要的任何地方使用。
  • 对不起,您能否详细说明第 2 行和第 3 行
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-06-02
  • 1970-01-01
  • 2018-09-01
  • 2014-07-06
  • 1970-01-01
相关资源
最近更新 更多