【问题标题】:How to get a result data to edit text without clicking on button?如何在不单击按钮的情况下获取结果数据来编辑文本?
【发布时间】:2016-02-24 06:58:11
【问题描述】:

我创建了一个转换器应用程序,我使用了 2 个编辑文本一个获取输入和其他显示输出,它将通过单击按钮显示输出是否有任何方法可以在不使用按钮的单击事件的情况下在 2 个编辑文本中获取输出显示。当我们在 1 个编辑文本中输入输入时,我希望将结果自动放置在 2 个编辑文本中。

EditText input,result;

input = (EditText) findViewById(R.id.ip);
result = (EditText) findViewById(R.id.res);  // my edit texts input and result
result.setClickable(false);
public void convert(View view){ //when clicking it get result to 2 edit text, but I want to get automatically to the second edit text when user enter the input 
    if (!input.getText().toString().equals("")){
        ufrom = (String) sp1.getSelectedItem();
        uto = (String) sp2.getSelectedItem();
        Double ip = Double.valueOf(input.getText().toString());
        TemperatureConverter.Units fromUnit = TemperatureConverter.Units.fromString(ufrom);
        TemperatureConverter.Units toUnit = TemperatureConverter.Units.fromString(uto);
        double r = con.TemperatureConvert(fromUnit,toUnit,ip);
        result.setText(String.valueOf(r));
    } else {
        result.setText("");
    }
}

【问题讨论】:

  • 您想在编辑文本中输入哪种类型的值??

标签: android android-activity android-edittext


【解决方案1】:

您可以为此目的使用 TextWatcher。您将在此侦听器中获得每个字符的值。根据您的需要更新您的输出 editText

editText1.addTextChangedListener(new TextWatcher() {
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

                // TODO Auto-generated method stub
            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

                // TODO Auto-generated method stub
            }

            @Override
            public void afterTextChanged(Editable s) {

                // Place the logic here for your output edittext
            }
        });

【讨论】:

    【解决方案2】:

    是的,你可以通过检测键盘操作完成

    et.setOnEditorActionListener(new TextView.OnEditorActionListener() {
        @Override
        public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
            if (actionId == EditorInfo.IME_ACTION_DONE) {
                // set result in second edit text 
                return true;
            }
            return false;
        }
    });
    

    【讨论】:

      【解决方案3】:

      这里你可以像这样为输入EditText添加TextWatcher,

        input.addTextChangedListener(new TextWatcher() {
                      @Override
                      public void beforeTextChanged(CharSequence s, int start, int count, int after) {
      
                      }
      
                      @Override
                      public void onTextChanged(CharSequence s, int start, int before, int count) {
      
      
                          // Check some Validations and Conditions here
      
                          // Convert function goes here if all validationand conditions checked
                          convert();
      
                      }
      
                      @Override
                      public void afterTextChanged(Editable s) {
      
                      }
                  });
      

      希望对你有帮助,我的朋友!

      【讨论】:

        猜你喜欢
        • 2014-11-20
        • 1970-01-01
        • 2021-11-25
        • 1970-01-01
        • 1970-01-01
        • 2019-02-06
        • 2015-08-25
        • 1970-01-01
        • 2023-03-18
        相关资源
        最近更新 更多