开发者

How to set the value in EditText field?

I have three EditText, I want to concatinate the 开发者_开发知识库strings present in the first two EditText fields,and display in the third EditText field.After entering the string at 2nd field,it automatically concatinate and set in the third 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());

I wrote the above code.But I result is not shomn in the third EditText. Please give the solution, that I implement in my program


This should work. Make sure you do not edit text2 in the TextChanged listener because then afterTextChanged would get called again.

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());
    };
});


If you want to detect when your two EditText fields change, you're going to need to use addTextChangedListener() on each of them. The following can go in your onCreate() method:

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);


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);
    }
}


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);
        }
    });}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜