简单点名小程序(伪)----android开发

效果演示

显示学生姓名与图片

选择缺勤类型

提交缺勤名单

代码

activity_main.xml

<?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/textView_name"
        android:layout_width="168dp"
        android:layout_height="59dp"
        android:layout_marginStart="113dp"
        android:layout_marginTop="10dp"
        android:layout_marginEnd="103dp"
        android:layout_marginBottom="346dp"
        android:text="TextView"
        android:textSize="30sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.0" />

    <Button
        android:id="@+id/btn_up"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="71dp"
        android:layout_marginTop="360dp"
        android:layout_marginEnd="60dp"
        android:layout_marginBottom="265dp"
        android:text="上一个"
        android:textSize="20sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toStartOf="@+id/btn_down"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.0" />

    <Button
        android:id="@+id/btn_down"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="97dp"
        android:layout_marginTop="360dp"
        android:layout_marginEnd="67dp"
        android:layout_marginBottom="265dp"
        android:text="下一个"
        android:textSize="18sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toEndOf="@+id/btn_up"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.0" />

    <RadioGroup
        android:id="@+id/rio_group"
        android:layout_width="302dp"
        android:layout_height="150dp"
        android:layout_marginStart="12dp"
        android:layout_marginBottom="12dp"
        app:layout_constraintBottom_toTopOf="@+id/btn_submit"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toEndOf="@+id/btn_up"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.736">

        <RadioButton
            android:id="@+id/rio_1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:checked="true"
            android:text="出勤" />

        <RadioButton
            android:id="@+id/rio_late"
            android:layout_width="wrap_content"
            android:layout_height="48dp"
            android:layout_weight="1"
            android:text="迟到" />

        <RadioButton
            android:id="@+id/rio_absent"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="缺席" />

        <RadioButton
            android:id="@+id/rio_leave"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="请假" />
    </RadioGroup>

    <Button
        android:id="@+id/btn_submit"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="166dp"
        android:layout_marginTop="259dp"
        android:layout_marginEnd="157dp"
        android:layout_marginBottom="16dp"
        android:text="提交"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.666"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/radioGroup" />

    <ImageView
        android:id="@+id/image_stu"
        android:layout_width="356dp"
        android:layout_height="132dp"
        android:layout_marginStart="113dp"
        android:layout_marginTop="21dp"
        android:layout_marginEnd="125dp"
        android:layout_marginBottom="3dp"
        app:layout_constraintBottom_toTopOf="@+id/rio_group"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.48"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/textView_name"
        app:layout_constraintVertical_bias="0.857"
        app:srcCompat="@mipmap/f005" />

</android.support.constraint.ConstraintLayout>

MainActivity.java

package com.lkw.demo.demostu;

import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity implements OnCheckedChangeListener,View.OnClickListener, DialogInterface.OnClickListener  {

    private Button btn_submit;
    private Button btn_up;
    private Button btn_down;
    private TextView textView_name;
    private ImageView img;
    private int index;
    private String[] name = {"夏言好", "张飞", "奥特曼", "假面骑士", "嘎子哥", "lkw"};
    private int[] img_stu={R.mipmap.f000, R.mipmap.f001,R.mipmap.f002,R.mipmap.f003,R.mipmap.f004,R.mipmap.f005};
    private boolean[] leave;   //请假
    private boolean[] late;    //迟到
    private boolean[] absent;  //缺席




    private RadioGroup rio_group;
    private RadioButton rio_1;
    private RadioButton rio_late;
    private RadioButton rio_absent;
    private RadioButton rio_leave;


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

        rio_group = findViewById(R.id.rio_group);
        rio_1 = findViewById(R.id.rio_1);
        rio_late = findViewById(R.id.rio_late);
        rio_absent = findViewById(R.id.rio_absent);
        rio_leave = findViewById(R.id.rio_leave);
        img = findViewById(R.id.image_stu);

        rio_group.setOnCheckedChangeListener(this);
        rio_late.setOnClickListener(this);
        rio_leave.setOnClickListener(this);
        rio_absent.setOnClickListener(this);

        index = 0;

        /*未到的人数均设为0*/

            btn_up = findViewById(R.id.btn_up);
            btn_down = findViewById(R.id.btn_down);
            btn_submit = findViewById(R.id.btn_submit);
            textView_name = findViewById(R.id.textView_name);
            textView_name.setText(name[index]);

            late = new boolean[6];
            leave = new boolean[6];
            absent = new boolean[6];
            btn_up.setOnClickListener(this);
            btn_down.setOnClickListener(this);
            btn_submit.setOnClickListener(this);

    }

    @Override
    public void onClick(View view) {
        if (view.getId() == btn_submit.getId()) {

            String lateList = this.getList(late);
            String leaveList = this.getList(leave);
            String absentList = this.getList(absent);

            new AlertDialog.Builder(this).setMessage("迟到:" + lateList + "缺席:" + absentList + "请假:" + leaveList).setCancelable(false)
                    .setIcon(android.R.drawable.ic_menu_edit)
                    .setTitle("缺勤名单")
                    .setPositiveButton("发送", this)
                    .setNeutralButton("取消", this).show();
        } else {
            if (view.getId() == btn_up.getId()) {
                index--;
            }
            if (view.getId() == btn_down.getId()) {
                index++;
            }
            if (index == -1) {
                index = 0;
                Toast.makeText(this, R.string.warn_name_first, Toast.LENGTH_LONG).show();
            }
            if (index == 6) {
                index = 5;
                Toast.makeText(this, R.string.warn_name_last, Toast.LENGTH_LONG).show();
            }

            textView_name.setText(name[index]);
            img.setImageResource(img_stu[index]);

           if (leave[index]) {
                rio_leave.setChecked(true);
            } else if (late[index]) {
                rio_late.setChecked(true);
            } else if (absent[index]){
                rio_absent.setChecked(true);
            }else{
                rio_1.setChecked(true);
            }
        }

    }






    @Override
    public void onClick(DialogInterface dialogInterface,int i){

            }




    private String getList(boolean[]tex){
                String answer="";
                for(int i=0;i<name.length;++i){
                    if(tex[i])
                        answer+=" "+name[i];
                }
                answer+="\n";
                return answer;
            }


    @Override
    public void onCheckedChanged(RadioGroup radioGroup, int i) {
        switch(i){
            case R.id.rio_late:
                absent[index] = false;
                leave[index] = false;
                late[index] = true;
                break;
            case R.id.rio_absent:
                leave[index] = false;
                late[index] = false;
                absent[index] = true;
                break;
            case R.id.rio_leave:
                late[index] = false;
                absent[index] = false;
                leave[index] = true;
                break;
        }
    }
}

总结

   通过第一阶段的学习对Android有了一个基本的入门,光从现在所掌握的知识,让我感觉现阶段的学习和以前javaSE的图形用户界面很像。但要直白简单一些。基本步骤就是添加一个视图,往里面放东西。在后台给这些东西绑上监听器,完成各种各样的事件逻辑,实现一些基本的小功能。上面的那个点名小程序也暂时只能算伪的。毕竟还不能实现真正的提交,真正的获取学生信息。这些都还需要后续学习的完善和实现。

  上面的代码也还存在一定的问题。学生的信息没有单独的封装成一个模型类,这是我之前写的时候没考虑到的。这会为后续的完善埋下隐患。

   另外就是开发软件给我的一些感受,Android studio的界面和idea真的贼像,大同小异。而且文件结构也和idea javaweb项目差不多。这让我看一遍之后就能牢记它的源文件,资源文件,配置文件都在什么位置。用起来还是蛮舒心的,没有啥阵痛期。

 

  • 3
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,让我们来一起编写一个随机点名小程序,使用到泛型和集合框架。 首先,我们需要定义一个学生类,包含学生的姓名和学号: ```java public class Student { private String name; private String id; public Student(String name, String id) { this.name = name; this.id = id; } public String getName() { return name; } public String getId() { return id; } } ``` 接下来,我们可以使用一个泛型类来表示班级,其中包含了所有的学生对象: ```java import java.util.ArrayList; import java.util.List; import java.util.Random; public class Class<T extends Student> { private List<T> students; public Class() { students = new ArrayList<>(); } public void addStudent(T student) { students.add(student); } public T getRandomStudent() { Random random = new Random(); int index = random.nextInt(students.size()); return students.get(index); } } ``` 在这个泛型类中,我们使用了一个 ArrayList 来保存所有的学生对象,然后通过 getRandomStudent 方法随机返回一个学生对象。 最后,我们可以编写一个测试类来模拟随机点名的过程: ```java public class Test { public static void main(String[] args) { Class<Student> class1 = new Class<>(); class1.addStudent(new Student("张三", "001")); class1.addStudent(new Student("李四", "002")); class1.addStudent(new Student("王五", "003")); System.out.println("随机点名结果:"); Student student = class1.getRandomStudent(); System.out.println(student.getName() + " " + student.getId()); } } ``` 在这个测试类中,我们创建了一个班级对象 class1,并添加了三个学生对象。然后通过调用 getRandomStudent 方法随机返回一个学生对象,并输出其姓名和学号。 这就是一个简单随机点名小程序,使用了泛型和集合框架来实现。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值