TextWatcher watch = new TextWatcher(){
@Override
public void afterTextChanged(Editable arg0) {
// TODO Auto-generated method stub
}
@Override
public void beforeTextChanged(CharSequence a, int arg1,
int arg2, int arg3) {
// TODO Auto-generated method stub
EditText et1 = (EditText)findViewById(arg1);
final int before = Integer.parseInt(et1.getText().toString());
}
@Override
public void onTextChanged(CharSequence a, int arg1,
int arg2, int arg3) {
// TODO Auto-generated method stub
int id = 1000 + arg1;
EditText et2 = (EditText)findViewById(id);
int x = Integer.parseInt(a.toString());
int y = Integer.parseInt(et2.getText().toString());
int sum = (x / before) * y;
}
我想根据编辑文本的上一个条目自动更新编辑文本,并将其显示在另一个编辑文本上。
例如,
我有两个编辑文字。一个用于calories
,另一个用于grams
。
如果我更改calories
的值,则grams
会相应更改 -
x是变化前的初始卡路里(在编辑文本1中), y是变化的卡路里(在编辑文本1中), z为克的值(在编辑文本2中),
等式为:y/x * z
答案 0 :(得分:0)
我认为这有助于你
package com.example.testandroid;
import android.app.Activity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.Menu;
import android.widget.EditText;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
EditText e1 = (EditText) findViewById(R.id.e1);
final EditText e2 = (EditText) findViewById(R.id.e2);
e1.addTextChangedListener(new TextWatcher() {
@Override
public void afterTextChanged(Editable s) {
String value = s.toString();
e2.setText(value);
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}
@Override
public void onTextChanged(final CharSequence s, int start,
int before, int count) {
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
答案 1 :(得分:0)
EditText ed1 = (EditText) findViewById(R.id.ed1);
ed1.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
// TODO Auto-generated method stub
}
@Override
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
int arg3) {
// TODO Auto-generated method stub
}
@Override
public void afterTextChanged(Editable ed) {
EditText ed2 = (EditText) findViewById(R.id.ed2);
ed2.setText(ed.toString());
}
});