3.23

所花时间(包括上课):3.5

打码量(行):650

博客量(篇):1

了解到知识点:学习单选按钮和编辑框

<!-- activity_main.xml -->

<RadioGroup

    android:id="@+id/radio_group"

    android:layout_width="wrap_content"

    android:layout_height="wrap_content">

 

    <RadioButton

        android:id="@+id/radio_button1"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:text="Option 1"

        android:checked="true" />

 

    <RadioButton

        android:id="@+id/radio_button2"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:text="Option 2" />

 

    <RadioButton

        android:id="@+id/radio_button3"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:text="Option 3" />

</RadioGroup>

import android.os.Bundle;

import android.widget.RadioButton;

import android.widget.RadioGroup;

import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

 

public class MainActivity extends AppCompatActivity {

 

    private RadioGroup radioGroup;

    private RadioButton radioButton1, radioButton2, radioButton3;

 

    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

 

        radioGroup = findViewById(R.id.radio_group);

        radioButton1 = findViewById(R.id.radio_button1);

        radioButton2 = findViewById(R.id.radio_button2);

        radioButton3 = findViewById(R.id.radio_button3);

 

        // 设置单选按钮选择事件的监听器

        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {

            @Override

            public void onCheckedChanged(RadioGroup group, int checkedId) {

                RadioButton checkedRadioButton = findViewById(checkedId);

                String selectedOption = checkedRadioButton.getText().toString();

                Toast.makeText(MainActivity.this, "Selected option: " + selectedOption, Toast.LENGTH_SHORT).show();

            }

        });

    }

}

<!-- activity_main.xml -->

<EditText

    android:id="@+id/edit_text"

    android:layout_width="match_parent"

    android:layout_height="wrap_content"

    android:hint="Enter your name" />

import android.os.Bundle;

import android.text.Editable;

import android.text.TextWatcher;

import android.widget.EditText;

import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

 

public class MainActivity extends AppCompatActivity {

 

    private EditText editText;

 

    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

 

        editText = findViewById(R.id.edit_text);

 

        // 设置编辑框文本变化的监听器

        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) {

                String enteredText = editText.getText().toString();

                Toast.makeText(MainActivity.this, "Entered text: " + enteredText, Toast.LENGTH_SHORT).show();

            }

        });

    }

}

posted @ 2024-03-23 15:59  赵千万  阅读(2)  评论(0编辑  收藏  举报