Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android-CheckBox 實現計算器

Android-CheckBox 實現計算器

編輯:關於Android編程

源代碼下載地址:http://download.csdn.net/detail/wu20093346/7718055

使用CheckBox的OnCheckedChangeListener做事件觸發,效果圖:

\

<喎?/kf/ware/vc/" target="_blank" class="keylink">vcD4KPHByZSBjbGFzcz0="brush:java;"> list=new OnKeyListener() { @Override public boolean onKey(View v, int keyCode, KeyEvent event) { // TODO Auto-generated method stub if(mBox1.isChecked()) { mBox1.setChecked(false); } if(mBox2.isChecked()) { mBox2.setChecked(false); } if(mBox3.isChecked()) { mBox3.setChecked(false); } if(mBox4.isChecked()) { mBox4.setChecked(false); } return false; } }; mEditText.setOnKeyListener(list); mEditText1.setOnKeyListener(list);如代碼所寫,每當用戶重新輸入數字後,就會激發文本框的onKey事件,讓所有的CheckBox變成不選中狀態。

 listner=new OnCheckedChangeListener() {

 @Override
 public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
 // TODO Auto-generated method stub
 switch (buttonView.getId()) {
 case R.id.Plus:
 if(!isEmpty(mEditText,mEditText1))
 {
 Confirm();
 mBox1.setChecked(false);
 return;
 }
 break;
 case R.id.Cut:
 if(!isEmpty(mEditText,mEditText1))
 {
 Confirm();
 mBox2.setChecked(false);
 return;
 }
 break;
 case R.id.Ride:
 if(!isEmpty(mEditText,mEditText1))
 {
 Confirm();
 mBox3.setChecked(false);
 return;
 }
 break;
 case R.id.Except:
 if(!isEmpty(mEditText,mEditText1))
 {
 Confirm();
 mBox4.setChecked(false);
 return;
 }
 break;
 default:
 break;
 }

 if(mBox1.isChecked())
 {
 mTextView.setText( GetOperation("+"));
 }
 else
 {
 mTextView.setText("");
 }
 if(mBox2.isChecked())
{
mTextView2.setText(GetOperation("-"));
 }
 else
{
 mTextView2.setText("");
 }
 if(mBox3.isChecked())
 {
 mTextView3.setText(GetOperation("*"));
 }
 else
 {
 mTextView3.setText("");
 }
 if(mBox4.isChecked())
 {
 mTextView4.setText(GetOperation("/"));
 }
 else
 {
 mTextView4.setText("");
 }
 }
 };
 mBox1.setOnCheckedChangeListener(listner);
 mBox2.setOnCheckedChangeListener(listner);
 mBox3.setOnCheckedChangeListener(listner);
 mBox4.setOnCheckedChangeListener(listner);
根據選擇的checkbox返回計算結果,如果uncheck將清空文本,選擇checkbox之前必須要輸入兩個數字。
全部代碼:

 package cn.terry;

import android.app.Activity;
 import android.app.AlertDialog;
 import android.content.DialogInterface; 
 import android.os.Bundle;  
 import android.view.KeyEvent;
 import android.view.View;
 import android.view.View.OnKeyListener;
 import android.widget.*;
 import android.widget.CompoundButton.OnCheckedChangeListener;
 public class CheckBoxCalc extends Activity {
 private TextView mTextView;
 private TextView mTextView2;
 private TextView mTextView3;
 private TextView mTextView4;
 private CheckBox mBox1;
 private CheckBox mBox2;
 private CheckBox mBox3;
 private CheckBox mBox4;
 private EditText mEditText;
 private EditText mEditText1;
 private boolean isbool=true;
 private OnCheckedChangeListener listner; 
 private Float Temp;
 private String Experssion;
 private OnKeyListener list;
 /** Called when the activity is first created. */
 @Override
 public void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.main);
 mTextView=(TextView)findViewById(R.id.result1);
 mTextView2=(TextView)findViewById(R.id.result2);
 mTextView3=(TextView)findViewById(R.id.result3);
 mTextView4=(TextView)findViewById(R.id.result4);
 mBox1=(CheckBox)findViewById(R.id.Plus);  //加減乘除
 mBox2=(CheckBox)findViewById(R.id.Cut);
 mBox3=(CheckBox)findViewById(R.id.Ride);
 mBox4=(CheckBox)findViewById(R.id.Except);
 mEditText=(EditText)findViewById(R.id.first);
mEditText1=(EditText)findViewById(R.id.second);
 list=new OnKeyListener() {
 
 @Override
 public boolean onKey(View v, int keyCode, KeyEvent event) {
 // TODO Auto-generated method stub 
 if(mBox1.isChecked())
 {
 mBox1.setChecked(false);
 } 
if(mBox2.isChecked())
 {
 mBox2.setChecked(false);
 } 
 if(mBox3.isChecked())
 {
 mBox3.setChecked(false);
} 
 if(mBox4.isChecked())
 {
 mBox4.setChecked(false);
 }

 return false;
 }
 };
 mEditText.setOnKeyListener(list);
mEditText1.setOnKeyListener(list);
 
 listner=new OnCheckedChangeListener() {
 
 @Override
 public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
 // TODO Auto-generated method stub
 switch (buttonView.getId()) {
 case R.id.Plus:
 if(!isEmpty(mEditText,mEditText1))
 {
 Confirm();
 mBox1.setChecked(false);
 return;
 }
 break;
 case R.id.Cut:
 if(!isEmpty(mEditText,mEditText1))
 {
 Confirm();
 mBox2.setChecked(false);
 return;
 }
 break;
 case R.id.Ride:
if(!isEmpty(mEditText,mEditText1))
 {
 Confirm();
 mBox3.setChecked(false);
 return;
 }
 break;
 case R.id.Except:
 if(!isEmpty(mEditText,mEditText1))
 {
 Confirm();
 mBox4.setChecked(false);
 return;
 }
 break;
 default:
 break;
 }
 
 if(mBox1.isChecked())
 {
 mTextView.setText( GetOperation("+"));
 }
 else
 {
 mTextView.setText("");
 }
 if(mBox2.isChecked())
 {
 mTextView2.setText(GetOperation("-"));
 }
 else
 {
 mTextView2.setText("");
 }
 if(mBox3.isChecked())
 {
 mTextView3.setText(GetOperation("*"));
 }
 else
 {
 mTextView3.setText("");
 }
 if(mBox4.isChecked())
 {
 mTextView4.setText(GetOperation("/"));
 }
 else
 {
 mTextView4.setText("");
 }
 }
 };
 mBox1.setOnCheckedChangeListener(listner);
 mBox2.setOnCheckedChangeListener(listner);
 mBox3.setOnCheckedChangeListener(listner);
mBox4.setOnCheckedChangeListener(listner);
 
 }
 public String GetOperation(String Operation)
{
 if(Operation=="+")
 {
 Temp=Float.parseFloat(mEditText.getText().toString())+
 Float.parseFloat(mEditText1.getText().toString());

 }
if(Operation=="-")
{
 Temp=Float.parseFloat(mEditText.getText().toString())-
 Float.parseFloat(mEditText1.getText().toString());
 
 }
 if(Operation=="*")
 {
 Temp=Float.parseFloat(mEditText.getText().toString())*
 Float.parseFloat(mEditText1.getText().toString());
 }
 if(Operation=="/")
 {
 Temp=Float.parseFloat(mEditText.getText().toString())/
 Float.parseFloat(mEditText1.getText().toString());
 }
 Experssion=mEditText.getText().toString()+Operation+mEditText1.getText().toString()
 +"="+Temp.toString();
 return Experssion;
 }

 
public void Confirm()
{ 
 new AlertDialog.Builder(CheckBoxCalc.this)
 .setTitle("alert")
 .setMessage("can not be null")
 .setPositiveButton("OK", new DialogInterface.OnClickListener() {
 
 @Override
 public void onClick(DialogInterface dialog, int which) {
 // TODO Auto-generated method stub

}
 })
 .create().show();  
 }
 public boolean isEmpty(EditText e,EditText a)
{
     if(e.getText().toString().length()>0 && a.getText().toString().length()>0)
     {
         isbool= true;
     }
     else
     {
         isbool=false;
     }
 return isbool;
 } }

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved