我有三个EditText,我想要连接前两个EditText字段中的字符串,并显示在第三个EditText字段中。在第二个字段输入字符串后,它 自动连接并设置在第三个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());
我写了上面的代码。但是我在第三个EditText中没有结果。 请提供我在我的程序中实施的解决方案
答案 0 :(得分:23)
这应该有效。确保你不在TextChanged监听器中编辑text2,因为这样会再次调用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());
};
});
答案 1 :(得分:6)
如果要检测两个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);
答案 2 :(得分:1)
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);
}
}
答案 3 :(得分:0)
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);
}
});}