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;
            }
        }
    }
  • 相关阅读:
    Code First 二 DataAnnotation 数据注解
    Code First 一
    LINQ 方法
    Leetcode练习(Python):栈类:第173题:二叉搜索树迭代器:实现一个二叉搜索树迭代器。你将使用二叉搜索树的根节点初始化迭代器。 调用 next() 将返回二叉搜索树中的下一个最小的数。
    Leetcode练习(Python):栈类:第103题:二叉树的锯齿形层次遍历:给定一个二叉树,返回其节点值的锯齿形层次遍历。(即先从左往右,再从右往左进行下一层遍历,以此类推,层与层之间交替进行)。
    Leetcode练习(Python):栈类:第150题:逆波兰表达式求值:根据逆波兰表示法,求表达式的值。 有效的运算符包括 +, -, *, / 。每个运算对象可以是整数,也可以是另一个逆波兰表达式。
    Leetcode练习(Python):栈类:第145题:二叉树的后序遍历:给定一个二叉树,返回它的 后序 遍历。
    Leetcode练习(Python):栈类:第144题:二叉树的前序遍历:给定一个二叉树,返回它的 前序 遍历。
    Leetcode练习(Python):栈类:第225题:用队列实现栈:使用队列实现栈的下列操作: push(x) -- 元素 x 入栈 pop() -- 移除栈顶元素 top() -- 获取栈顶元素 empty() -- 返回栈是否为空
    Leetcode练习(Python):栈类:用栈实现队列:使用栈实现队列的下列操作: push(x) -- 将一个元素放入队列的尾部。 pop() -- 从队列首部移除元素。 peek() -- 返回队列首部的元素。 empty() -- 返回队列是否为空。
  • 原文地址:https://www.cnblogs.com/Master-Sun/p/14260238.html
Copyright © 2011-2022 走看看