Как установить значение в поле EditText?
У меня есть три EditText, я хочу конкатенировать строки, присутствующие в первых двух полях EditText, и отображать в третьем поле EditText. После ввода строки во 2-ом поле это
автоматически конкатенировать и устанавливать в третьем EditText.
EditText text1 = (EditText) findViewById(R.id.text1);
mtext1=text1.getText.toString();
EditText text2 = (EditText) findViewById(R.id.text2);
mtext2 = text2.getText.toString();
mtext3=mtext1.concat().mtext2;
Edit text3 = (EditText) findViewById(R.id.text3);
text3 = setText(mtext3.toString());
Я написал приведенный выше код. Но результат не является shomn в третьем EditText.
Пожалуйста, дайте решение, которое я реализую в своей программе
Ответы
Ответ 1
Это должно сработать. Убедитесь, что вы не редактируете text2 в прослушивателе TextChanged, так как afterTextChanged снова будет вызван.
final EditText text1 = (EditText) findViewById(R.id.text1);
final EditText text2 = (EditText) findViewById(R.id.text2);
final EditText text3 = (EditText) findViewById(R.id.text3);
text2.addTextChangedListener(new TextWatcher() {
void afterTextChanged(Editable s) {
text3.setText(text1.getText().toString() + text2.getText().toString());
};
});
Ответ 2
Если вы хотите определить, когда изменяются ваши два поля EditText, вам нужно будет использовать addTextChangedListener() для каждого из них. В вашем методе onCreate() может идти следующее:
final EditText text1 = (EditText) findViewById(R.id.text1);
final EditText text2 = (EditText) findViewById(R.id.text2);
final EditText text3 = (EditText) findViewById(R.id.text3);
TextWatcher watcher = new TextWatcher() {
void afterTextChanged(Editable s) {
text3.setText(text1.getText() + text2.getText());
};
});
text1.addTextChangedListener(watcher);
text2.addTextChangedListener(watcher);
Ответ 3
package com.tiru;
import android.app.Activity;
import android.os.Bundle;
import android.widget.EditText;
public class SetEditText extends Activity {
private String mtext1 = null;
private String mtext2 = null;
private String mtext3 = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
EditText text1 = (EditText) findViewById(R.id.text1);
EditText text2 = (EditText) findViewById(R.id.text2);
mtext1 = text1.getText();
mtext2 = text2.getText();
mtext3 = mtext1 + mtext2;
EditText text3 = (EditText) findViewById(R.id.text3);
text3.setText(mtext3);
}
}
Ответ 4
EditText inputWeight,inputHeight,outputResult;
Button calculate;
float num1,num2,out;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_bmi);
inputWeight = (EditText) findViewById(R.id.weight);
inputHeight = (EditText) findViewById(R.id.height);
outputResult = (EditText) findViewById(R.id.result);
calculate = (Button) findViewById(R.id.cal);
calculate.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
num1 = Float.parseFloat(inputWeight.getText().toString());
num2 = Float.parseFloat(inputHeight.getText().toString());
out=num1/(num2*num2);
calculate.setText(" "+out);
}
});}