我正在创建一个我想自动进行计算的应用程序。
数字 1和2 EditText ,数字 3 是 TextView 。 当我填写数字 1和2 时,我希望自动总结并在 TextView 中显示结果。在这个例子中1 + 2 = 3
如何为此
设置活动我做了一个xml示例:
答案 0 :(得分:2)
这是add_two_numbers.xml布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<LinearLayout android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView android:text="First Number : "
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<EditText android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView android:text="Second Number : "
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<EditText android:id="@+id/editText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout android:orientation="horizontal"
android:layout_gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView android:text="Result"
android:id="@+id/textView_result"
android:textColor="#FF00FF"
android:textSize="18dip"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
</LinearLayout>
这是一个Activity AddTwoNumbers.java
public class AddTwoNumbers extends Activity {
EditText editText1;
EditText editText2;
TextView textViewResult;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.add_two_numbers);
editText1 = (EditText) findViewById(R.id.editText1);
editText2 = (EditText) findViewById(R.id.editText2);
textViewResult = (TextView) findViewById(R.id.textView_result);
editText1.addTextChangedListener(new TextWatcher() {
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub
}
public void onTextChanged(CharSequence s, int start, int before,
int count) {
textViewResult.setText(addNumbers());
}
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
}
});
editText2.addTextChangedListener(new TextWatcher() {
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub
}
public void onTextChanged(CharSequence s, int start, int before,
int count) {
textViewResult.setText(addNumbers());
}
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
}
});
}
private String addNumbers() {
int number1;
int number2;
if(editText1.getText().toString() != "" && editText1.getText().length() > 0) {
number1 = Integer.parseInt(editText1.getText().toString());
} else {
number1 = 0;
}
if(editText2.getText().toString() != "" && editText2.getText().length() > 0) {
number2 = Integer.parseInt(editText2.getText().toString());
} else {
number2 = 0;
}
return Integer.toString(number1 + number2);
}
}
我已在2.3平台上测试过它。它工作正常。
答案 1 :(得分:0)
您可能需要为每个EditTexts提供一个键监听器,这样在每个键事件之后,您可以刷新字段的值并显示新的计算。
答案 2 :(得分:0)
您需要添加两个TextView并为其注册更改为listener的文本。每当文本发生变化时,您都可以验证输入,进行计算并更新显示。
答案 3 :(得分:0)
如果您想自动引发任何事件,则需要向该视图组件添加适当的侦听器
edittext1.addTextChangedListener(new TextWatcher(){
public void afterTextChanged(
Editable arg0) {
// TODO Auto-generated method stub
}
public void beforeTextChanged(
CharSequence s, int start,
int count, int after) {
// TODO Auto-generated method stub
}
public void onTextChanged(
CharSequence s, int start,
int before, int count) {
// TODO Auto-generated method stub
}
});
edittext2.addTextChangedListener(new TextWatcher(){
public void afterTextChanged(
Editable arg0) {
// TODO Auto-generated method stub
}
public void beforeTextChanged(
CharSequence s, int start,
int count, int after) {
// TODO Auto-generated method stub
}
public void onTextChanged(
CharSequence s, int start,
int before, int count) {
// TODO Auto-generated method stub
}
});