如何从edittext获取用户输入并在同一活动的另一个edittext中显示

时间:2017-01-29 07:14:45

标签: android

我正在制作带有两个edittext的货币转换器,我打算从edittext视图获取输入并在第二个edittext中显示它。我想知道如何能够做到这一点。

2 个答案:

答案 0 :(得分:1)

使用按钮从EditText1获取值并为EditText2设置值

    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
           //Any Modification/process u need to do with the input can be done here
            editText2.setText(editText1.getText().toString()); 
        }
    });

答案 1 :(得分:1)

public class TestEditTextListener extends AppCompatActivity {

    private EditText firstEditText;
    private EditText secondEditText;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.test);
        firstEditText = (EditText)findViewById(R.id.firstEditText);
        secondEditText = (EditText)findViewById(R.id.secondEditText);


        firstEditText.addTextChangedListener(new EditTextListener());

    }

    private class EditTextListener implements 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) {

            secondEditText.setText(firstEditText.getText());
        }

        @Override
        public void afterTextChanged(Editable s) {
        }
    }
}