初学Android移动开发,先从一个简单的项目开始——简易计算器。
如果你在Android studio配置方面有什么错误,可以参考我的其他博客。这里只是写一下我作为一个初学者的开发笔记,总结自己的得失,为以后的开发打基础。对你有帮助的地方请自取。
目录
前言:
开发过程中的笔记:
代码目录结构:
源代码:
private EditText firstText;
private EditText secondText;
private TextView calculateTypeText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
firstText = (EditText) this.findViewById(R.id.firstNumber);
secondText = (EditText)this.findViewById(R.id.secondNumber);
calculateTypeText = (TextView) this.findViewById(R.id.calculateTypeText);
}
对于伸手党,最难过的莫过于只有代码,没有一个具体的大纲一样的目录结构。所以,习惯伸手的我,给习惯伸手的你一个便利。
MainActivity.java:
package com.felix.felix.pro_calculator;
import android.content.Intent;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import org.w3c.dom.Text;
public class MainActivity extends AppCompatActivity {
private EditText firstText;
private EditText secondText;
private TextView calculateTypeText;
private int firstNumber = 0;
private int secondNumber =0;
private Button chooseButton;
private Button calculateButton;
private int requestCode= 0;
private int calculationType= -1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
firstText = (EditText) this.findViewById(R.id.firstNumber);
secondText = (EditText)this.findViewById(R.id.secondNumber);
calculateTypeText = (TextView) this.findViewById(R.id.calculateTypeText);
chooseButton =(Button)this.findViewById(R.id.chooseButton);
calculateButton=(Button)this.findViewById(R.id.calculateButton);
//定义一个点击事件,点击跳转界面
chooseButton.setOnClickListener(new ChooseButtonListener());
calculateButton.setOnClickListener(new CalculateButtonListener());
}
//对于点击事件的类和函数复写
class ChooseButtonListener implements View.OnClickListener{
@Override
public void onClick(View arg0) {
//跳转界面的具体实现
Intent intent = new Intent(MainActivity.this,CalculationTypeActivity.class);
//带着其它界面的信息返回本界面。requestCode 是本界面的标识
MainActivity.this.startActivityForResult(intent,requestCode);
}
}
class CalculateButtonListener implements View.OnClickListener{
@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this,ResultActivity.class);
intent.putExtra("firstNumber",firstText.getText().toString());
intent.putExtra("secondNumber",secondText.getText().toString());
//计算结果
intent.putExtra("calculationType",calculationType);
int result =0;
firstNumber = Integer.parseInt(firstText.getText().toString());
secondNumber = Integer.parseInt(secondText.getText().toString());
switch (calculationType){
case 1:
result = firstNumber + secondNumber;
break;
case 2:
result = firstNumber - secondNumber;
break;
case 3:
result = firstNumber * secondNumber;
break;
case 4:
result = firstNumber / secondNumber;
break;
default:
result = 1;
break;
}
//带着这个界面的信息去其他界面
intent.putExtra("result",result);
startActivity(intent);
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(this.requestCode==requestCode&& CalculationTypeActivity.resultCode==resultCode){
Bundle bundle = data.getExtras();
calculationType = data.getIntExtra("type1",-1);
System.out.println("calculationType:" + calculationType);
calculateTypeText.setText(ConstantInfo.typeMap.get(calculationType));
}
}
}
activity_main.xml
CalculationTypeActivity.java
package com.felix.felix.pro_calculator;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
public class CalculationTypeActivity extends AppCompatActivity {
private RadioGroup group;
private RadioButton add;
private RadioButton minus;
private RadioButton multiply;
private RadioButton divider;
private Button okBtn;
private int calculationType = -1;
protected static final int resultCode = 0x321;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_calculation_type);
group = (RadioGroup)this.findViewById(R.id.group);
add = (RadioButton)this.findViewById(R.id.add);
minus = (RadioButton)this.findViewById(R.id.minus);
multiply = (RadioButton)this.findViewById(R.id.multiply);
divider = (RadioButton)this.findViewById(R.id.divider);
okBtn = (Button)this.findViewById(R.id.ok);
group.setOnCheckedChangeListener(new GroupSelectionListener());
okBtn.setOnClickListener(new okLstener());
}
class GroupSelectionListener implements RadioGroup.OnCheckedChangeListener{
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
//选择的是哪一个按钮
int radioButtonId = group.getCheckedRadioButtonId();
RadioButton btn = (RadioButton)CalculationTypeActivity.this.findViewById(radioButtonId);
//为啥不用switch?
if(btn.getId()==add.getId()){
calculationType=1;
}else if(btn.getId()==minus.getId()){
calculationType=2;
}else if(btn.getId()==multiply.getId()){
calculationType=3;
}else if(btn.getId()==divider.getId()){
calculationType=4;
}
System.out.println("current value:" + calculationType);
}
}
//处理点击时间按钮,并且把信息返回到MainActivity中
class okLstener implements View.OnClickListener{
@Override
public void onClick(View v) {
Intent intent = getIntent();
intent.putExtra("type1",calculationType);
Bundle bundle = new Bundle();
bundle.putInt("type",calculationType);
intent.putExtras(bundle);
//把这个activity的值回传回去
CalculationTypeActivity.this.setResult(resultCode,intent);
//把这个界面消掉,让MainActivity出现
CalculationTypeActivity.this.finish();
}
}
}
activity_calculation_type.xml
ResultActivity.java
package com.felix.felix.pro_calculator;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class ResultActivity extends AppCompatActivity {
private TextView resultText;
private Button back;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_result);
resultText = (TextView)this.findViewById(R.id.resultText);
back = (Button)this.findViewById(R.id.back);
back.setOnClickListener(new BackListener());
Intent intent = this.getIntent();
StringBuilder builder = new StringBuilder();
builder.append(intent.getStringExtra("firstNumber"))
.append(" ")
.append(ConstantInfo.infoMap.get(intent.getIntExtra("calculationType",0)))
.append(" ")
.append(intent.getStringExtra("secondNumber"))
.append(" = ")
.append(intent.getIntExtra("result",-1));
//把结果存入resultText中
resultText.setText(builder.toString());
}
class BackListener implements View.OnClickListener{
@Override
public void onClick(View v) {
Intent intent = new Intent(ResultActivity.this,MainActivity.class);
startActivity(intent);
ResultActivity.this.finish();
}
}
}
activity_result.xml
ConstantInfo.java
package com.felix.felix.pro_calculator;
import java.util.HashMap;
import java.util.Map;
/*
Created by Felix on 2018/11/28 21:01
*/
public class ConstantInfo {
public static final String add = "加法";
public static final String minus = "减法";
public static final String multiply = "乘法";
public static final String divider = "除法";
public static final Map typeMap = new HashMap();
public static final Map infoMap = new HashMap();
static {
typeMap.put(1,add);
typeMap.put(2,minus);
typeMap.put(3,multiply);
typeMap.put(4,divider);
infoMap.put(1,"+");
infoMap.put(2,"-");
infoMap.put(3,"*");
infoMap.put(4,"/");
}
}
转载注明出处。
指教请联系:邮箱 [email protected] QQ:1311665691