Home  >  Article  >  android classic simple small project

android classic simple small project

angryTom
angryTomOriginal
2019-07-30 13:39:589884browse

android classic simple small project

If you want to know more about Android, you can click: Android Manual

This small project not only requires us to make use of button groups, but also demonstrates how to use the same listener to handle different events and then make corresponding changes to the TextView text. Enough nonsense.

The most direct way to handle this project is to set up a listening event for each button and then handle each event, that's right. This method is feasible, but is the code we see in the end very inelegant, with many repeated operations? So we thought of using a listening event to handle different events. So what to do? Follow me and do it together. The approximate code is as follows: declare a click listening event.

private OnClickListener listener;
listener= new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.Button01:
if(isEmpty(mEditText1,mEditText2)==false)
{
Confirm().show();
return;
}
mTextView1.setText("+");
StrTemp=Float.toString((Float.parseFloat(mEditText1.getText().toString())+
(Float.parseFloat(mEditText2.getText().toString()))));
mTextView2.setText(StrTemp);
break;
case R.id.Button02:
if(isEmpty(mEditText1,mEditText2)==false)
{
Confirm().show();
return;
}
mTextView1.setText("-");
StrTemp=Float.toString((Float.parseFloat(mEditText1.getText().toString())-
(Float.parseFloat(mEditText2.getText().toString()))));
mTextView2.setText(StrTemp);
break;
case R.id.Button03:
if(isEmpty(mEditText1,mEditText2)==false)
{
Confirm().show();
return;
}
mTextView1.setText("*");
StrTemp=Float.toString((Float.parseFloat(mEditText1.getText().toString())*
(Float.parseFloat(mEditText2.getText().toString()))));
mTextView2.setText(StrTemp);
break;
case R.id.Button04: 
if(isEmpty(mEditText1,mEditText2)==false)
{
Confirm().show();
return;
}
mTextView1.setText("/"); 
StrTemp=Float.toString((Float.parseFloat(mEditText1.getText().toString())/
(Float.parseFloat(mEditText2.getText().toString()))));
mTextView2.setText(StrTemp);
break;
default:
break;
}
}
};

The code is simple and clear. You can get the corresponding button through the getid() method and process it later. OK The code is roughly like this, and then set the click event of the button

mButton1.setOnClickListener(listener);
        mButton2.setOnClickListener(listener);
        mButton3.setOnClickListener(listener);
        mButton4.setOnClickListener(listener);

The code also encapsulates the pop-up box code as follows:

public AlertDialog Confirm()
{
alert=new AlertDialog.Builder(SimpleCalc.this)
.setTitle(R.string.confirm)
.setMessage(R.string.content)
.setPositiveButton(R.string.OK, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}
})
.create();
return alert;
}

So far a simple calculator has been created Done, next time we will put all the buttons on the screen and use the event delivery method to complete the calculation by clicking the button numbers. I believe friends who have read this article will know how to do it.

The above is the detailed content of android classic simple small project. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:How to use pageofficeNext article:How to use pageoffice