第一个上手的安卓小项目,包含两个界面:login和calculator。
功能比较简单粗糙,目的主要是熟悉android基本结构与操作。
环境
- Android Studio + Genymotion
演示
详解
布局layout文件之login.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.mycalculator.Login">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="@drawable/bg"
android:gravity="center">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingBottom="30dp"
android:paddingTop="20dp"
android:background="#99404348"
android:gravity="center">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Welcome"
android:textColor="#FFFFFF"
android:textSize="18sp"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingBottom="20dp"
android:paddingTop="20dp"
android:background="#99000000">
<EditText
android:id="@+id/username"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@null"
android:layout_gravity="center"
android:layout_marginRight="50dp"
android:layout_marginLeft="50dp"
android:textColor="#9F9FA0"
android:textColorHint="#9F9FA0"
android:hint="your username"/>
<View
android:layout_width="match_parent"
android:layout_height="1dip"
android:background="#83738F"
android:layout_marginRight="40dp"
android:layout_marginLeft="40dp"
android:layout_marginTop="10dp"
android:layout_marginBottom="5dp"></View>
<EditText
android:id="@+id/password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@null"
android:layout_gravity="center"
android:inputType="textPassword"
android:hint="your password"
android:textColor="#9F9FA0"
android:textColorHint="#9F9FA0"
android:layout_marginLeft="50dp"
android:layout_marginRight="50dp"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingBottom="20dp"
android:paddingTop="10dp"
android:background="#99404348"
android:gravity="center"
android:orientation="horizontal">
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="@+id/login"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="40dp"
android:layout_marginRight="10dp"
android:layout_weight="1"
android:background="@drawable/buttonstyle"
android:text="Login"/>
<Button
android:id="@+id/cancel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="40dp"
android:layout_marginLeft="10dp"
android:background="@drawable/buttonstyle"
android:layout_weight="1"
android:text="Cancel"/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
布局layout文件之activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:background="@drawable/bg"
android:layout_width="match_parent"
android:layout_height="match_parent">
<View
android:layout_width="wrap_content"
android:layout_height="2dp"
android:background="#000" />
<GridLayout
android:layout_width="match_parent"
android:layout_height="89dp"
android:layout_gravity="top"
android:layout_weight="1.2">
<TextView
android:id="@+id/the_first_number"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="50dp" />
<TextView
android:id="@+id/the_option"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="50dp" />
<TextView
android:id="@+id/the_second_number"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="50dp" />
<TextView
android:id="@+id/the_equal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:maxLines="1"
android:textSize="50dp" />
<TextView
android:id="@+id/the_answer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="50dp" />
</GridLayout>
<View
android:layout_width="match_parent"
android:layout_height="3px"
android:background="@color/back"></View>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
<Button
android:id="@+id/surplus"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@drawable/circle"
android:text="%"
android:textColor="#99404348"
android:textSize="30sp" />
<Button
android:background="@drawable/circle"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_marginTop="5dp"
android:textColor="#99404348"
android:layout_marginBottom="5dp"
android:id="@+id/root"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="√"
android:textSize="30sp"/>
<Button
android:id="@+id/clear"
android:background="@drawable/circle"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp"
android:layout_width="0dp"
android:textColor="#99404348"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="C"
android:textSize="30sp"/>
<Button
android:id="@+id/delete"
android:background="@drawable/circle"
android:layout_marginLeft="5dp"
android:textColor="#99404348"
android:layout_marginRight="5dp"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="DEL"
android:textSize="30sp"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
<Button
android:id="@+id/seven"
android:layout_width="0dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp"
android:textColor="#99404348"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="7"
android:background="@drawable/circle"
android:textSize="30sp"/>
<Button
android:id="@+id/eight"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:textColor="#99404348"
android:text="8"
android:background="@drawable/circle"
android:textSize="30sp"/>
<Button
android:id="@+id/nine"
android:layout_width="0dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp"
android:background="@drawable/circle"
android:layout_height="match_parent"
android:textColor="#99404348"
android:layout_weight="1"
android:text="9"
android:textSize="30sp"/>
<Button
android:id="@+id/substraction"
android:layout_width="0dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp"
android:background="@drawable/circle"
android:textColor="#99404348"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="-"
android:textSize="30sp"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
<Button
android:id="@+id/four"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp"
android:layout_width="0dp"
android:textColor="#99404348"
android:background="@drawable/circle"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="4"
android:textSize="30sp"/>
<Button
android:id="@+id/five"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp"
android:layout_width="0dp"
android:textColor="#99404348"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@drawable/circle"
android:text="5"
android:textSize="30sp" />
<Button
android:id="@+id/six"
android:layout_width="0dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp"
android:textColor="#99404348"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="6"
android:background="@drawable/circle"
android:textSize="30sp"/>
<Button
android:id="@+id/add"
android:layout_width="0dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp"
android:textColor="#99404348"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="+"
android:background="@drawable/circle"
android:textSize="30sp"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
<Button
android:id="@+id/one"
android:layout_width="0dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp"
android:textColor="#99404348"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@drawable/circle"
android:text="1"
android:textSize="30sp"/>
<Button
android:id="@+id/two"
android:layout_width="0dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp"
android:background="@drawable/circle"
android:textColor="#99404348"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="2"
android:textSize="30sp"/>
<Button
android:id="@+id/three"
android:layout_width="0dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp"
android:textColor="#99404348"
android:layout_height="match_parent"
android:background="@drawable/circle"
android:layout_weight="1"
android:text="3"
android:textSize="30sp"/>
<Button
android:id="@+id/division"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp"
android:textColor="#99404348"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="÷"
android:background="@drawable/circle"
android:textSize="30sp"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
<Button
android:id="@+id/zero"
android:layout_width="0dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp"
android:textColor="#99404348"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="0"
android:background="@drawable/circle"
android:textSize="30sp"/>
<Button
android:id="@+id/point"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp"
android:layout_width="0dp"
android:layout_height="match_parent"
android:textColor="#99404348"
android:layout_weight="1"
android:text="."
android:background="@drawable/circle"
android:textSize="30sp"/>
<Button
android:id="@+id/equal"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp"
android:textColor="#99404348"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="="
android:background="@drawable/circle"
android:textSize="30sp"/>
<Button
android:id="@+id/mulitipliction"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:textColor="#99404348"
android:text="×"
android:background="@drawable/circle"
android:textSize="30sp"/>
</LinearLayout>
</LinearLayout>
drawable文件中buttonstyle.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="false">
<shape>
<!-- 色值 -->
<solid android:color="#585EE0" />
<!-- 圆角 -->
<corners android:radius="10dp" />
</shape>
</item>
<item android:state_pressed="true">
<shape>
<!-- 色值 -->
<solid android:color="#289BE5" />
<!-- 圆角 -->
<corners android:radius="10dp" />
</shape>
</item>
</selector>
drawable文件中circle.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<solid android:color="#CEFE97"/>
<corners android:radius="360dip"/>
<padding
android:left="20dp"
android:top="10dp"
android:right="20dp"
android:bottom="10dp"
/>
</shape>
values文件中colors.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="colorPrimary">#008577</color>
<color name="colorPrimaryDark">#00574B</color>
<color name="colorAccent">#D81B60</color>
<color name="back">#C0C0C0</color>
<color name="button">#C75A</color>
</resources>
values文件中strings.xml
<resources>
<string name="app_name">mycalculator</string>
</resources>
values文件中styles.xml
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
</resources>
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
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.mycalculator">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.AppCompat.NoActionBar">
<activity android:name=".Login">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".MainActivity" >
</activity>
</application>
</manifest>