Android大作业

发布时间 2023-06-18 01:15:44作者: 林汐岚

计算器

布局文件

<?xml version="1.0" encoding="utf-8"?>
<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    android:rowCount="8"
    android:columnCount="5"
    >

    <TextView
        android:id="@+id/txt_input"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_row="0"
        android:layout_column="0"
        android:layout_columnSpan="4"
        android:layout_gravity="fill"
        android:layout_marginStart="4dp"
        android:text="@string/display"
        android:textSize="50sp"
        />
    <Button
        android:id="@+id/btn_clr"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_row="1"
        android:layout_column="0"
        android:layout_columnSpan="2"
        android:layout_gravity="fill"
        android:layout_marginLeft="7dp"
        android:layout_marginRight="7dp"
        android:text="@string/clear"
        android:textSize="26sp"
        />
    <Button
        android:id="@+id/btn_left"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_row="2"
        android:layout_column="0"
        android:layout_marginLeft="7dp"
        android:layout_marginRight="7dp"
        android:text="@string/left"
        android:textSize="26sp"
        />
    <Button
        android:id="@+id/btn_right"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_row="2"
        android:layout_column="1"
        android:layout_marginLeft="7dp"
        android:layout_marginRight="7dp"
        android:text="@string/right"
        android:textSize="26sp"
        />
    <Button
        android:id="@+id/btn_7"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_row="3"
        android:layout_column="0"
        android:layout_marginLeft="7dp"
        android:layout_marginRight="7dp"
        android:text="@string/num7"
        android:textSize="26sp"
        />
    <Button
        android:id="@+id/btn_8"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_row="3"
        android:layout_column="1"
        android:layout_marginLeft="7dp"
        android:layout_marginRight="7dp"
        android:text="@string/num8"
        android:textSize="26sp"
        />
    <Button
        android:id="@+id/btn_4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_row="4"
        android:layout_column="0"
        android:layout_marginLeft="7dp"
        android:layout_marginRight="7dp"
        android:text="@string/num4"
        android:textSize="26sp"
        />
    <Button
        android:id="@+id/btn_5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_row="4"
        android:layout_column="1"
        android:layout_marginLeft="7dp"
        android:layout_marginRight="7dp"
        android:text="@string/num5"
        android:textSize="26sp"
        />
    <Button
        android:id="@+id/btn_1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_row="5"
        android:layout_column="0"
        android:layout_marginLeft="7dp"
        android:layout_marginRight="7dp"
        android:text="@string/num1"
        android:textSize="26sp"
        />
    <Button
        android:id="@+id/btn_2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_row="5"
        android:layout_column="1"
        android:layout_marginLeft="7dp"
        android:layout_marginRight="7dp"
        android:text="@string/num2"
        android:textSize="26sp"
        />
    <Button
        android:id="@+id/btn_0"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_row="6"
        android:layout_column="0"
        android:layout_columnSpan="2"
        android:layout_gravity="fill"
        android:layout_marginLeft="7dp"
        android:layout_marginRight="7dp"
        android:text="@string/num0"
        android:textSize="26sp"
        />
    <Button
        android:id="@+id/btn_del"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_row="1"
        android:layout_column="2"
        android:layout_columnSpan="2"
        android:layout_gravity="fill"
        android:layout_marginLeft="7dp"
        android:layout_marginRight="7dp"
        android:text="@string/del"
        android:textSize="26sp"
        />
    <Button
        android:id="@+id/btn_div"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_row="2"
        android:layout_column="2"
        android:layout_marginLeft="7dp"
        android:layout_marginRight="7dp"
        android:text="@string/div"
        android:textSize="26sp"
        />
    <Button
        android:id="@+id/btn_mul"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_row="2"
        android:layout_column="3"
        android:layout_marginLeft="7dp"
        android:layout_marginRight="7dp"
        android:text="@string/mul"
        android:textSize="26sp"
        />
    <Button
        android:id="@+id/btn_9"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_row="3"
        android:layout_column="2"
        android:layout_marginLeft="7dp"
        android:layout_marginRight="7dp"
        android:text="@string/num9"
        android:textSize="26sp"
        />
    <Button
        android:id="@+id/btn_sub"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_row="3"
        android:layout_column="3"
        android:layout_marginLeft="7dp"
        android:layout_marginRight="7dp"
        android:text="@string/sub"
        android:textSize="26sp"
        />
    <Button
        android:id="@+id/btn_6"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_row="4"
        android:layout_column="2"
        android:layout_marginLeft="7dp"
        android:layout_marginRight="7dp"
        android:text="@string/num6"
        android:textSize="26sp"
        />
    <Button
        android:id="@+id/btn_sum"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_row="4"
        android:layout_column="3"
        android:layout_marginLeft="7dp"
        android:layout_marginRight="7dp"
        android:text="@string/sum"
        android:textSize="26sp"
        />
    <Button
        android:id="@+id/btn_3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_row="5"
        android:layout_column="2"
        android:layout_marginLeft="7dp"
        android:layout_marginRight="7dp"
        android:text="@string/num3"
        android:textSize="26sp"
        />
    <Button
        android:id="@+id/btn_dec"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_row="6"
        android:layout_column="2"
        android:layout_marginLeft="7dp"
        android:layout_marginRight="7dp"
        android:text="@string/dec"
        android:textSize="26sp"
        />
    <Button
        android:id="@+id/btn_equ"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_row="5"
        android:layout_column="3"
        android:layout_rowSpan="2"
        android:layout_gravity="fill"
        android:layout_marginLeft="7dp"
        android:layout_marginRight="7dp"
        android:text="@string/equ"
        android:textSize="26sp"
        />
</GridLayout>

Java文件

package cn.edu.xju.mycomputer;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import java.util.Objects;
import java.util.Stack;

public class MainActivity extends AppCompatActivity  implements View.OnClickListener{

    //创建对象
    Button btn_0,btn_1,btn_2,btn_3,btn_4,btn_5,btn_6,btn_7,btn_8,btn_9,btn_dec;
    Button btn_mul,btn_div,btn_sum,btn_sub;
    Button btn_clr,btn_del,btn_equ;
    Button btn_left,btn_right;
    TextView txt_input;
    Boolean resErr = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //实例化对象
        btn_0= (Button) findViewById(R.id.btn_0);
        btn_1= (Button) findViewById(R.id.btn_1);
        btn_2= (Button) findViewById(R.id.btn_2);
        btn_3= (Button) findViewById(R.id.btn_3);
        btn_4= (Button) findViewById(R.id.btn_4);
        btn_5= (Button) findViewById(R.id.btn_5);
        btn_6= (Button) findViewById(R.id.btn_6);
        btn_7= (Button) findViewById(R.id.btn_7);
        btn_8= (Button) findViewById(R.id.btn_8);
        btn_9= (Button) findViewById(R.id.btn_9);
        btn_dec= (Button) findViewById(R.id.btn_dec);
        btn_sum= (Button) findViewById(R.id.btn_sum);
        btn_sub= (Button) findViewById(R.id.btn_sub);
        btn_mul= (Button) findViewById(R.id.btn_mul);
        btn_div= (Button) findViewById(R.id.btn_div);
        btn_clr= (Button) findViewById(R.id.btn_clr);
        btn_del= (Button) findViewById(R.id.btn_del);
        btn_equ= (Button) findViewById(R.id.btn_equ);
        txt_input= (TextView) findViewById(R.id.txt_input);
        btn_left= (Button) findViewById(R.id.btn_left);
        btn_right= (Button) findViewById(R.id.btn_right);

        //给按钮设置的点击事件
        btn_0.setOnClickListener(this);
        btn_1.setOnClickListener(this);
        btn_2.setOnClickListener(this);
        btn_3.setOnClickListener(this);
        btn_4.setOnClickListener(this);
        btn_5.setOnClickListener(this);
        btn_6.setOnClickListener(this);
        btn_7.setOnClickListener(this);
        btn_8.setOnClickListener(this);
        btn_9.setOnClickListener(this);
        btn_dec.setOnClickListener(this);
        btn_sum.setOnClickListener(this);
        btn_sub.setOnClickListener(this);
        btn_mul.setOnClickListener(this);
        btn_div.setOnClickListener(this);
        btn_clr.setOnClickListener(this);
        btn_del.setOnClickListener(this);
        btn_equ.setOnClickListener(this);
        btn_left.setOnClickListener(this);
        btn_right.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        if(resErr) {
            txt_input.setText("");
            resErr = false;
        }

        String str=txt_input.getText().toString();
        switch (v.getId()){
            case   R.id.btn_0:
            case   R.id.btn_1:
            case   R.id.btn_2:
            case   R.id.btn_3:
            case   R.id.btn_4:
            case   R.id.btn_5:
            case   R.id.btn_6:
            case   R.id.btn_7:
            case   R.id.btn_8:
            case   R.id.btn_9:
                txt_input.setText(str+((Button)v).getText());
                break;
            case   R.id.btn_left:
                if(!str.equals("") && !lastOperation(str, null)) {
                    str = str + " * ";
                }
                txt_input.setText(str+((Button)v).getText());
                break;
            case   R.id.btn_right:
                if (str.contains("(")) {
                    if (lastOperation(str, "dec")) {
                        str = str + "0";
                    }
                    txt_input.setText(str+((Button)v).getText());
                }
                break;
            case R.id.btn_sum:
            case R.id.btn_sub:
            case R.id.btn_mul:
            case R.id.btn_div:
                if (lastOperation(str, "dec")) {
                    str = str + "0";
                }
                if(!lastOperation(str, null)) {
                    txt_input.setText(str+" "+((Button)v).getText()+" ");
                }
                break;
            case   R.id.btn_dec:
                if (!hasPoint(str)) {
                    if (lastOperation(str, null) || lastOperation(str, "left")  || str.equals("")) {
                        str = str + "0";
                    }
                    txt_input.setText(str+((Button)v).getText());
                }
                break;
            case R.id.btn_clr:
                txt_input.setText("");
                break;
            case R.id.btn_del: //判断是否为空,然后在进行删除
                if(!str.equals("")){
                    txt_input.setText(str.substring(0, str.length() - 1));
                }
                break;
            case R.id.btn_equ: //单独运算最后结果
                getResult();//调用下面的方法
                break;
        }
    }

    // 判断首位是否为运算符号、小数点
    private static Boolean firstOperation(String str, String type) {
        String newStr = str.trim();
        if (newStr.equals("")) return false;
        char firstChar = newStr.charAt(0);
        if (Objects.equals(type, "dec")) {
            return firstChar == '.';
        } else if(Objects.equals(type, "left")){
            return firstChar == '(';
        }else if(Objects.equals(type, "right")){
            return firstChar == ')';
        } else {
            return firstChar == '+' || firstChar == '-' || firstChar == '*' || firstChar == '/';
        }
    }

    // 判断末位是否为运算符号、小数点
    private static Boolean lastOperation(String str, String type) {
        String newStr = str.trim();
        if (newStr.equals("")) return false;
        char lastChar = newStr.charAt(newStr.length()-1);
        if (Objects.equals(type, "dec")) {
            return lastChar == '.';
        } else if(Objects.equals(type, "left")){
            return lastChar == '(';
        } else if(Objects.equals(type, "right")){
            return lastChar == ')';
        } else {
            return lastChar == '+' || lastChar == '-' || lastChar == '*' || lastChar == '/';
        }
    }

    // 判断当前输入到运算符前,是否有小数点
    private static Boolean hasPoint(String str) {
        int index = 0;
        String[] operationArr = {"+", "-", "*", "/"};
        for(int i = 0; i < operationArr.length; i++) {
            int w = str.lastIndexOf(operationArr[i]);
            if (w > index) index = w;
        }
        String newStr = str.substring(index);
        return newStr.contains(".");
    }

    private void getResult() {
        String exp=txt_input.getText().toString();
        if(exp.equals("")) return ;
        resErr = false;
        // 判断括号是否正确
        if (getNum(exp, "(") != getNum(exp, ")")) {
            resErr = true;
            txt_input.setText("等式有问题!!!");
            return ;
        }
        //因为没有运算符所以不用运算
        if(!exp.contains(" ")){
            return ;
        }
        if (firstOperation(exp, null)) {
            exp = "0 " + exp;
        }
        if(lastOperation(exp, null) || lastOperation(exp, "dec")) {
            exp = exp + "0 ";
        }

        Stack<Double> numStack = new Stack<>();
        Stack<Character> signalStack = new Stack<>();
        int index = 0;// 记录已经执行的符号数
        int len = exp.length();
        while (index < len) {
            char c = exp.charAt(index); // 取出这一步的符号
            if (c == '(') {
                signalStack.push(c);// 若是左括号就进栈
            }
            // 否则要先判断优先级
            else if (c == '+' || c == '-' || c == '*' || c == '/') {
                int currOperLevel = getOperlevel(c);// 当前符号的优先级
                while (true) {
                    int stackOperLevel = 0;// 栈顶元素的优先级
                    if (!signalStack.isEmpty()) {
                        Object obj = signalStack.peek();
                        stackOperLevel = getOperlevel((char) obj);
                    }
                    // 若当前元素优先级大于栈顶元素的优先级则入栈
                    if (currOperLevel > stackOperLevel) {
                        signalStack.push(c);
                        break;// 直到让比自己优先级高的符号都出栈运算了再把自己进栈
                    } else {// 不能入栈就进行计算
                        try {
                            char optemp = '0';
                            double num1 = 0;
                            double num2 = 0;
                            if (!signalStack.isEmpty()) {
                                optemp = (char) signalStack.pop();// 取出优先级大的那个符号
                            }
                            if (!numStack.isEmpty()) {
                                num1 = (double) numStack.pop();
                                num2 = (double) numStack.pop();// 取出数据栈中的两个数
                            }
                            numStack.push(caculateResult(optemp, num2, num1));// 将算出来的结果数据再次进入数据栈
                        } catch (Exception e) {
                            // TODO: handle exception
                            e.printStackTrace();
                        }
                    }
                }
            } else if (c == ')') {// 右括号就返回栈顶元素,右括号是不进栈的
                while (true) {
                    char theop = (char) signalStack.pop();
                    if (theop == '(') {
                        break;
                    } else {
                        try {
                            double num1 = (double) numStack.pop();
                            double num2 = (double) numStack.pop();
                            numStack.push(caculateResult(theop, num2, num1));// 运算括号内的内容
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                }
            } else if (c >= '0' && c <= '9') {
                int tempIndex = index + 1;
                while (tempIndex < len) {
                    char temp = exp.charAt(tempIndex);// 取字符串中处于当前字符的下一位
                    if ((temp >= '0' && temp <= '9') || temp == '.') {
                        tempIndex++;// 若为数字则继续向后取
                    } else {
                        break;// 证明数字去完
                    }
                }
                String numstr = exp.substring(index, tempIndex);// 截取这个字符串则为两个符号之间的数字
                try {
                    double numnum = Double.parseDouble(numstr);// 将数字转换成整型便于运算
                    numStack.push(numnum);
                    index = tempIndex - 1;
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            index++;
        }

        // 检查符号栈是否为空
        while (true) {
            Object obj = null;
            if (!signalStack.isEmpty()) {
                obj = signalStack.pop();
            }
            if (obj == null) {
                break;// 为空证明运算已结束
            } else {// 不为空就出栈运算
                char opterTemp = (char) obj;
                double num1 = (double) numStack.pop();
                double num2 = (double) numStack.pop();
                numStack.push(caculateResult(opterTemp, num2, num1));
            }
        }
        double result = (double) numStack.pop();
        txt_input.setText(result + "");
    }

    private static int getNum(String originStr, String targetStr){
        int res = 0;
        int i = originStr.indexOf(targetStr);
        while (i != -1){
            i = originStr.indexOf(targetStr,i+1);
            res++;
        }
        return res;
    }

    //计算加减乘除余
    private static Double caculateResult(char optemp, double num1, double num2) {

        switch (optemp) {
            case '+':
                return num1 + num2;
            case '-':
                return num1 - num2;
            case '*':
                return num1 * num2;
            case '/':
                return num1 / num2;
        }
        return 0.0;
    }

    //返回符号优先级
    private static int getOperlevel(char c) {
        switch (c) {
            case '+':
            case '-':
                return 1;
            case '*':
            case '/':
                return 2;
            default:
                return 0;
        }
    }

}

图片播放器

布局文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:gravity="center|fill"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center">
        <ImageView
            android:id="@+id/img"
            android:layout_width="420dip"
            android:layout_height="360dip"
            android:src="@drawable/img1"
            />
    </LinearLayout>

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content">
        <Button
            android:id="@+id/btn_last"
            android:layout_width="160dip"
            android:layout_height="wrap_content"
            android:layout_marginLeft="20dip"
            android:layout_marginRight="20dip"
            style="?android:attr/buttonBarButtonStyle"
            android:text="@string/last"
            />
        <Button
            android:id="@+id/btn_next"
            android:layout_width="160dip"
            android:layout_height="wrap_content"
            android:layout_marginLeft="20dip"
            android:layout_marginRight="20dip"
            style="?android:attr/buttonBarButtonStyle"
            android:text="@string/next"
            />
    </LinearLayout>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text=""
        android:id="@+id/debug">
    </TextView>
</LinearLayout>

Java文件

package cn.edu.xju.myimage;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.os.CancellationSignal;
import android.provider.ContactsContract;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.function.Consumer;

import cn.edu.xju.myimage.R.id;

public class MainActivity extends AppCompatActivity {
    TextView debug;
    ImageView img;
    Button btn_last,btn_next;
    private final int[] imgs={
            R.drawable.img1,
            R.drawable.img2,
            R.drawable.img3,
            R.drawable.img4,
            R.drawable.img5,
            R.drawable.img6,
            //R.drawable.img7,
            //R.drawable.img8,
            //R.drawable.img9,
            //R.drawable.img10,
    };
    int index=0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        img=(ImageView) findViewById(id.img);
        btn_last=(Button) findViewById(id.btn_last);
        btn_next=(Button) findViewById(id.btn_next);
        btn_last.setOnClickListener(new mClick());
        btn_next.setOnClickListener(new mClick());

        debug=(TextView) findViewById(id.debug);

        /*
        String str= String.valueOf(imgs.length);
        debug.setText(str);
         */
    }

    private class mClick implements View.OnClickListener {
        @Override
        public void onClick(View v) {
            //上一张
            if(v==btn_last){
                if(index>0 && index< imgs.length){
                    index--;
                }
                else{
                    index=imgs.length-1;
                }
            }
            //下一张
            if(v==btn_next){
                if(index>=0 && index< imgs.length-1){
                    index++;
                }
                else{
                    index=0;
                }
            }
            img.setImageResource(imgs[index]);
            String str= String.valueOf(index);
            debug.setText(str);
        }
    }
}

音乐播放器

布局文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    android:orientation="vertical">

    <TextView
        android:id="@+id/text1"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:text="@string/hello"
        android:textSize="20sp"/>
    <ImageView
        android:id="@+id/img"
        android:layout_width="match_parent"
        android:layout_height="300dp"
        android:src="@drawable/img1"
        android:contentDescription="@string/image"
        />
    <CheckBox
        android:id="@+id/check1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
    <CheckBox
        android:id="@+id/check2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
    <CheckBox
        android:id="@+id/check3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
    <CheckBox
        android:id="@+id/check4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
    <CheckBox
        android:id="@+id/check5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        >
        <ImageButton
            android:id="@+id/Stop"
            android:layout_width="64dp"
            android:layout_height="64dp"
            android:layout_marginLeft="38dp"
            android:layout_marginRight="38dp"
            android:contentDescription="@string/stop"
            android:src="@drawable/stop" />
        <ImageButton
            android:id="@+id/Start"
            android:layout_width="64dp"
            android:layout_height="64dp"
            android:layout_marginLeft="38dp"
            android:layout_marginRight="38dp"
            android:src="@drawable/start"
            android:contentDescription="@string/start" />
        <ImageButton
            android:id="@+id/Pause"
            android:layout_width="64dp"
            android:layout_height="64dp"
            android:layout_marginLeft="38dp"
            android:layout_marginRight="38dp"
            android:src="@drawable/pause"
            android:contentDescription="@string/pause" />
    </LinearLayout>

</LinearLayout>

Java文件

package cn.edu.xju.myplayer;

import androidx.appcompat.app.AppCompatActivity;

import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.widget.CheckBox;
import android.widget.ImageButton;
import android.widget.TextView;

import java.io.IOException;

public class MainActivity extends AppCompatActivity {
    TextView txt;
    ImageButton mStopButton,mStartButton,mPauseButton;
    MediaPlayer mMediaPlayer;
    int res_file=R.raw.mtest1;
    //CheckBox ch1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mMediaPlayer=new MediaPlayer();
        txt=(TextView) findViewById(R.id.text1);
        mStopButton=(ImageButton) findViewById(R.id.Stop);
        mStartButton=(ImageButton) findViewById(R.id.Start);
        mPauseButton=(ImageButton) findViewById(R.id.Pause);
        //ch1=(CheckBox) findViewById(R.id.check1);

        mStopButton.setOnClickListener(new mStopClick());
        mStartButton.setOnClickListener(new mStartClick());
        mPauseButton.setOnClickListener(new mPauseClick());
    }

    private void playMusic(String path){
        try{
            mMediaPlayer.reset();//重置
            mMediaPlayer.setDataSource(path);//设置文件路径
            mMediaPlayer.prepare();//准备播放
            mMediaPlayer.start();//开始播放
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    private class mStopClick implements View.OnClickListener {
        @Override
        public void onClick(View v) {
            //检测是否正在播放
            if(mMediaPlayer.isPlaying()){
                mMediaPlayer.reset();//重置
                mMediaPlayer.release();//释放资源
            }
        }
    }

    private class mStartClick implements View.OnClickListener {
        @Override
        public void onClick(View v) {
            try{
                mMediaPlayer=MediaPlayer.create(MainActivity.this,res_file);
                mMediaPlayer.start();
            } catch (IllegalStateException e) {
                throw new RuntimeException(e);
            }
        }
    }

    private class mPauseClick implements View.OnClickListener {
        @Override
        public void onClick(View v) {
            if(mMediaPlayer.isPlaying()){
                mMediaPlayer.pause();
            }
            else{
                mMediaPlayer.start();
            }
        }
    }
}