Android-CheckBox implementation calculator, androidcheckbox

Source: Internet
Author: User

Android-CheckBox implementation calculator, androidcheckbox

Source code: http://download.csdn.net/detail/wu20093346/7718055

Use the OnCheckedChangeListener of the CheckBox to trigger the event ,:


 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);
As written in the Code, every time a user enters a number again, the onKey event of the text box is triggered to make all the checkboxes unselected.

 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);
Return the calculation result based on the selected checkbox. If uncheck clears the text, you must enter two numbers before selecting the checkbox.
All code:

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; p Rivate 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); // Add Trim multiply mBox2 = (CheckBox) findViewById (R. id. cut); mBox3 = (CheckBox) findViewById (R. id. ride); mBox4 = (CheckBox) findViewById (R. id. before t); 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); listener (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. Else T: 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); listener (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 &. getText (). toString (). length ()> 0) {isbool = true;} else {isbool = false;} return isbool ;}}


Android implements layout and calculator addition operations

Gridlayout is a good implementation, but this value supports Versions later than 4.0. There are a lot of examples on the Internet. You can check it out. Linearlayout can also be used, but it is complicated.
 
How does android use EditText to display all user input, such as 3 + 2-8 ??

StringBuffer sb = new StringBuffer ();
Sb. append ("3 ");
Sb. append ("+ ");
Sb. append ("2 ");
Sb. append ("-");
Sb. append ("8 ");
EditText. setText (sb. toString ());

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.