zoukankan      html  css  js  c++  java
  • 常用组件的综合应用--注册窗口(数值)

    过程中出了点小问题,详情见:https://www.cnblogs.com/Master-Sun/p/14260259.html

    activity_reg.xml

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".RegActivity"
        android:padding="10dp"
        android:orientation="vertical">
    
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">
            <TextView
                android:text="姓名"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"/>
            <EditText
                android:id="@+id/et_reg_name"
                android:maxLines="1"
                android:layout_weight="0.3"
                android:inputType="text"
                android:layout_width="0dp"
                android:layout_height="wrap_content"/>
            <TextView
                android:text="年龄"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"/>
            <EditText
                android:id="@+id/et_reg_age"
                android:maxLines="1"
                android:layout_weight="0.3"
                android:inputType="number"
                android:layout_width="0dp"
                android:layout_height="wrap_content"/>
             <!--weight权重 站的比例-->
    
        </LinearLayout>
    
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">
            <TextView
                android:text="电话"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"/>
            <EditText
                android:id="@+id/et_reg_phone"
                android:maxLines="1"
                android:layout_weight="0.3"
                android:inputType="phone"
                android:layout_width="0dp"
                android:layout_height="wrap_content"/>
            <TextView
                android:text="地址"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"/>
            <EditText
                android:id="@+id/et_reg_address"
                android:maxLines="1"
                android:layout_weight="0.3"
                android:inputType="text"
                android:layout_width="0dp"
                android:layout_height="wrap_content"/>
            <!--weight权重 站的比例-->
    
        </LinearLayout>
    
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:layout_marginTop="20dp">
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="性别:"
                android:layout_gravity="center"/>
            <RadioGroup
                android:id="@+id/re_reg_gender"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal">
                <RadioButton
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:id="@+id/rb_reg_male"
                    android:checked="true"
                    android:text="男"/>
                <RadioButton
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:id="@+id/rb_reg_female"
                    android:text="女"/>
            </RadioGroup>
    
        </LinearLayout>
    
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:layout_marginTop="20dp">
            <TextView
                android:text="爱好:"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"/>
            <CheckBox
                android:id="@+id/cb_reg_sing"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:checked="true"
                android:text="唱"/>
            <CheckBox
                android:id="@+id/cb_reg_dance"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:checked="true"
                android:text="跳"/>
            <CheckBox
                android:id="@+id/cb_reg_rap"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:checked="true"
                android:text="rap"/>
            <CheckBox
                android:id="@+id/cb_reg_basketball"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:checked="true"
                android:text="篮球"/>
    
    
        </LinearLayout>
    
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:layout_marginTop="20dp"
            android:gravity="center_horizontal">
    
            <Button
                android:layout_marginRight="10dp"
                android:id="@+id/btn_reg_submit"
                android:text="提交"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"/>
            <Button
                android:layout_marginLeft="10dp"
                android:id="@+id/btn_reg_cancel"
                android:text="取消"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"/>
    
        </LinearLayout>
        <TextView
            android:id="@+id/tv_reg_content"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>
    
    </LinearLayout>

    RegActivity.java

    package com.androidstudy.uicomponenttest;
    
    import androidx.appcompat.app.AppCompatActivity;
    
    import android.os.Bundle;
    import android.util.Log;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.widget.Button;
    import android.widget.CheckBox;
    import android.widget.CompoundButton;
    import android.widget.EditText;
    import android.widget.LinearLayout;
    import android.widget.RadioButton;
    import android.widget.RadioGroup;
    import android.widget.TextView;
    import android.widget.Toast;
    
    public class RegActivity extends AppCompatActivity implements View.OnClickListener {
        private Button btnSubmit,btnCancel;
        private EditText etName,etAge,etPhone,etAddr;
        private RadioGroup rgGender;
        private CheckBox cbSing,cbDance,cbRap,cbBasketball;
        private TextView tvContent;
        private String gender;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_reg);
            initView();
        }
    
    
        public void initView(){
    //        btnSubmit.findViewById(R.id.btn_reg_submit); //出现问题,
    //        LayoutInflater inflater = (LayoutInflater) LayoutInflater.from(this);//也错了
     //       LinearLayout Reg = (LinearLayout) inflater.inflate(R.layout.activity_reg,null);
    
            btnSubmit=this.findViewById(R.id.btn_reg_submit);
            btnCancel=this.findViewById(R.id.btn_reg_cancel);
    
            btnSubmit.setOnClickListener(this);//监听器注册
            btnCancel.setOnClickListener(this);
    
            etName=this.findViewById(R.id.et_reg_name);
            etAge=this.findViewById(R.id.et_reg_age);
            etPhone=this.findViewById(R.id.et_reg_phone);
            etAddr=this.findViewById(R.id.et_reg_address);
    
            rgGender=this.findViewById(R.id.re_reg_gender);
    
            cbSing=this.findViewById(R.id.cb_reg_sing);
            cbDance=this.findViewById(R.id.cb_reg_dance);
            cbRap=this.findViewById(R.id.cb_reg_rap);
            cbBasketball=this.findViewById(R.id.cb_reg_basketball);
    
            tvContent = this.findViewById(R.id.tv_reg_content);
    
    
            //监听checkbox的状态
            CompoundButton.OnCheckedChangeListener checkedChangeListener = new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    switch (buttonView.getId()){
                        case R.id.cb_reg_sing:
                            if(isChecked){
                                Toast.makeText(RegActivity.this,"唱",Toast.LENGTH_SHORT).show();
                            }
                            break;
                        case R.id.cb_reg_dance:
                            if(isChecked){
                                Toast.makeText(RegActivity.this,"跳",Toast.LENGTH_SHORT).show();
                            }
                            break;
                        case R.id.cb_reg_rap:
                            if(isChecked){
                                Toast.makeText(RegActivity.this,"rap",Toast.LENGTH_SHORT).show();
                            }
                            break;
                        case R.id.cb_reg_basketball:
                            if(isChecked){
                                Toast.makeText(RegActivity.this,"篮球",Toast.LENGTH_SHORT).show();
                            }
                            break;
                    }
                }
            };
            cbSing.setOnCheckedChangeListener(checkedChangeListener);
            cbDance.setOnCheckedChangeListener(checkedChangeListener);
            cbRap.setOnCheckedChangeListener(checkedChangeListener);
            cbBasketball.setOnCheckedChangeListener(checkedChangeListener);
    
    
            //监听单选
            rgGender.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(RadioGroup group, int checkedId) {
                    RadioButton rb = findViewById(checkedId); //被选中的RadioButton的id
                    gender = rb.getText().toString();
                    Log.d("信息", gender);
    
                }
            });
    
    
        }
    
    
        //按钮点击
        @Override
        public void onClick(View v) {
    
            switch (v.getId()){
                case R.id.btn_reg_submit:
    
                    StringBuilder hobby = new StringBuilder();
                    if(cbSing.isChecked()){
                        hobby.append(cbSing.getText().toString()).append("  ");//两个点后面是有顺序的 相当于append里面+ + +
                    }
                    if(cbDance.isChecked()){
                        hobby.append(cbDance.getText().toString()).append("  ");//两个点后面是有顺序的 相当于append里面+ + +
                    }
                    if(cbRap.isChecked()){
                        hobby.append(cbRap.getText().toString()).append("  ");//两个点后面是有顺序的 相当于append里面+ + +
                    }
                    if(cbBasketball.isChecked()){
                        hobby.append(cbBasketball.getText().toString()).append("  ");//两个点后面是有顺序的 相当于append里面+ + +
                    }
                    StringBuilder info = new StringBuilder();
                    info.append("姓名:" + etName.getText().toString()+"
    ");
                    info.append("年龄:" + etAge.getText().toString()+"
    ");
                    info.append("电话:" + etPhone.getText().toString()+"
    ");
                    info.append("地址:" + etAddr.getText().toString()+"
    ");
                    info.append("性别:"+ gender+"
    ");
                    info.append(hobby.toString());
    
                    tvContent.setText(info.toString());
                    
                    break;
                case R.id.btn_reg_cancel:
                    break;
            }
        }
    }
  • 相关阅读:
    搜索
    c++ map与unordered_map区别及使用
    01BFS
    宇智波程序笔记55-Flutter 混合开发】嵌入原生View-iOS
    宇智波程序笔记54-解Bug之路-记一次线上请求偶尔变慢的排查
    宇智波程序笔记53-从红黑树的本质出发,彻底理解红黑树!
    宇智波程序笔记52-最受欢迎的微服务框架概览
    宇智波程序笔记51-JDK 15安装及新特性介绍
    宇智波程序笔记50-解Bug之路-记一次线上请求偶尔变慢的排查
    宇智波程序笔记49-link JDBC Connector:Flink 与数据库集成最佳实践
  • 原文地址:https://www.cnblogs.com/Master-Sun/p/14260238.html
Copyright © 2011-2022 走看看