栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

第38篇 Android Studio实现点击图片显示信息(五)逻辑代码

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

第38篇 Android Studio实现点击图片显示信息(五)逻辑代码

第38篇 Android Studio实现点击图片显示信息(五)逻辑代码
  • 5.代码
    • 5.1.设置整体监听的两种方法

5.代码
package com.example.dormitoryinformation;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.widget.RadioGroup;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity implements RadioGroup.OnCheckedChangeListener{
    RadioGroup rgp_one;
    RadioGroup rgp_two;
    boolean is_select_rgb_one = true;

    String[] studentName = {"姓名: 张三","姓名: 李四","姓名: 王五","姓名: 杨六"};
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        rgp_one = findViewById(R.id.rgp_one);
        rgp_two = findViewById(R.id.rgp_two);
        rgp_one.setOnCheckedChangeListener(this);
        rgp_two.setOnCheckedChangeListener(this);
    }

    @Override
    public void onCheckedChanged(RadioGroup radioGroup, int i) {
        TextView student_name = findViewById(R.id.txt_student_name);
        TextView student_sex = findViewById(R.id.txt_student_sex);
        TextView student_ban = findViewById(R.id.txt_student_ban);
        TextView student_dormitory = findViewById(R.id.txt_student_dormitory);
        TextView student_bed = findViewById(R.id.txt_student_bed);
        TextView student_class = findViewById(R.id.txt_student_class);
        
        if(radioGroup.getId() == R.id.rgp_one){
            if(!is_select_rgb_one){
                rgp_two.clearCheck();
                is_select_rgb_one = true;
            }
        }
        else if(radioGroup.getId() == R.id.rgp_two) {
            if(is_select_rgb_one){
                rgp_one.clearCheck();
                is_select_rgb_one = false;
            }
        }

        if(i == R.id.rbt_student1){
            student_name.setText(String.format("%s%s", this.getString(R.string.student_name), this.getString(R.string.student_name_zhao)));
            student_sex.setText(String.format("%s%s", this.getString(R.string.student_sex), this.getString(R.string.sex_man)));
            student_ban.setText(String.format("%s%s", this.getString(R.string.student_ban), this.getString(R.string.ban_number)));
            student_dormitory.setText(String.format("%s%s", this.getString(R.string.student_dormitory), this.getString(R.string.dormitory_number)));
            student_bed.setText(String.format("%s%s", this.getString(R.string.student_bed), this.getString(R.string.bed_number_1)));
            student_class.setText(String.format("%s%s", this.getString(R.string.student_class), this.getString(R.string.class_name)));
        }
        else if(i == R.id.rbt_student2){
            student_name.setText(studentName[1]);
            student_sex.setText(R.string.sex_man);
            student_ban.setText(R.string.ban_number);
            student_dormitory.setText(R.string.dormitory_number);
            student_bed.setText(R.string.bed_number_2);
            student_class.setText(R.string.class_name);
        }
        else if(i == R.id.rbt_student3){
            student_name.setText(studentName[2]);
            student_sex.setText(R.string.sex_man);
            student_ban.setText(R.string.ban_number);
            student_dormitory.setText(R.string.dormitory_number);
            student_bed.setText(R.string.bed_number_3);
            student_class.setText(R.string.class_name);
        }
        else if(i == R.id.rbt_student4){
            student_name.setText(studentName[3]);
            student_sex.setText(R.string.sex_man);
            student_ban.setText(R.string.ban_number);
            student_dormitory.setText(R.string.dormitory_number);
            student_bed.setText(R.string.bed_number_4);
            student_class.setText(R.string.class_name);
        }
        else if(i == R.id.rbt_student5 || i == R.id.rbt_student6){
            student_name.setText(R.string.no_student);
            student_sex.setText("");
            student_ban.setText("");
            student_dormitory.setText("");
            student_bed.setText("");
            student_class.setText("");
        }
    }
}
5.1.设置整体监听的两种方法

如果我们需要这个按钮在多处使用,而且不多次初始化,那么就可以把它作为类变量,通过以下方法设置。

RadioGroup rgp_one;
RadioGroup rgp_two;

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

    rgp_one = findViewById(R.id.rgp_one);
    rgp_two = findViewById(R.id.rgp_two);
    rgp_one.setOnCheckedChangeListener(this);
    rgp_two.setOnCheckedChangeListener(this);
}

否则,直接就这样设置就行了,可以看到RadioButton都没有使用的,都不需要为它们设置变量,因为android的方便在于,可以通过id去在某一个地方使用这个按钮,不需要传变量。

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

		RadioGroup rgp_one = findViewById(R.id.rgp_one);
		RadioGroup rgp_two = findViewById(R.id.rgp_two);
		rgp_one.setOnCheckedChangeListener(this);
		rgp_two.setOnCheckedChangeListener(this);
}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/314947.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号