我正在制作一个要自动进行计算的应用程序.
数字1和2是EditText,数字3是TextVIEw.
当我填写数字1和2时,我希望它自动汇总并在TextVIEw中显示结果.在这个例子中1 2 = 3
如何为此设置活动
我做了一个xml示例:
解决方法:
这是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>
这是一个活动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平台上对其进行了测试.一切正常.
总结以上是内存溢出为你收集整理的android-自动计算全部内容,希望文章能够帮你解决android-自动计算所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)