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();
    }
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章