search
HomeCommon Problemandroid classic simple small project

android classic simple small project

Jul 30, 2019 pm 01:39 PM
android

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function