Android:week 6总结 EditText、单选框、复选框

目录

 

 

1.EditText

2.单选框(RadioButton、RadioGroup)

3.复选框 (CheckBox)


 

1.EditText

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">
<!--ImageView 需要放在前面,不然会覆盖掉文字-->
<!--    match_parent  background 背景-->
<!--    wrap_content  src 前景图片-->
    <ImageView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@mipmap/bg">
    </ImageView>
    <EditText
        android:id="@+id/et1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        android:textSize="20sp"
        android:hint="输入姓名"
        android:inputType="text">
    </EditText>
    <TextView
        android:id="@+id/tv1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="用户名"
        android:textSize="20sp"
        app:layout_constraintRight_toLeftOf="@id/et1"
        app:layout_constraintBaseline_toBaselineOf="@id/et1"
        android:layout_marginRight="10sp"/>

    <EditText
        android:id="@+id/et2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintBaseline_toBaselineOf="@+id/tv2"
        app:layout_constraintLeft_toLeftOf="@+id/et1"
        android:textSize="20sp"
        android:hint="输入密码"
        android:inputType="textPassword">
    </EditText>
    <TextView
        android:id="@+id/tv2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="密    码"
        android:textSize="20sp"
        app:layout_constraintLeft_toLeftOf="@id/tv1"
        app:layout_constraintTop_toBottomOf="@id/tv1"
        android:layout_marginRight="10sp"/>

    <TextView
        android:id="@+id/tv3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="20sp"
        app:layout_constraintBottom_toTopOf="@id/tv1"
        app:layout_constraintLeft_toLeftOf="@id/tv1"
        android:layout_marginTop="10sp" />

    <!--前景图片-->
    <ImageView
        android:layout_width="60sp"
        android:layout_height="60sp"
        app:layout_constraintBottom_toTopOf="@id/tv1"
        app:layout_constraintLeft_toLeftOf="@id/tv1"
        android:src="@mipmap/pic">
    </ImageView>
</androidx.constraintlayout.widget.ConstraintLayout>

将输入内容同步显示在TextView中

package com.example.myapplication;

import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.widget.EditText;
import android.widget.CheckBox;
import android.widget.RadioGroup;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity{
    private EditText editText;
    private TextView textView;
    private RadioGroup radioGroup;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        editText = findViewById(R.id.et1);
        textView = findViewById(R.id.tv3); //把信息显示在这里

//        匿名内部类
        editText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }
            //获取完成信息
            @Override
            public void afterTextChanged(Editable s) {
                textView.setText("input:  "+s.toString());
            }
        });


    }
}

 

2.单选框(RadioButton、RadioGroup)

选项垂直放置或水平放置
android:orientation="horizontal"

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">
<!--ImageView 需要放在前面,不然会覆盖掉文字-->
<!--    match_parent  background 背景-->
<!--    wrap_content  src 前景图片-->
    <ImageView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@mipmap/bg">
    </ImageView>
    <EditText
        android:id="@+id/et1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        android:textSize="20sp"
        android:hint="输入姓名"
        android:inputType="text">
    </EditText>
    <TextView
        android:id="@+id/tv1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="用户名"
        android:textSize="20sp"
        app:layout_constraintRight_toLeftOf="@id/et1"
        app:layout_constraintBaseline_toBaselineOf="@id/et1"
        android:layout_marginRight="10sp"/>

    <EditText
        android:id="@+id/et2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintBaseline_toBaselineOf="@+id/tv2"
        app:layout_constraintLeft_toLeftOf="@+id/et1"
        android:textSize="20sp"
        android:hint="输入密码"
        android:inputType="textPassword">
    </EditText>
    <TextView
        android:id="@+id/tv2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="密    码"
        android:textSize="20sp"
        app:layout_constraintLeft_toLeftOf="@id/tv1"
        app:layout_constraintTop_toBottomOf="@id/tv1"
        android:layout_marginRight="10sp"/>

    <TextView
        android:id="@+id/tv3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="20sp"
        app:layout_constraintBottom_toTopOf="@id/tv1"
        app:layout_constraintLeft_toLeftOf="@id/tv1"
        android:layout_marginTop="10sp" />

    <!--前景图片-->
    <ImageView
        android:layout_width="60sp"
        android:layout_height="60sp"
        app:layout_constraintBottom_toTopOf="@id/tv1"
        app:layout_constraintRight_toLeftOf="@id/tv1"
        android:src="@mipmap/pic">
    </ImageView>
    <RadioGroup
        android:id="@+id/rg"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintTop_toBottomOf="@id/tv2"
        app:layout_constraintLeft_toLeftOf="@id/tv2"
        android:layout_marginTop="10sp"
        android:orientation="horizontal">
        <RadioButton
            android:id="@+id/rb1"
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            android:text="Girl"
            android:textSize="20sp">
        </RadioButton>

        <RadioButton
            android:id="@+id/rb2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Boy"
            android:textSize="20sp">
        </RadioButton>
    </RadioGroup>
</androidx.constraintlayout.widget.ConstraintLayout>
package com.example.myapplication;

import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.widget.EditText;
import android.widget.CheckBox;
import android.widget.RadioGroup;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity implements  RadioGroup.OnCheckedChangeListener,TextWatcher{
    private EditText editText;
    private TextView textView;
    private RadioGroup radioGroup;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        editText = findViewById(R.id.et1);
        textView = findViewById(R.id.tv3); //把信息显示在这里
        radioGroup = findViewById(R.id.rg);
        //监听事件
        editText.addTextChangedListener(this);
        radioGroup.setOnCheckedChangeListener(this);

 (获取用户名名称)    方法一:匿名内部类
       /* editText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }
            //获取完成信息
            @Override
            public void afterTextChanged(Editable s) {
                textView.setText("input:  "+s.toString());
            }
        });*/

 (获取选项内容)      方法一:匿名内部类
      /* radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                if(checkedId==R.id.rb1)
                    textView.setText("choosed: Gril");
                else
                    textView.setText("choosed: Boy");
            }
        });*/
    }

 (获取用户名名称)    方法二:匿名内部类   [ 需要implements+TextWatcher]
    //所有的重载需要全部实现
    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) { }
    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) { }
    @Override
    public void afterTextChanged(Editable s) {
        textView.setText("input:  "+s.toString());
    }

 (获取选项内容)      方法二:实现接口    [ 需要implements+RadioGroup.OnCheckedChangeListener]
    //一般建议用接口来实现
    @Override
    public void onCheckedChanged(RadioGroup groupmint ,int checkedId) {
        switch (checkedId) {
            case R.id.rb1:
                textView.setText("choosed: Gril");
                break;
            case R.id.rb2:
                textView.setText("choosed: Boy");
                break;
        }
    }

}

3.复选框 (CheckBox)

 

package com.example.myapplication;

import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.CheckBox;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity implements  RadioGroup.OnCheckedChangeListener,TextWatcher,CompoundButton.OnCheckedChangeListener{
    private EditText editText;
    private TextView textView;
    private RadioGroup radioGroup;
    private CompoundButton compoundButton;
    private CheckBox checkBox1,checkBox2;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        editText = findViewById(R.id.et1);
        textView = findViewById(R.id.tv3); //把信息显示在这里
        radioGroup = findViewById(R.id.rg);
        compoundButton = findViewById(R.id.cb1);
        checkBox1 = findViewById(R.id.cb1);
        checkBox2 = findViewById(R.id.cb2);
        //监听事件
        editText.addTextChangedListener(this);
        radioGroup.setOnCheckedChangeListener(this);
        checkBox1.setOnCheckedChangeListener(this);
        checkBox2.setOnCheckedChangeListener(this);
(显示复选框内容)     方法一:匿名内部类     但是只能显示其中一个
       /* checkBox1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if(isChecked)
                        textView.setText("Choosed: 篮球");
            }
        });
        checkBox2.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if(isChecked)
                    textView.setText("Choosed: 足球");
            }
        });*/

 (获取用户名名称)    方法一:匿名内部类
       /* editText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }
            //获取完成信息
            @Override
            public void afterTextChanged(Editable s) {
                textView.setText("input:  "+s.toString());
            }
        });*/

 (获取选项内容)      方法一:匿名内部类
      /* radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                if(checkedId==R.id.rb1)
                    textView.setText("choosed: Gril");
                else
                    textView.setText("choosed: Boy");
            }
        });*/
    }

 (获取用户名名称)    方法二:匿名内部类   [ 需要implements+TextWatcher]
    //所有的重载需要全部实现
    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) { }
    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) { }
    @Override
    public void afterTextChanged(Editable s) {
        textView.setText("input:  "+s.toString());
    }

 (获取选项内容)      方法二:实现接口    [ 需要implements+RadioGroup.OnCheckedChangeListener]
    //一般建议用接口来实现
    @Override
    public void onCheckedChanged(RadioGroup groupmint ,int checkedId) {
        switch (checkedId) {
            case R.id.rb1:
                textView.setText("choosed: Gril");
                break;
            case R.id.rb2:
                textView.setText("choosed: Boy");
                break;
        }
    }

    String str =new String();

(获取复选框内容)     方法二:通过继承实现    [ 需要implements+ CompoundButton.OnCheckedChangeListener]
    @Override
    public void onCheckedChanged(CompoundButton button,boolean isChecked)
    {
        String text = button.getText().toString();
        if(isChecked) {
            if(!str.contains(text))
                str = str +" "+text;
        }
        else {
            if(str.contains(text))
                str = str.replace(text,"");
        }
        //textView.setText(str);

        //提示信息
        Toast.makeText(this,str,Toast.LENGTH_LONG).show();
    }
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,以下是一个含有单选框复选框Android Studio注册样式的代码示例: ``` <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <EditText android:id="@+id/editTextEmail" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="Email" /> <EditText android:id="@+id/editTextPassword" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="Password" android:inputType="textPassword" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Gender" /> <RadioGroup android:id="@+id/radioGroupGender" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <RadioButton android:id="@+id/radioButtonMale" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Male" /> <RadioButton android:id="@+id/radioButtonFemale" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Female" /> </RadioGroup> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Hobbies" /> <CheckBox android:id="@+id/checkBoxReading" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Reading" /> <CheckBox android:id="@+id/checkBoxTravelling" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Travelling" /> <Button android:id="@+id/buttonRegister" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Register" /> </LinearLayout> ``` 在这个布局中,我们使用了一个EditText来输入邮箱和密码,一个RadioGroup来选择性别(男/女),以及两个CheckBox来选择爱好(阅读/旅行)。最后,我们使用了一个Button来注册。你可以根据需要进行修改和调整。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值