nwafu android

实验一 

package com.example.administrator.a001;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {

    private static String TAG="LIFTCYCLE";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Log.i(TAG, "(1)onCreate()");

        Button button=(Button)findViewById(R.id.btn_finish);
        button.setOnClickListener(new View.OnClickListener(){
            public void onClick(View view){
                finish();
            }
        });
    }

    @Override
    public void onStart(){
        super.onStart();
        Log.i(TAG, "(2)onStart()");
    }

    @Override
    public void onRestoreInstanceState(Bundle savedInstanceState){
        super.onRestoreInstanceState(savedInstanceState);
        Log.i(TAG, "(3)onRestoreInstanceState()");
   }

    @Override
    public void onResume(){
        super.onResume();
        Log.i(TAG, "(4)onResume()");
    }

    @Override
    public void onSaveInstanceState(Bundle savedInstanceState){
        super.onSaveInstanceState(savedInstanceState);
        Log.i(TAG, "(5)onSaveInstanceState()");
    }

    @Override
    public void onRestart(){
        super.onRestart();
        Log.i(TAG, "(6)onRestart()");
    }

    @Override
    public void onPause(){
        super.onPause();
        Log.i(TAG, "(7)onPause()");
    }

    @Override
    public void onStop(){
        super.onStop();
        Log.i(TAG, "(8)onStop()");
    }

    @Override
    public void onDestroy(){
        super.onDestroy();
        Log.i(TAG, "(9)onDestroy()");
    }
}
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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="com.example.administrator.a001.MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="finish"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        android:id="@+id/btn_finish" />

</android.support.constraint.ConstraintLayout>

实验二

package com.example.administrator.calculator;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import java.util.Arrays;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    Button button_0;
    Button button_1;
    Button button_2;
    Button button_3;
    Button button_4;
    Button button_5;
    Button button_6;
    Button button_7;
    Button button_8;
    Button button_9;
    Button button_sum;
    Button button_reduce;
    Button button_ride;
    Button button_except;
    Button button_spot;
    Button button_equal;
    Button button_left;
    Button button_right;
    Button button_ac;
    Button button_del;
    private TextView input;
    private TextView output;
    private StringBuilder pending = new StringBuilder();

    private void initView() {
        button_0 = (Button) findViewById(R.id.button_0);
        button_1 = (Button) findViewById(R.id.button_1);
        button_2 = (Button) findViewById(R.id.button_2);
        button_3 = (Button) findViewById(R.id.button_3);
        button_4 = (Button) findViewById(R.id.button_4);
        button_5 = (Button) findViewById(R.id.button_5);
        button_6 = (Button) findViewById(R.id.button_6);
        button_7 = (Button) findViewById(R.id.button_7);
        button_8 = (Button) findViewById(R.id.button_8);
        button_9 = (Button) findViewById(R.id.button_9);
        button_sum = (Button) findViewById(R.id.button_sum);
        button_reduce = (Button) findViewById(R.id.button_reduce);
        button_ride = (Button) findViewById(R.id.button_ride);
        button_except = (Button) findViewById(R.id.button_except);
        button_spot = (Button) findViewById(R.id.button_spot);
        button_equal = (Button) findViewById(R.id.button_equal);
        button_left = (Button) findViewById(R.id.button_left);
        button_right = (Button) findViewById(R.id.button_right);
        button_ac = (Button) findViewById(R.id.button_ac);
        button_del = (Button) findViewById(R.id.button_del);
        input = (TextView) findViewById(R.id.textView1);
        output = (TextView) findViewById(R.id.textView2);

        button_0.setOnClickListener(this);
        button_1.setOnClickListener(this);
        button_2.setOnClickListener(this);
        button_3.setOnClickListener(this);
        button_4.setOnClickListener(this);
        button_5.setOnClickListener(this);
        button_6.setOnClickListener(this);
        button_7.setOnClickListener(this);
        button_8.setOnClickListener(this);
        button_9.setOnClickListener(this);
        button_sum.setOnClickListener(this);
        button_reduce.setOnClickListener(this);
        button_ride.setOnClickListener(this);
        button_except.setOnClickListener(this);
        button_spot.setOnClickListener(this);
        button_equal.setOnClickListener(this);
        button_left.setOnClickListener(this);
        button_right.setOnClickListener(this);
        button_ac.setOnClickListener(this);
        button_del.setOnClickListener(this);
    }

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

        initView();
        if(savedInstanceState != null){
            input.setText(savedInstanceState.getString("input"));
            output.setText(savedInstanceState.getString("output"));
            pending = pending.append(savedInstanceState.getString("pending"));
        }
    }

    @Override
    public void onSaveInstanceState(Bundle savedInstanceState){
        super.onSaveInstanceState(savedInstanceState);
        savedInstanceState.putString("input",input.getText().toString().trim());
        savedInstanceState.putString("output",output.getText().toString().trim());
        savedInstanceState.putString("pending",pending.toString().trim());
    }

    @Override
    public void onStop(){
        super.onStop();
    }

    @Override
    public void onDestroy(){
        super.onDestroy();
    }
    @Override
    public void onClick(View v) {
        int last = 0;
        if (pending.length() != 0) {
            last = pending.codePointAt(pending.length() - 1);
        }
        switch (v.getId()) {
            case R.id.button_0:
                pending = pending.append("0");
                input.setText(pending);
                break;
            case R.id.button_1:
                pending = pending.append("1");
                input.setText(pending);
                break;
            case R.id.button_2:
                pending = pending.append("2");
                input.setText(pending);
                break;
            case R.id.button_3:
                pending = pending.append("3");
                input.setText(pending);
                break;
            case R.id.button_4:
                pending = pending.append("4");
                input.setText(pending);
                break;
            case R.id.button_5:
                pending = pending.append("5");
                input.setText(pending);
                break;
            case R.id.button_6:
                pending = pending.append("6");
                input.setText(pending);
                break;
            case R.id.button_7:
                pending = pending.append("7");
                input.setText(pending);
                break;
            case R.id.button_8:
                pending = pending.append("8");
                input.setText(pending);
                break;
            case R.id.button_9:
                pending = pending.append("9");
                input.setText(pending);
                break;
            case R.id.button_sum:
                pending = pending.append("+");
                input.setText(pending);
                break;
            case R.id.button_reduce:
                pending = pending.append("-");
                input.setText(pending);
                break;
            case R.id.button_ride:
                pending = pending.append("×");
                input.setText(pending);
                break;
            case R.id.button_except:
                pending = pending.append("÷");
                input.setText(pending);
                break;
            case R.id.button_left:
                pending = pending.append("(");
                input.setText(pending);
                break;
            case R.id.button_right:
                pending = pending.append(")");
                input.setText(pending);
                break;
            case R.id.button_spot:
                if (judge1()) {
                    pending = pending.append(".");
                    input.setText(pending);
                }
                break;
            case R.id.button_del: //删除
                if (pending.length() != 0) {
                    pending = pending.delete(pending.length() - 1, pending.length());
                    input.setText(pending);
                }
                break;
            case R.id.button_ac: //清空
                pending = pending.delete(0, pending.length());
                input.setText(pending);
                output.setText(pending);
                break;
            case R.id.button_equal: //等于
                if ((pending.length() > 1)) {
                    cal inf = new cal();
                    String result;
                    try {
                        String a = inf.toSuffix(pending);

                        if((Double.parseDouble(inf.dealEquation(a)))-Math.round(Double.parseDouble(inf.dealEquation(a)))==0){
                            result = String.valueOf((long)Double.parseDouble(inf.dealEquation(a)));
                        }
                        else{
                            result = String.valueOf(Double.parseDouble(inf.dealEquation(a)));
                        }
                    } catch (Exception ex) {

                        result = "Error";
                    }
                    input.setText(pending);
                    output.setText(result);
                    pending = pending.delete(0, pending.length());
                    if (Character.isDigit(result.charAt(0))) {
                        pending = pending.append(result);
                    }
                }
                break;
            default:
                break;
        }
    }

    private boolean judge1() {
        String a = "+-×/.";
        int[] b = new int[a.length()];
        int max;
        for (int i = 0; i < a.length(); i++) {
            String c = "" + a.charAt(i);
            b[i] = pending.lastIndexOf(c);
        }
        Arrays.sort(b);
        if (b[a.length() - 1] == -1) {
            max = 0;
        } else {
            max = b[a.length() - 1];
        }
        if (pending.indexOf(".", max) == -1) {
            return true;
        } else {
            return false;
        }
    }
}

 

package com.example.administrator.calculator;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class cal {
    //使用集合定义好符号的运算优先级别
    private static final Map<Character,Integer> basic =new HashMap<Character, Integer>();
    static {
        basic.put('-',1);
        basic.put('+', 1);
        basic.put('×', 2);
        basic.put('÷', 2);
        basic.put('(', 0);//在运算中  ()的优先级最高,但是此处因程序中需要 故设置为0
    }


    //将中缀表达式转换为后缀表达式
    public String toSuffix(StringBuilder infix){
        List<String> queue = new ArrayList<String>();                                    //定义队列  用于存储 数字  以及最后的  后缀表达式
        List<Character> stack = new ArrayList<Character>();                             //定义栈    用于存储  运算符  最后stack中会被 弹空

        char[] charArr = infix.substring(0,infix.length()).trim().toCharArray();                                    //字符数组  用于拆分数字或符号
        String standard = "×÷+-()";                                                        //判定标准 将表达式中会出现的运算符写出来
        char ch = '&';                                                                    //在循环中用来保存 字符数组的当前循环变量的  这里仅仅是初始化一个值  没有意义
        int len = 0;                                                                    //用于记录字符长度 【例如100*2,则记录的len为3 到时候截取字符串的前三位就是数字】
        for (int i = 0; i < charArr.length; i++)                                    //开始迭代
        {

            ch = charArr[i];                                                            //保存当前迭代变量
            if(Character.isDigit(ch))
            {                                                    //如果当前变量为 数字
                len++;
            }
            else if(ch == '.')
            {                                                        //如果当前变量为  .  会出现在小数里面
                len++;
            }
            else if(standard.indexOf(ch) != -1)     //如果是上面标准中的 任意一个符号
            {
                if(len > 0) {                                                            //长度也有
                    queue.add(String.valueOf(Arrays.copyOfRange(charArr, i - len, i)));    //说明符号之前的可以截取下来做数字
                    len = 0;                                                            //长度置空
                }
                if(ch == '(') {                                                            //如果是左括号
                    stack.add(ch);                                                        //将左括号 放入栈中
                    continue;                                                            //跳出本次循环  继续找下一个位置
                }
                if (!stack.isEmpty()) {                                                    //如果栈不为empty
                    int size = stack.size() - 1;                                        //获取栈的大小-1  即代表栈最后一个元素的下标
                    boolean flag = false;                                                //设置标志位
                    while (size >= 0 && ch == ')' && stack.get(size) != '(') {            //若当前ch为右括号,则 栈里元素从栈顶一直弹出,直到弹出到 左括号
                        queue.add(String.valueOf(stack.remove(size)));                    //注意此处条件:ch并未入栈,所以并未插入队列中;同样直到找到左括号的时候,循环结束了,所以左括号也不会放入队列中【也就是:后缀表达式中不会出现括号】
                        size--;                                                            //size-- 保证下标永远在栈最后一个元素【栈中概念:指针永远指在栈顶元素】
                        flag = true;                                                    //设置标志位为true  表明一直在取()中的元素
                    }
                    if(ch==')'&&stack.get(size) == '('){
                        flag = true;
                    }
                    while (size >= 0 && !flag && basic.get(stack.get(size)) >= basic.get(ch)) {    //若取得不是()内的元素,并且当前栈顶元素的优先级>=对比元素 那就出栈插入队列
                        queue.add(String.valueOf(stack.remove(size)));                    //同样  此处也是remove()方法,既能得到要获取的元素,也能将栈中元素移除掉
                        size--;
                    }
                }
                if(ch != ')') {                                                            //若当前元素不是右括号
                    stack.add(ch);                                                        //就要保证这个符号 入栈
                } else {                                                                //否则就要出栈 栈内符号
                    stack.remove(stack.size() - 1);
                }
            }
            if(i == charArr.length - 1) {                                                //如果已经走到了  中缀表达式的最后一位
                if(len > 0) {                                                            //如果len>0  就截取数字
                    queue.add(String.valueOf(Arrays.copyOfRange(charArr, i - len+1, i+1)));
                }
                int size = stack.size() - 1;                                            //size表示栈内最后一个元素下标
                while (size >= 0) {                                                        //一直将栈内  符号全部出栈 并且加入队列中  【最终的后缀表达式是存放在队列中的,而栈内最后会被弹空】
                    queue.add(String.valueOf(stack.remove(size)));
                    size--;
                }
            }
        }
        String a = queue.toString();
        return a.substring(1,a.length()-1);
    }


    public String dealEquation(String equation){

        String [] arr = equation.split(", ");                                    //根据, 拆分字符串
        List<String> list = new ArrayList<String>();                            //用于计算时  存储运算过程的集合【例如list中当前放置  100   20  5  /  则取出20/5 最终将结果4存入list   此时list中结果为  100  4 】

        for (int i = 0; i < arr.length; i++) {                                    //此处就是上面说的运算过程, 因为list.remove的缘故,所以取出最后一个数个最后两个数  都是size-2
            int size = list.size();
            switch (arr[i]) {
                case "+": double a = Double.parseDouble(list.remove(size-2))+ Double.parseDouble(list.remove(size-2)); list.add(String.valueOf(a));     break;
                case "-": double b = Double.parseDouble(list.remove(size-2))- Double.parseDouble(list.remove(size-2)); list.add(String.valueOf(b));     break;
                case "×": double c = Double.parseDouble(list.remove(size-2))* Double.parseDouble(list.remove(size-2)); list.add(String.valueOf(c));     break;
                case "÷": double d = Double.parseDouble(list.remove(size-2))/ Double.parseDouble(list.remove(size-2)); list.add(String.valueOf(d));       break;
                default: list.add(arr[i]);     break;                                    //如果是数字  直接放进list中
            }
        }

        return list.size()
                == 1 ? list.get(0) : "运算失败" ;                    //最终list中仅有一个结果,否则就是算错了
    }
}

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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="com.example.administrator.calculator.MainActivity">

    <android.support.constraint.Guideline
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/guideline"
        app:layout_constraintGuide_percent="0.3"
        android:orientation="horizontal"
        tools:layout_editor_absoluteY="154dp"
        tools:layout_editor_absoluteX="0dp" />

    <Button
        android:id="@+id/button_spot"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:background="#666"
        android:padding="10dp"
        android:text="."
        android:textColor="#fff"
        android:textAppearance="?android:textAppearanceLarge"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toLeftOf="@+id/button_0"
        app:layout_constraintTop_toBottomOf="@+id/button_1"/>

    <Button
        android:id="@+id/button_0"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:padding="10dp"
        android:text="0"
        android:textColor="#fff"
        android:textAppearance="?android:textAppearanceLarge"
        android:background="#666"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toRightOf="@+id/button_spot"
        app:layout_constraintRight_toLeftOf="@+id/button_equal"
        app:layout_constraintTop_toBottomOf="@+id/button_2"/>

    <Button
        android:id="@+id/button_equal"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:background="#666"
        android:padding="10dp"
        android:text="="
        android:textColor="#fff"
        android:textAppearance="?android:textAppearanceLarge"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toRightOf="@+id/button_0"
        app:layout_constraintRight_toLeftOf="@+id/button_sum"
        app:layout_constraintTop_toBottomOf="@+id/button_3"/>

    <Button
        android:id="@+id/button_sum"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:background="#ff8000"
        android:padding="10dp"
        android:text="+"
        android:textColor="#fff"
        android:textAppearance="?android:textAppearanceLarge"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toRightOf="@+id/button_equal"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/button_reduce"/>

    <Button
        android:id="@+id/button_1"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:background="#666"
        android:padding="10dp"
        android:text="1"
        android:textColor="#fff"
        android:textAppearance="?android:textAppearanceLarge"
        app:layout_constraintBottom_toTopOf="@+id/button_spot"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toLeftOf="@+id/button_2"
        app:layout_constraintTop_toBottomOf="@+id/button_4"/>

    <Button
        android:id="@+id/button_2"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:background="#666"
        android:padding="10dp"
        android:text="2"
        android:textColor="#fff"
        android:textAppearance="?android:textAppearanceLarge"
        app:layout_constraintBottom_toTopOf="@+id/button_0"
        app:layout_constraintLeft_toRightOf="@+id/button_1"
        app:layout_constraintRight_toLeftOf="@+id/button_3"
        app:layout_constraintTop_toBottomOf="@+id/button_5"/>

    <Button
        android:id="@+id/button_3"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:background="#666"
        android:padding="10dp"
        android:text="3"
        android:textColor="#fff"
        android:textAppearance="?android:textAppearanceLarge"
        app:layout_constraintBottom_toTopOf="@+id/button_equal"
        app:layout_constraintLeft_toRightOf="@+id/button_2"
        app:layout_constraintRight_toLeftOf="@+id/button_reduce"
        app:layout_constraintTop_toBottomOf="@+id/button_6"/>

    <Button
        android:id="@+id/button_reduce"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:background="#ff8000"
        android:padding="10dp"
        android:text="-"
        android:textColor="#fff"
        android:textAppearance="?android:textAppearanceLarge"
        app:layout_constraintBottom_toTopOf="@+id/button_sum"
        app:layout_constraintLeft_toRightOf="@+id/button_3"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/button_ride"/>

    <Button
        android:id="@+id/button_4"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:background="#666"
        android:padding="10dp"
        android:text="4"
        android:textColor="#fff"
        android:textAppearance="?android:textAppearanceLarge"
        app:layout_constraintBottom_toTopOf="@+id/button_1"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toLeftOf="@+id/button_5"
        app:layout_constraintTop_toBottomOf="@+id/button_7"/>

    <Button
        android:id="@+id/button_5"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:background="#666"
        android:padding="10dp"
        android:text="5"
        android:textColor="#fff"
        android:textAppearance="?android:textAppearanceLarge"
        app:layout_constraintBottom_toTopOf="@+id/button_2"
        app:layout_constraintLeft_toRightOf="@+id/button_4"
        app:layout_constraintRight_toLeftOf="@+id/button_6"
        app:layout_constraintTop_toBottomOf="@+id/button_8"/>

    <Button
        android:id="@+id/button_6"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:background="#666"
        android:padding="10dp"
        android:text="6"
        android:textColor="#fff"
        android:textAppearance="?android:textAppearanceLarge"
        app:layout_constraintBottom_toTopOf="@+id/button_3"
        app:layout_constraintLeft_toRightOf="@+id/button_5"
        app:layout_constraintRight_toLeftOf="@+id/button_ride"
        app:layout_constraintTop_toBottomOf="@+id/button_9"/>

    <Button
        android:id="@+id/button_ride"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:background="#ff8000"
        android:padding="10dp"
        android:text="×"
        android:textColor="#fff"
        android:textAppearance="?android:textAppearanceLarge"
        app:layout_constraintBottom_toTopOf="@+id/button_reduce"
        app:layout_constraintLeft_toRightOf="@+id/button_6"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/button_except"/>

    <Button
        android:id="@+id/button_7"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:background="#666"
        android:padding="10dp"
        android:text="7"
        android:textColor="#fff"
        android:textAppearance="?android:textAppearanceLarge"
        app:layout_constraintBottom_toTopOf="@+id/button_4"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toLeftOf="@+id/button_8"
        app:layout_constraintTop_toBottomOf="@+id/button_left"/>

    <Button
        android:id="@+id/button_8"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:background="#666"
        android:padding="10dp"
        android:text="8"
        android:textColor="#fff"
        android:textAppearance="?android:textAppearanceLarge"
        app:layout_constraintBottom_toTopOf="@+id/button_5"
        app:layout_constraintLeft_toRightOf="@+id/button_7"
        app:layout_constraintRight_toLeftOf="@+id/button_9"
        app:layout_constraintTop_toBottomOf="@+id/button_right"/>

    <Button
        android:id="@+id/button_9"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:background="#666"
        android:padding="10dp"
        android:text="9"
        android:textColor="#fff"
        android:textAppearance="?android:textAppearanceLarge"
        app:layout_constraintBottom_toTopOf="@+id/button_6"
        app:layout_constraintLeft_toRightOf="@+id/button_8"
        app:layout_constraintRight_toLeftOf="@+id/button_except"
        app:layout_constraintTop_toBottomOf="@+id/button_ac"/>

    <Button
        android:id="@+id/button_except"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:background="#ff8000"
        android:padding="10dp"
        android:text="÷"
        android:textColor="#fff"
        android:textAppearance="?android:textAppearanceLarge"
        app:layout_constraintBottom_toTopOf="@+id/button_ride"
        app:layout_constraintLeft_toRightOf="@+id/button_9"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/button_del"/>

    <Button
        android:id="@+id/button_left"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:background="#ff8000"
        android:padding="10dp"
        android:text="("
        android:textColor="#fff"
        android:textAppearance="?android:textAppearanceLarge"
        app:layout_constraintBottom_toTopOf="@+id/button_7"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toLeftOf="@+id/button_right"
        app:layout_constraintTop_toTopOf="@+id/guideline" />

    <Button
        android:id="@+id/button_right"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:background="#ff8000"
        android:padding="10dp"
        android:text=")"
        android:textColor="#fff"
        android:textAppearance="?android:textAppearanceLarge"
        app:layout_constraintBottom_toTopOf="@+id/button_8"
        app:layout_constraintLeft_toRightOf="@+id/button_left"
        app:layout_constraintRight_toLeftOf="@+id/button_ac"
        app:layout_constraintTop_toTopOf="@+id/guideline" />

    <Button
        android:id="@+id/button_ac"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:background="#ff8000"
        android:padding="10dp"
        android:text="AC"
        android:textColor="#fff"
        android:textAppearance="?android:textAppearanceLarge"
        app:layout_constraintBottom_toTopOf="@+id/button_9"
        app:layout_constraintLeft_toRightOf="@+id/button_right"
        app:layout_constraintRight_toLeftOf="@+id/button_del"
        app:layout_constraintTop_toTopOf="@+id/guideline" />

    <Button
        android:id="@+id/button_del"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:background="#ff8000"
        android:padding="10dp"
        android:text="DEL"
        android:textColor="#fff"
        android:textAppearance="?android:textAppearanceLarge"
        app:layout_constraintBottom_toTopOf="@+id/button_except"
        app:layout_constraintLeft_toRightOf="@+id/button_ac"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="@+id/guideline" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:background="#A4A4A4"
        android:textColor="#000"
        android:textSize="56sp"
        android:autoSizeTextType="uniform"
        android:gravity="right|center_vertical"
        app:layout_constraintTop_toBottomOf="@+id/textView1"
        app:layout_constraintBottom_toTopOf="@+id/guideline"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintLeft_toLeftOf="parent" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:background="#A4A4A4"
        android:textColor="#000"
        android:textSize="56sp"
        android:autoSizeTextType="uniform"
        android:gravity="right|center_vertical"
        app:layout_constraintBottom_toTopOf="@+id/textView2"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"/>

</android.support.constraint.ConstraintLayout>

实验三

package com.example.gb.mylistview;

import android.content.DialogInterface;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    private List<Student> StudentList = new ArrayList<>();
    private EditText et_Class, et_ID, et_Name;
    private Button bt_add;
    private DateAdapter adapter;
    private ListView listView;

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

        bt_add = (Button) findViewById(R.id.bt);
        et_Class = (EditText) findViewById(R.id.et1);
        et_ID = (EditText) findViewById(R.id.et2);
        et_Name = (EditText) findViewById(R.id.et3);

        initStudents();
        adapter = new DateAdapter(MainActivity.this, R.layout.listview, StudentList);
        listView = (ListView) findViewById(R.id.lv);
        listView.setAdapter(adapter);
        myOnclick();
    }

    private void initStudents() {
        Student i0 = new Student("软件工程1602", "2016012934", "张胜君");
        StudentList.add(i0);
        Student i1 = new Student("软件工程1602", "2016012935", "郭斌");
        StudentList.add(i1);
        Student i2 = new Student("软件工程1602", "2016012936", "刘秦畅");
        StudentList.add(i2);
        Student i3 = new Student("软件工程1602", "2016012937", "张政");
        StudentList.add(i3);
    }

    private void myOnclick() {
        bt_add.setOnClickListener(new Button.OnClickListener() {

            @Override
            public void onClick(View v) {
                if (!et_Class.getText().toString().trim().equals("") && !et_ID.getText().toString().trim().equals("") && !et_Name.getText().toString().trim().equals("")) {
                    Student i = new Student(et_Class.getText().toString().trim(), et_ID.getText().toString().trim(), et_Name.getText().toString().trim());
                    StudentList.add(i);
                    adapter.notifyDataSetChanged();
                    Toast.makeText(getBaseContext(), "添加成功", Toast.LENGTH_SHORT).show();
                }
            }
        });
        listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> parent, View view, final int position, long id) {
                AlertDialog.Builder builder=new AlertDialog.Builder(MainActivity.this);
                builder.setMessage("确定删除?");
                builder.setTitle("提示");

                //添加AlertDialog.Builder对象的setPositiveButton()方法
                builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        if(StudentList.remove(position)!=null){
                            System.out.println("success");
                        }else {
                            System.out.println("failed");
                        }
                        adapter.notifyDataSetChanged();
                        Toast.makeText(getBaseContext(), "删除列表项", Toast.LENGTH_SHORT).show();
                    }
                });

                //添加AlertDialog.Builder对象的setNegativeButton()方法
                builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                    }
                });
                builder.create().show();
                return true;
                /*StudentList.remove(position);
                adapter.notifyDataSetChanged();
                return true;*/
            }
        });
    }
}
package com.example.gb.mylistview;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import java.util.List;

public class DateAdapter extends BaseAdapter{

    private Context context;
    private final int resourceId;
    private List<Student> stuList;

    DateAdapter(Context context, int textViewResourceId, List<Student> objects) {
        this.context = context;
        this.resourceId = textViewResourceId;
        this.stuList = objects;
    }

    @Override
    public int getCount() {
        return stuList.size();
    }

    @Override
    public Object getItem(int position) {
        return stuList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if(convertView == null){
            holder = new ViewHolder();
            convertView = LayoutInflater.from(context).inflate(resourceId, null);//实例化一个对象
            holder.StudentClass = (TextView) convertView.findViewById(R.id.item_1);
            holder.StudentID = (TextView) convertView.findViewById(R.id.item_2);//获取该布局内的文本视图
            holder.StudentName = (TextView) convertView.findViewById(R.id.item_3);
            convertView.setTag(holder);
        }else {
            holder = (ViewHolder) convertView.getTag();
        }
        holder.StudentClass.setText(stuList.get(position).getStuClass());
        holder.StudentID.setText(stuList.get(position).getStuID());
        holder.StudentName.setText(stuList.get(position).getStuName());//为文本视图设置文本内容
        return convertView;
    }

    private class ViewHolder{
        private TextView StudentClass;
        private TextView StudentID;
        private TextView StudentName;
    }
}
package com.example.gb.mylistview;

public class Student {
    private String stuClass;
    private String stuID;
    private String stuName;

    Student(String stuClass, String stuID, String stuName){
        this.stuClass = stuClass;
        this.stuID = stuID;
        this.stuName = stuName;
    }

    public String getStuClass(){
        return stuClass;
    }

    public String getStuID(){
        return stuID;
    }

    public String getStuName(){
        return stuName;
    }
}

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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">

    <TextView
        android:id="@+id/tv1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="班级:"
        android:textSize="20sp"
        app:layout_constraintBottom_toTopOf="@+id/tv2"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toLeftOf="@+id/et1"
        app:layout_constraintTop_toTopOf="parent" />

    <EditText
        android:id="@+id/et1"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toTopOf="@+id/et2"
        app:layout_constraintLeft_toRightOf="@+id/tv1"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"/>

    <TextView
        android:id="@+id/tv2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="学号:"
        android:textSize="20sp"
        app:layout_constraintBottom_toTopOf="@+id/tv3"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toLeftOf="@+id/et2"
        app:layout_constraintTop_toBottomOf="@+id/tv1"/>

    <EditText
        android:id="@+id/et2"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        app:layout_constraintLeft_toRightOf="@+id/tv2"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/et1"
        app:layout_constraintBottom_toTopOf="@+id/et3" />

    <TextView
        android:id="@+id/tv3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="姓名:"
        android:textSize="20sp"
        app:layout_constraintBottom_toTopOf="@+id/guideline1"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toLeftOf="@+id/et3"
        app:layout_constraintTop_toBottomOf="@+id/tv2" />

    <EditText
        android:id="@+id/et3"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toTopOf="@+id/guideline1"
        app:layout_constraintLeft_toRightOf="@+id/tv3"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/et2"/>

    <android.support.constraint.Guideline
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/guideline1"
        app:layout_constraintGuide_percent="0.3"
        android:orientation="horizontal"/>

    <Button
        android:id="@+id/bt"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:text="添加"
        android:textSize="20sp"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/guideline1"
        app:layout_constraintBottom_toTopOf="@+id/guideline2"/>

    <android.support.constraint.Guideline
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/guideline2"
        app:layout_constraintGuide_percent="0.38"
        android:orientation="horizontal"/>

    <TextView
        android:id="@+id/item_1"
        android:layout_width="0dp"
        android:layout_height="35dp"
        android:layout_weight="2"
        android:text="班级"
        android:gravity="center"
        android:textSize="20sp"
        android:background="@color/colorAccent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toLeftOf="@+id/item_2"
        app:layout_constraintTop_toBottomOf="@+id/guideline2"
        app:layout_constraintBottom_toTopOf="@+id/guideline3"/>

    <TextView
        android:id="@+id/item_2"
        android:layout_width="0dp"
        android:layout_height="35dp"
        android:layout_weight="2"
        android:text="学号"
        android:gravity="center"
        android:textSize="20sp"
        android:background="@color/colorAccent"
        app:layout_constraintLeft_toRightOf="@+id/item_1"
        app:layout_constraintRight_toLeftOf="@+id/item_3"
        app:layout_constraintTop_toBottomOf="@+id/guideline2"
        app:layout_constraintBottom_toTopOf="@+id/guideline3"/>
    <TextView
        android:id="@+id/item_3"
        android:layout_width="0dp"
        android:layout_height="35dp"
        android:layout_weight="2"
        android:text="姓名"
        android:gravity="center"
        android:textSize="20sp"
        android:background="@color/colorAccent"
        app:layout_constraintLeft_toRightOf="@+id/item_2"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/guideline2"
        app:layout_constraintBottom_toTopOf="@+id/guideline3"/>

    <android.support.constraint.Guideline
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/guideline3"
        app:layout_constraintGuide_percent="0.46"
        android:orientation="horizontal"/>

    <ListView
        android:id="@+id/lv"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/item_1"
        app:layout_constraintBottom_toBottomOf="parent">
    </ListView>

</android.support.constraint.ConstraintLayout>
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/item_1"
        android:layout_width="0dp"
        android:layout_height="30dp"
        android:layout_weight="2"
        android:text="班级"
        android:gravity="center"
        android:textSize="20sp"
        android:background="@drawable/shape"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toLeftOf="@+id/item_2"
        app:layout_constraintTop_toBottomOf="parent"
        app:layout_constraintBottom_toTopOf="parent"/>

    <TextView
        android:id="@+id/item_2"
        android:layout_width="0dp"
        android:layout_height="30dp"
        android:layout_weight="2"
        android:text="学号"
        android:gravity="center"
        android:textSize="20sp"
        android:background="@drawable/shape"
        app:layout_constraintLeft_toRightOf="@+id/item_1"
        app:layout_constraintRight_toLeftOf="@+id/item_3"
        app:layout_constraintTop_toBottomOf="parent"
        app:layout_constraintBottom_toTopOf="parent"/>
    <TextView
        android:id="@+id/item_3"
        android:layout_width="0dp"
        android:layout_height="30dp"
        android:layout_weight="2"
        android:text="姓名"
        android:gravity="center"
        android:textSize="20sp"
        android:background="@drawable/shape"
        app:layout_constraintLeft_toRightOf="@+id/item_2"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="parent"
        app:layout_constraintBottom_toTopOf="parent"/>

</android.support.constraint.ConstraintLayout>

实验四

package com.example.administrator.myactivity;

import android.app.Activity;
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 MainActivity extends AppCompatActivity {

    private Intent intent;
    private Button button_login;
    private TextView textView_show;

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

        intent=new Intent(this,LoginActivity.class);

        textView_show = (TextView) findViewById(R.id.tv_show);
        button_login = (Button) findViewById(R.id.bt_login);

        button_login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivityForResult(intent,0);
            }
        });
    }

    protected void onActivityResult(int requestCode,int resultCode,Intent data){
        super.onActivityResult(requestCode,resultCode,data);
        switch (requestCode){
            case 0:
                if(resultCode== Activity.RESULT_OK){
                    String name="用户名:" + data.getStringExtra("name");
                    textView_show.setText(name);
                }else if(resultCode==Activity.RESULT_CANCELED) {
                    textView_show.setText("错误");
                }
                break;
            }

    }
}

package com.example.administrator.myactivity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class LoginActivity extends AppCompatActivity {
    private EditText editText_name;
    private Button button_return;

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

        editText_name=(EditText)findViewById(R.id.et);
        button_return=(Button)findViewById(R.id.bt);

        button_return.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent i=new Intent();
                i.putExtra("name",editText_name.getText().toString().trim());
                setResult(RESULT_OK,i);
                finish();
            }
        });
    }
}
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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">

    <Button
        android:id="@+id/bt_login"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="登录"
        app:layout_constraintBottom_toTopOf="@+id/tv_show"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        />
    <TextView
        android:id="@+id/tv_show"
        android:layout_width="wrap_content"
        android:layout_height="40dp"
        android:text="用户名:"
        android:gravity="center"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/bt_login" />

</android.support.constraint.ConstraintLayout>
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/tv"
        android:layout_width="100dp"
        android:layout_height="40dp"
        android:text="用户名:"
        android:gravity="center"
        app:layout_constraintBottom_toTopOf="@+id/bt"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toLeftOf="@+id/et"
        app:layout_constraintTop_toTopOf="parent"/>
    <EditText
        android:id="@+id/et"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_constraintBottom_toBottomOf="@+id/tv"
        app:layout_constraintLeft_toRightOf="@+id/tv"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="@+id/tv"/>
    <Button
        android:id="@+id/bt"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="返回"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/tv"
        />

</android.support.constraint.ConstraintLayout>

实验五

package com.example.administrator.stopwatch;

import android.app.Activity;
import android.content.Intent;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {
    private static Handler handler = new Handler();
    private static TextView tv_show = null;
    private static Button bt_reset,bt_start,bt_stop;

    private static String time;

    private int min=0,sec=0,psec= 0;

    boolean runflag = true;
    int status = 0; // 0 - clear ; 1 - start ; 2 - stop
    long last,templast = -1;

    String str="";

    private Thread workThread;

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

        tv_show = (TextView)findViewById(R.id.tv_show);
        bt_reset = (Button)findViewById(R.id.bt_reset);
        bt_start = (Button)findViewById(R.id.bt_start);
        bt_stop = (Button)findViewById(R.id.bt_stop);

        workThread = new Thread(null,RefreshLabel,"workThread");
        workThread.start();
        last = System.currentTimeMillis();

        bt_start.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //计时开始
                status = 1;
                if(templast != -1)
                {
                    last = templast;
                }
            }
        });
        bt_stop.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //停止计时
                status = 2;
                templast = last;
            }
        });
        bt_reset.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                min = sec = psec = 0;
                //清零
                status = 0;
                last = System.currentTimeMillis();
                tv_show.setText("00:00:00");
                templast = -1;
            }
        });
    }

    private Runnable RefreshLabel = new Runnable() {
        @Override
        public void run() {
            try{
                while(runflag)
                {
                    Thread.sleep(50);
                    if(status == 1)
                    {
                        int t=0;
                        long tmp = System.currentTimeMillis();
                        long cha = tmp - last;

                        t = (int)cha/1000;
                        psec = (int)cha - t * 1000;
                        psec %= 100;
                        sec = t - ((int)t/60)*60;
                        min = (int)t/60;
                        if(min == 60)
                        {
                            min = 0;
                        }
                        str = "";
                        str = str + String.format("%02d",min) + ":" +String.format("%02d",sec) + ":" + String.format("%02d",psec);

                        tv_show.post(new Runnable() {
                            @Override
                            public void run() {
                                tv_show.setText(str);
                            }
                        });
                    }
                }
            }
            catch(InterruptedException e)
            {
                e.printStackTrace();
            }
        }
    };
}
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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="com.example.administrator.stopwatch.MainActivity">

    <TextView
        android:id="@+id/tv_title"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:text="秒表"
        android:gravity="center"
        android:textSize="35sp"
        app:layout_constraintBottom_toTopOf="@+id/tv_show"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/tv_show"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:text="00:00:00"
        android:gravity="center"
        android:textSize="35sp"
        app:layout_constraintBottom_toBottomOf="@+id/gl_1"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/tv_title" />

    <android.support.constraint.Guideline
        android:id="@+id/gl_1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        app:layout_constraintGuide_percent="0.3"/>

    <Button
        android:id="@+id/bt_reset"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="清零"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toLeftOf="@+id/bt_start"
        app:layout_constraintTop_toBottomOf="@+id/gl_1" />

    <Button
        android:id="@+id/bt_start"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="计时"
        app:layout_constraintLeft_toRightOf="@+id/bt_reset"
        app:layout_constraintRight_toLeftOf="@+id/bt_stop"
        app:layout_constraintTop_toBottomOf="@+id/gl_1" />

    <Button
        android:id="@+id/bt_stop"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="停止"
        app:layout_constraintLeft_toRightOf="@+id/bt_start"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/gl_1" />

</android.support.constraint.ConstraintLayout>

实验六

package com.example.gb.test6;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.ContextMenu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    private List<Student> StudentList = new ArrayList<>();
    private EditText et_Class, et_ID, et_Name;
    private Button bt_add;
    private ListViewAdapter adapter;
    private ListView listView;
    private int item_id;

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

        bt_add = (Button) findViewById(R.id.bt);
        et_Class = (EditText) findViewById(R.id.et1);
        et_ID = (EditText) findViewById(R.id.et2);
        et_Name = (EditText) findViewById(R.id.et3);
        dbAdapter = new DBAdapter(this);
        dbAdapter.open();
        initStudents();
        adapter = new ListViewAdapter(MainActivity.this, R.layout.listview, StudentList);
        listView = (ListView) findViewById(R.id.lv);
        listView.setAdapter(adapter);
        registerForContextMenu(listView);
        myOnclick();
    }

    private void initStudents() {
        StudentList= dbAdapter.findAllStudent();
    }

    private void myOnclick() {
        bt_add.setOnClickListener(new Button.OnClickListener() {

            @Override
            public void onClick(View v) {
                if (!et_Class.getText().toString().trim().equals("") && !et_ID.getText().toString().trim().equals("") && !et_Name.getText().toString().trim().equals("")) {
                    Student i = new Student(et_Class.getText().toString().trim(), et_ID.getText().toString().trim(), et_Name.getText().toString().trim());
                    if(dbAdapter.insert(i)==-1){
                        Toast.makeText(getBaseContext(), "添加失败", Toast.LENGTH_SHORT).show();
                    }else{
                        StudentList.add(i);
                        adapter.notifyDataSetChanged();
                        Toast.makeText(getBaseContext(), "添加成功", Toast.LENGTH_SHORT).show();
                    }
                }
            }
        });
    }

    @Override
    public void onCreateContextMenu(ContextMenu menu, View v,
                                    ContextMenu.ContextMenuInfo menuInfo) {
        // 加载xml中的上下文菜单
        super.onCreateContextMenu(menu, v, menuInfo);
        AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) menuInfo;
        item_id = info.position;
        MenuInflater menuInflater = getMenuInflater();
        menuInflater.inflate(R.menu.main, menu);
    }
    @Override
    public boolean onContextItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            /*case R.id.edit:
                Toast.makeText(MainActivity.this, "编辑操作", Toast.LENGTH_SHORT).show();
                break;*/
            /*case R.id.share:
                Toast.makeText(MainActivity.this, "分享操作", Toast.LENGTH_SHORT).show();
                break;*/
            case R.id.delete:
                String s = StudentList.get(item_id).getStuID();
                dbAdapter.deleteOneData(StudentList.get(item_id).getStuID());
                StudentList.remove(item_id);
                adapter.notifyDataSetChanged();
                Toast.makeText(MainActivity.this, "删除"+s+"成功", Toast.LENGTH_SHORT).show();
                break;
            default:
                break;
        }
        return super.onContextItemSelected(item);
    }
}
package com.example.gb.test6;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import java.util.List;

public class ListViewAdapter extends BaseAdapter{

    private Context context;
    private final int resourceId;
    private List<Student> stuList;

    ListViewAdapter(Context context, int textViewResourceId, List<Student> objects) {
        this.context = context;
        this.resourceId = textViewResourceId;
        this.stuList = objects;
    }

    @Override
    public int getCount() {
        return stuList.size();
    }

    @Override
    public Object getItem(int position) {
        return stuList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if(convertView == null){
            holder = new ViewHolder();
            convertView = LayoutInflater.from(context).inflate(resourceId, null);//实例化一个对象
            holder.StudentClass = (TextView) convertView.findViewById(R.id.item_1);
            holder.StudentID = (TextView) convertView.findViewById(R.id.item_2);//获取该布局内的文本视图
            holder.StudentName = (TextView) convertView.findViewById(R.id.item_3);
            convertView.setTag(holder);
        }else {
            holder = (ViewHolder) convertView.getTag();
        }
        holder.StudentClass.setText(stuList.get(position).getStuClass());
        holder.StudentID.setText(stuList.get(position).getStuID());
        holder.StudentName.setText(stuList.get(position).getStuName());//为文本视图设置文本内容
        return convertView;
    }

    private class ViewHolder{
        private TextView StudentClass;
        private TextView StudentID;
        private TextView StudentName;
    }
}
package com.example.gb.test6;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.database.sqlite.SQLiteOpenHelper;

import java.util.ArrayList;
import java.util.List;

public class DBAdapter {
    private static final String DB_NAME = "student.db";
    private static final String DB_TABLE = "studentinfo";
    private static final int DB_VERSION = 1;

    private static final String KEY_ROWID = "_id";
    private static final String KEY_NAME = "name";
    private static final String KEY_CLASS = "class";
    private static final String KEY_ID = "id";
    private static final String TAG = "DBAdapter";

    private SQLiteDatabase db;
    private final Context context;
    private DBOpenHelper dbOpenHelper;

    private static class DBOpenHelper extends SQLiteOpenHelper{
        public DBOpenHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
            super(context, name, factory, version);
        }

        private static final String DB_CREATE="create table " + DB_TABLE + "(" + KEY_ROWID +
                " integer primary key autoincrement, " + KEY_NAME  + " text not null, " +
                KEY_CLASS + " text, " + KEY_ID + " text unique not null);";
        @Override
        public void onCreate(SQLiteDatabase db) {
            db.execSQL(DB_CREATE);
        }

        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
            db.execSQL("DROP TABLE IF EXISTS " + DB_TABLE);
            onCreate(db);
        }
    }

    public DBAdapter(Context context){
        this.context = context;
    }

    public void open(){
        dbOpenHelper = new DBOpenHelper(context,DB_NAME,null,DB_VERSION);
        try {
            db = dbOpenHelper.getWritableDatabase();
        }catch (SQLiteException ex){
            db = dbOpenHelper.getReadableDatabase();
        }
    }

    public void close(){
        if(db!=null){
            db.close();
            db=null;
        }
    }

    public List<Student> findAllStudent()
    {

        Cursor c = db.query(DB_TABLE, null, null, null, null,null, null);
        Student []stu = convertToStudent(c);
        List<Student> StudentList = new ArrayList<>();
        if(stu!=null){
            for(int i=0;i<stu.length;i++){
                StudentList.add(stu[i]);
            }
            return StudentList;
        }else{
            return StudentList;
        }

    }

    public long insert(Student student){
        ContentValues newValues=new ContentValues();

        newValues.put(KEY_NAME,student.getStuName());
        newValues.put(KEY_CLASS,student.getStuClass());
        newValues.put(KEY_ID,student.getStuID());

        return db.insert(DB_TABLE,null,newValues);
    }

    public long deleteALLData(){
        return db.delete(DB_TABLE,null,null);
    }

    public long deleteOneData(String id){
        return db.delete(DB_TABLE,KEY_ID+"="+id,null);
    }

    private Student[] convertToStudent(Cursor c)
    {
        int resultsCount = c.getCount();
        if(resultsCount==0||!c.moveToFirst())
        {
            return null;
        }
        Student []stu = new Student[resultsCount];
        for (int i = 0; i < stu.length; i++) {
            String stuID  = c.getString(c.getColumnIndex("id"));
            String stuName = c.getString(c.getColumnIndex("name"));
            String stuClass = c.getString(c.getColumnIndex("class"));
            stu[i] = new Student(stuClass,stuID,stuName);

            //切记不可少了这一句,这个是循环的条件
            c.moveToNext();
        }
        return stu;
    }
}
package com.example.gb.test6;

public class Student {
    private String stuClass;
    private String stuID;
    private String stuName;

    Student(String stuClass, String stuID, String stuName){
        this.stuClass = stuClass;
        this.stuID = stuID;
        this.stuName = stuName;
    }

    public void setStuID(String stuID){
        this.stuID = stuID;
    }

    public void setStuName(String stuName){
        this.stuName = stuName;
    }

    public void setStuClass(String stuClass){
        this.stuClass = stuClass;
    }

    public String getStuClass(){
        return stuClass;
    }

    public String getStuID(){
        return stuID;
    }

    public String getStuName(){
        return stuName;
    }
}

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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">

    <TextView
        android:id="@+id/tv1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="班级:"
        android:textSize="20sp"
        app:layout_constraintBottom_toTopOf="@+id/tv2"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toLeftOf="@+id/et1"
        app:layout_constraintTop_toTopOf="parent" />

    <EditText
        android:id="@+id/et1"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toTopOf="@+id/et2"
        app:layout_constraintLeft_toRightOf="@+id/tv1"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"/>

    <TextView
        android:id="@+id/tv2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="学号:"
        android:textSize="20sp"
        app:layout_constraintBottom_toTopOf="@+id/tv3"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toLeftOf="@+id/et2"
        app:layout_constraintTop_toBottomOf="@+id/tv1"/>

    <EditText
        android:id="@+id/et2"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        app:layout_constraintLeft_toRightOf="@+id/tv2"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/et1"
        app:layout_constraintBottom_toTopOf="@+id/et3" />

    <TextView
        android:id="@+id/tv3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="姓名:"
        android:textSize="20sp"
        app:layout_constraintBottom_toTopOf="@+id/guideline1"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toLeftOf="@+id/et3"
        app:layout_constraintTop_toBottomOf="@+id/tv2" />

    <EditText
        android:id="@+id/et3"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toTopOf="@+id/guideline1"
        app:layout_constraintLeft_toRightOf="@+id/tv3"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/et2"/>

    <android.support.constraint.Guideline
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/guideline1"
        app:layout_constraintGuide_percent="0.3"
        android:orientation="horizontal"/>

    <Button
        android:id="@+id/bt"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:text="添加"
        android:textSize="20sp"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/guideline1"
        app:layout_constraintBottom_toTopOf="@+id/guideline2"/>

    <android.support.constraint.Guideline
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/guideline2"
        app:layout_constraintGuide_percent="0.38"
        android:orientation="horizontal"/>

    <TextView
        android:id="@+id/item_1"
        android:layout_width="0dp"
        android:layout_height="35dp"
        android:layout_weight="2"
        android:text="班级"
        android:gravity="center"
        android:textSize="20sp"
        android:background="@color/colorAccent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toLeftOf="@+id/item_2"
        app:layout_constraintTop_toBottomOf="@+id/guideline2"
        app:layout_constraintBottom_toTopOf="@+id/guideline3"/>

    <TextView
        android:id="@+id/item_2"
        android:layout_width="0dp"
        android:layout_height="35dp"
        android:layout_weight="2"
        android:text="学号"
        android:gravity="center"
        android:textSize="20sp"
        android:background="@color/colorAccent"
        app:layout_constraintLeft_toRightOf="@+id/item_1"
        app:layout_constraintRight_toLeftOf="@+id/item_3"
        app:layout_constraintTop_toBottomOf="@+id/guideline2"
        app:layout_constraintBottom_toTopOf="@+id/guideline3"/>
    <TextView
        android:id="@+id/item_3"
        android:layout_width="0dp"
        android:layout_height="35dp"
        android:layout_weight="2"
        android:text="姓名"
        android:gravity="center"
        android:textSize="20sp"
        android:background="@color/colorAccent"
        app:layout_constraintLeft_toRightOf="@+id/item_2"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/guideline2"
        app:layout_constraintBottom_toTopOf="@+id/guideline3"/>

    <android.support.constraint.Guideline
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/guideline3"
        app:layout_constraintGuide_percent="0.46"
        android:orientation="horizontal"/>

    <ListView
        android:id="@+id/lv"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/item_1"
        app:layout_constraintBottom_toBottomOf="parent">
    </ListView>

</android.support.constraint.ConstraintLayout>
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/item_1"
        android:layout_width="0dp"
        android:layout_height="30dp"
        android:layout_weight="2"
        android:text="班级"
        android:gravity="center"
        android:textSize="20sp"
        android:background="@drawable/shape"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toLeftOf="@+id/item_2"
        app:layout_constraintTop_toBottomOf="parent"
        app:layout_constraintBottom_toTopOf="parent"/>

    <TextView
        android:id="@+id/item_2"
        android:layout_width="0dp"
        android:layout_height="30dp"
        android:layout_weight="2"
        android:text="学号"
        android:gravity="center"
        android:textSize="20sp"
        android:background="@drawable/shape"
        app:layout_constraintLeft_toRightOf="@+id/item_1"
        app:layout_constraintRight_toLeftOf="@+id/item_3"
        app:layout_constraintTop_toBottomOf="parent"
        app:layout_constraintBottom_toTopOf="parent"/>
    <TextView
        android:id="@+id/item_3"
        android:layout_width="0dp"
        android:layout_height="30dp"
        android:layout_weight="2"
        android:text="姓名"
        android:gravity="center"
        android:textSize="20sp"
        android:background="@drawable/shape"
        app:layout_constraintLeft_toRightOf="@+id/item_2"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="parent"
        app:layout_constraintBottom_toTopOf="parent"/>

</android.support.constraint.ConstraintLayout>

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值