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

[Android]实验2.1 用户界面

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

[Android]实验2.1 用户界面

提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档

文章目录

运行图代码


一、运行图 

运行效果:

 

二、代码

MainActivity.java  代码如下(示例): 

package com.example.myapplication;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Spinner;
import android.widget.Toast;

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

public class MainActivity extends AppCompatActivity {
    private EditText edit;
    private RadioGroup genderGroup;
    private RadioButton male,female;
    private String info="",sex="",major="",hobby="";
    private Button btn1,btn2;
    private Spinner spinner;
    private List majorlist;
    private CheckBox hobby1,hobby2,hobby3,hobby4;
    private Toast toast;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        edit=findViewById(R.id.edit);
        genderGroup=findViewById(R.id.genderGroup);
        male=findViewById(R.id.male);
        female=findViewById(R.id.female);
        spinner=findViewById(R.id.spinner);
        hobby1=findViewById(R.id.hobby1);
        hobby2=findViewById(R.id.hobby2);
        hobby3=findViewById(R.id.hobby3);
        hobby4=findViewById(R.id.hobby4);

        btn1=findViewById(R.id.btn1);
        btn2=findViewById(R.id.btn2);
        genderGroup.setonCheckedChangeListener(new RadioGroup.onCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int i) {
                RadioButton rd=findViewById(i);
                sex=rd.getText().toString();
            }
        });
        majorlist=new ArrayList<>();
        majorlist.add("计算机科学与技术");
        majorlist.add("人工智能");
        majorlist.add("软件工程");
        majorlist.add("信息安全");
        majorlist.add("大数据");
        majorlist.add("网络工程");
        ArrayAdapter data=new ArrayAdapter<>(this,
                android.R.layout.simple_spinner_item,majorlist);
        data.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinner.setAdapter(data);
        spinner.setonItemSelectedListener(new AdapterView.onItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView adapterView, View view, int i, long l) {
                major=majorlist.get(i);
            }
            @Override
            public void onNothingSelected(AdapterView adapterView) {

            }
        });
        CompoundButton.onCheckedChangeListener Hobby=new CompoundButton.onCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                hobby="";
                if(hobby1.isChecked())
                    hobby+=hobby1.getText()+",";
                if(hobby2.isChecked())
                    hobby+=hobby2.getText()+",";
                if(hobby3.isChecked())
                    hobby+=hobby3.getText()+",";
                if(hobby4.isChecked())
                    hobby+=hobby4.getText()+",";
            }
        };
        hobby1.setonCheckedChangeListener(Hobby);
        hobby2.setonCheckedChangeListener(Hobby);
        hobby3.setonCheckedChangeListener(Hobby);
        hobby4.setonCheckedChangeListener(Hobby);
        btn1.setonClickListener(new View.onClickListener() {
            @Override
            public void onClick(View view) {
                if(edit.getText().toString().equals(""))
                    toast.makeText(btn1.getContext(), "请输入您的姓名!",Toast.LENGTH_SHORT).show();
                else{
                    info="你好,"+edit.getText().toString()+"!n";
                    info+="你的性别是:"+sex+"!n";
                    info+="你的专业是"+major+"!n";
                    info+="你的个人爱好有:"+hobby.substring(0,hobby.length()-1)+"!n";
                    toast.makeText(btn1.getContext(),info,Toast.LENGTH_SHORT).show();
                }
            }
        });
        btn2.setonClickListener(new View.onClickListener() {
            @Override
            public void onClick(View view) {

            }
        });
    }
}

activity_main.xml 代码如下(示例):



    
        
        
    
    
    
        
        
            
            

            
        
    

    
        
        

        
    
    
    
        
        
    
    
        
        
    


    
    
        

string.xml 代码如下(示例):


    My Application
    音乐
    运动
    游泳
    阅读
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/778115.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

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

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