1 <?xml version="1.0" encoding="utf-8"?>
2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
3 xmlns:tools="http://schemas.android.com/tools"
4 android:layout_width="match_parent"
5 android:layout_height="match_parent"
6 tools:context=".MainActivity"
7 android:orientation="vertical">
8
9
10 <!--Multiple selection box-->
11<CheckBox
12 android:id="@ + id/cb_cg"
13 android:layout_width="wrap_content"
14 android:layout_height="wrap_content"
15 android:text="Singing"
16 android:checked="true"
17>
18 </CheckBox>
19 <CheckBox
20 android:id="@ + id/cb_gm"
21 android:layout_width="wrap_content"
22 android:layout_height="wrap_content"
23 android:text="playing games"
24 android:checked="false"
25>
26 </CheckBox>
27
28
29 <!-- The layout file calls the OnClick function: -->
30 <Button
31 android:layout_width="wrap_content"
32 android:layout_height="wrap_content"
33 android:background="@drawable/select_login_btn"
34 android:onClick="login"
35 />
36 <!--Anonymous inner class method-->
37 <Button
38 android:id="@ + id/btn2"
39 android:layout_width="wrap_content"
40 android:layout_height="wrap_content"
41 android:textSize="30sp"
42 android:text="Anonymous inner class method"
43 />
44
45 <EditText
46 android:id="@ + id/ed01"
47 android:layout_width="match_parent"
48 android:layout_height="wrap_content"
49 android:hint="(Prompt content) Only numbers can be entered"
50 android:inputType="number"
51 android:text=""
52 android:textColor="@android:color/background_dark"
53 android:textColorHint="@android:color/holo_green_light" />
54
55 <EditText
56 android:id="@ + id/ed02"
57 android:layout_width="match_parent"
58 android:layout_height="wrap_content"
59 android:hint="Enter password"
60 android:text=""
61 android:textColor="@android:color/background_dark"
62 android:textColorHint="@android:color/holo_green_light"
63 />
64 <ImageView
65 android:layout_width="60dp"
66 android:layout_height="wrap_content"
67 android:background="@android:drawable/btn_dialog"
68 />
69
70 <!--
71 inputType
72 number: number
73 numberPassword:Number password
74 text: text
75 password: text password
76 phone: mobile phone number
77 email:mailbox
78 date: date
79 -->
80 <RadioGroup
81 android:id="@ + id/rg_gender"
82 android:layout_width="wrap_content"
83 android:layout_height="wrap_content"
84 android:orientation="vertical"
85 >
86 <RadioButton
87 android:id="@ + id/male"
88 android:layout_width="wrap_content"
89 android:layout_height="wrap_content"
90 android:text="male"
91 />
92 <RadioButton
93 android:id="@ + id/female"
94 android:layout_width="wrap_content"
95 android:layout_height="wrap_content"
96 android:text="female"/>
97 </RadioGroup>
98 <!-- RadioButton and RadioGroup must be used together -->
99 <RadioGroup
100 android:layout_width="wrap_content"
101 android:layout_height="wrap_content"
102 android:orientation="horizontal"
103 >
104 <RadioButton
105 android:layout_width="wrap_content"
106 android:layout_height="wrap_content"
107 android:text="2000"
108 />
109 <RadioButton
110 android:layout_width="wrap_content"
111 android:layout_height="wrap_content"
112 android:text="2001"/>
113 </RadioGroup>
114
115
116
117
118 </LinearLayout>
Activity
1 package com.example.myapplication;
2
3 import android.os.Bundle;
4 import android.util.Log;
5 import android.view.View;
6 import android.widget.Button;
7 import android.widget.CheckBox;
8 import android.widget.CompoundButton;
9 import android.widget.EditText;
10 import android.widget.RadioGroup;
11 import android.widget.Toast;
12
13 import androidx.appcompat.app.AppCompatActivity;
14
15 public class MainActivity extends AppCompatActivity implements RadioGroup.OnCheckedChangeListener {
16
17
18 @Override
19 protected void onCreate(Bundle savedInstanceState) {
20 super.onCreate(savedInstanceState);
21 setContentView(R.layout.activity_btn);
22 final Button btn2;
23 final EditText ed01=findViewById(R.id.ed01);
24 final EditText ed02=findViewById(R.id.ed02);
25 btn2 = findViewById(R.id.btn2);
26 btn2.setOnClickListener(new View.OnClickListener() {
27 public void onClick(View v) {
28 Log.i("MainActivity", "Anonymous inner class button was clicked");
29 Log.i("ed01", ed01.getText().toString());
30 Log.i("ed02", ed02.getText().toString());
31/*
32 <!--Using toast -->
33 1 The prompt box created by makeText(context, text, duration) is not displayed. You must add the .show() method to display it.
34
35 (1)context
36 must be an activity
37 Just write Activity.this
38 context
39 Everything visible to the eye is related to context
40 (2)text
41 displayed content
42 (3)duration
43 Show time
44 Toast.LENGTH.SHORT: 1 second, the actual value is 0
45 LENGTH.LONG: 2 seconds, the actual value is 1 second
46 2 show()
47 Show prompt box
48 */
49 Toast.makeText(MainActivity.this,"This is the displayed content",Toast.LENGTH_SHORT).show();
50 }
51 });
52 RadioGroup rg_gender = findViewById(R.id.rg_gender);
53 final CheckBox cb_cg=findViewById(R.id.cb_cg);
54 cb_cg.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
55 @Override
56 public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
57/*
58 buttonView: the clicked control
59 isChecked: Whether the check box is checked
60
61 */
62 Log.i("onCheckedChanged",cb_cg.getText().toString() + (isChecked?"selected":"unchecked"));
63}
64 });
65 rg_gender.setOnCheckedChangeListener(this);
66
67 }
68
69 public void login(View v){
70 Log.i("MainActivity","Button clicked");
71 }
72
73 @Override
74 public void onCheckedChanged(RadioGroup group, int checkedId) {
75 switch (checkedId)
76 {
77 case R.id.male:
78 Log.i("onCheckedChanged","Male");
79 case R.id.female:
80 Log.i("OnCheckedChanged","Female");
81 }
82}
83
84
85
86}
87/*
88 java variables
89 1. Function: Save data, easy to use
90 2. Scope: valid range, braces
91 3. Variable classification:
92 1.Attributes
93 instance properties
94 format:
95 Permission modifier Return value type Method name (method parameter list) {Method body}
96 static properties
97 constants
98 2. Local variables
99 */
100
101
102/*
103 Button click event
104 1. Can only be written in java files
105 2. How to click the time
106 (1)public void method name (View v)
107 The method name can be customized
108 */
package com.example.myapplication;<br><br>import android.os.Bundle;<br>import android.util.Log;<br>import android.view.View;<br>import android.widget.Button;<br>import android.widget.CheckBox;<br>import android.widget.CompoundButton;<br>import android.widget.EditText;<br>import android.widget.RadioGroup;<br>import android.widget.Toast;<br><br>import androidx.appcompat.app.AppCompatActivity;<br><br>public class MainActivity extends AppCompatActivity implements RadioGroup.OnCheckedChangeListener {<!-- --><br><br><br> @Override<br>protected void onCreate(Bundle savedInstanceState) {<!-- --><br> super.onCreate(savedInstanceState);<br>setContentView(R.layout.activity_btn);<br> final Button btn2;<br> final EditText ed01=findViewById(R.id.ed01);<br> final EditText ed02=findViewById(R.id.ed02);<br>btn2 = findViewById(R.id.btn2);<br>btn2.setOnClickListener(new View.OnClickListener() {<!-- --><br> public void onClick(View v) {<!-- --><br> Log.i("MainActivity", "Anonymous inner class button was clicked");<br>Log.i("ed01", ed01.getText().toString());<br>Log.i("ed02", ed02.getText().toString());<br>/*<br> <!-- How to use toast --><br> 1 The prompt box created by makeText(context, text, duration) is not displayed. You must add the .show() method to display it.<br><br> (1)context<br> Must be an activity<br> Just write Activity.this<br> context<br> Everything visible to the eye is related to context<br> (2)text<br> Displayed content<br> (3)duration<br> display time<br> Toast.LENGTH.SHORT: 1 second, the actual value is 0<br> LENGTH.LONG: 2 seconds, the actual value is 1 second<br> 2 show()<br> Show tooltip<br> */<br>Toast.makeText(MainActivity.this,"This is the displayed content",Toast.LENGTH_SHORT).show();<br>}<br> });<br>RadioGroup rg_gender = findViewById(R.id.rg_gender);<br> final CheckBox cb_cg=findViewById(R.id.cb_cg);<br>cb_cg.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {<!-- --><br> @Override<br>public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {<!-- --><br> /*<br> buttonView: the clicked control<br> isChecked: Whether the check box is checked<br><br> */<br>Log.i("onCheckedChanged",cb_cg.getText().toString() + (isChecked?"selected":"unchecked"));<br>}<br> });<br>rg_gender.setOnCheckedChangeListener(this);<br><br>}<br><br> public void login(View v){<!-- --><br> Log.i("MainActivity","Button clicked");<br>}<br><br> @Override<br>public void onCheckedChanged(RadioGroup group, int checkedId) {<!-- --><br> switch(checkedId)<br> {<!-- --><br> case R.id.male:<br> Log.i("onCheckedChanged","Male");<br> case R.id.female:<br> Log.i("OnCheckedChanged","Female");<br>}<br> }<br><br><br><br>}<br> /*<br> java variables<br> 1. Function: Save data, easy to use<br> 2. Scope: valid range, braces<br> 3. Variable classification:<br> 1.Attributes<br> Instance properties<br> Format:<br> Permission modifier Return value type Method name (method parameter list) {Method body}<br> static properties<br> constant<br> 2. Local variables<br> */<br><br><br>/*<br> Button click event<br> 1. Can only be written in java files<br> 2. How to click the time<br> (1)public void method name (View v)<br> Method name can be customized<br> */<br>