第一个上手的安卓小项目,包含两个界面:login和calculator。
功能比较简单粗糙,目的主要是熟悉android基本结构与操作。
环境
- Android Studio + Genymotion
演示
详解
布局layout文件之login.xml
布局layout文件之activity_main.xml
drawable文件中buttonstyle.xml
-
-
drawable文件中circle.xml
values文件中colors.xml
#008577
#00574B
#D81B60
#C0C0C0
#C75A
values文件中strings.xml
mycalculator
values文件中styles.xml
Login.java
package com.example.mycalculator;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.app.Activity;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
public class Login extends AppCompatActivity{
private EditText et_username;
private EditText et_password;
private Button bt_log;
private Button bt_bos;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.login);
//透明状态栏
getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
//通过资源标识获得控件实例
et_username = (EditText) findViewById(R.id.username);
et_password = (EditText) findViewById(R.id.password);
bt_log = (Button) findViewById(R.id.login);
bt_bos = (Button) findViewById(R.id.cancel);
//给登录按钮注册监听器,实现监听器接口,编写事件
bt_log.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//获取用户输入的数据
String strUsername = et_username.getText().toString();
String strPassword = et_password.getText().toString();
//判断用户名和密码是否正确(为可以进行测试,将用户名和密码都定义为admin)
if(strUsername.equals("admin") && strPassword.equals("admin")){
Toast.makeText(Login.this,"用户名和密码正确!",Toast.LENGTH_SHORT).show();
//创建一个Intent的实例
Intent intent = new Intent();
//设置从哪个activity启动哪个activity
intent.setClass(Login.this, MainActivity.class);
//启动另一个activity
startActivity(intent);
}else {
Toast.makeText(Login.this,"用户名或密码错误!",Toast.LENGTH_SHORT).show();
}
}
});
//给取消按钮注册监听器,实现监听器接口,编写事件
bt_bos.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
finish();
}
});
}
}
MainActivity.java
package com.example.mycalculator;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
Button zero;
Button one;
Button two;
Button three;
Button four;
Button five;
Button six;
Button seven;
Button eight;
Button nine;
Button point;
Button clear;
Button add;
Button substraction;
Button mulitipliction;
Button division;
Button root;
Button surplus;
Button equal;
Button delete;
TextView theFirstView;
TextView theOptionView;
TextView theSecondView;
TextView theEqualView;
TextView theAnswerView;
String option = "";
String str1 = "", str2 = "";
double number1, number2;
int answerFlag = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView(); //初始化布局
}
public void onClick(View view) {
switch (view.getId()) {
case R.id.zero:
case R.id.one:
case R.id.two:
case R.id.three:
case R.id.four:
case R.id.five:
case R.id.six:
case R.id.seven:
case R.id.eight:
case R.id.nine:
if (answerFlag == 0) {
if (option == "") {
str1 = str1 + ((Button) view).getText().toString();
theFirstView.setText(str1);
} else {
str2 = str2 + ((Button) view).getText().toString();
theSecondView.setText(str2);
}
}
if (answerFlag == 1) {
clear();
answerFlag = 0;
str1 = str1 + ((Button) view).getText().toString();
theFirstView.setText(str1);
}
break;
case R.id.point:
if (str1 != "" && option == "") {
str1 = str1 + ((Button) view).getText().toString();
theFirstView.setText(str1);
}
if (option != "" && str2 != "") {
str2 = str2 + ((Button) view).getText().toString();
theSecondView.setText(str2);
} else {
//什么都不执行
}
break;
case R.id.add:
option = "+";
theOptionView.setText("+");
break;
case R.id.substraction:
option = "-";
theOptionView.setText("-");
break;
case R.id.mulitipliction:
option = "×";
theOptionView.setText("×");
break;
case R.id.division:
option = "÷";
theOptionView.setText("÷");
break;
case R.id.surplus:
option = "%";
theOptionView.setText("%");
break;
case R.id.root:
if (str1 == "") {
option = "√";
theOptionView.setText("√");
}
break;
case R.id.clear:
clear();
break;
case R.id.delete:
if (theAnswerView.getText().toString() == "") {
if ((theSecondView.getText().toString()) != "") {
int length = str2.length();
if (length == 1) {
theSecondView.setText("");
} else {
str2 = str2.substring(0, length - 1);
theSecondView.setText(str2);
}
break;
}
if ((theSecondView.getText().toString()) == "" && (theOptionView.getText().toString()) != "") {
theOptionView.setText("");
break;
}
if ((theSecondView.getText().toString()) == "" && (theAnswerView.getText().toString()) == "" && (theFirstView.getText().toString()) != "") {
int length = str1.length();
if (length == 1) {
theFirstView.setText("");
} else {
str1 = str1.substring(0, length - 1);
theFirstView.setText(str1);
}
break;
}
}
break;
case R.id.equal:
theEqualView.setText("=");
answerFlag = 1;
if (option == "+") {
number1 = Double.parseDouble(theFirstView.getText().toString());
number2 = Double.parseDouble(theSecondView.getText().toString());
theAnswerView.setText((number1 + number2) + "");
}
if (option == "-") {
number1 = Double.parseDouble(theFirstView.getText().toString());
number2 = Double.parseDouble(theSecondView.getText().toString());
theAnswerView.setText((number1 - number2) + "");
}
if (option == "×") {
number1 = Double.parseDouble(theFirstView.getText().toString());
number2 = Double.parseDouble(theSecondView.getText().toString());
theAnswerView.setText((number1 * number2) + "");
}
if (option == "÷") {
number1 = Double.parseDouble(theFirstView.getText().toString());
number2 = Double.parseDouble(theSecondView.getText().toString());
if (number2 == 0) {
theAnswerView.setText("错误");
} else {
theAnswerView.setText((number1 / number2) + "");
}
}
if (option == "√") {
number2 = Double.parseDouble(theSecondView.getText().toString());
theAnswerView.setText(Math.sqrt(number2) + "");
}
if (option == "%") {
number1 = Double.parseDouble(theFirstView.getText().toString());
number2 = Double.parseDouble(theSecondView.getText().toString());
theAnswerView.setText(number1 % number2 + "");
}
break;
}
}
public void initView() {
zero = (Button) findViewById(R.id.zero);
one = (Button) findViewById(R.id.one);
two = (Button) findViewById(R.id.two);
three = (Button) findViewById(R.id.three);
four = (Button) findViewById(R.id.four);
five = (Button) findViewById(R.id.five);
six = (Button) findViewById(R.id.six);
seven = (Button) findViewById(R.id.seven);
eight = (Button) findViewById(R.id.eight);
nine = (Button) findViewById(R.id.nine);
point = (Button) findViewById(R.id.point);
clear = (Button) findViewById(R.id.clear);
add = (Button) findViewById(R.id.add);
substraction = (Button) findViewById(R.id.substraction);
mulitipliction = (Button) findViewById(R.id.mulitipliction);
division = (Button) findViewById(R.id.division);
root = (Button) findViewById(R.id.root);
surplus = (Button) findViewById(R.id.surplus);
equal = (Button) findViewById(R.id.equal);
delete = (Button) findViewById(R.id.delete);
theFirstView = (TextView) findViewById(R.id.the_first_number);
theOptionView = (TextView) findViewById(R.id.the_option);
theSecondView = (TextView) findViewById(R.id.the_second_number);
theEqualView = (TextView) findViewById(R.id.the_equal);
theAnswerView = (TextView) findViewById(R.id.the_answer);
zero.setOnClickListener(this);
one.setOnClickListener(this);
two.setOnClickListener(this);
three.setOnClickListener(this);
four.setOnClickListener(this);
five.setOnClickListener(this);
six.setOnClickListener(this);
seven.setOnClickListener(this);
eight.setOnClickListener(this);
nine.setOnClickListener(this);
point.setOnClickListener(this);
clear.setOnClickListener(this);
add.setOnClickListener(this);
substraction.setOnClickListener(this);
mulitipliction.setOnClickListener(this);
division.setOnClickListener(this);
root.setOnClickListener(this);
surplus.setOnClickListener(this);
equal.setOnClickListener(this);
delete.setOnClickListener(this);
theFirstView.setOnClickListener(this);
theOptionView.setOnClickListener(this);
theEqualView.setOnClickListener(this);
theSecondView.setOnClickListener(this);
theAnswerView.setOnClickListener(this);
}
public void clear() {
str1 = "";
str2 = "";
theFirstView.setText("");
theSecondView.setText("");
theOptionView.setText("");
theEqualView.setText("");
theAnswerView.setText("");
option = "";
}
}
Manifests文件中AndroidManifest.xml