復習AndroidStudio復選控件、單選控件、文本控件以及activity數據傳送以及回傳練習(日常作業練習)


 

 

 

復習AndroidStudio復選控件、單選控件、文本控件以及activity數據傳送以及回傳練習(日常作業練習)

 

 

                                                     ————安德風

 

 

一、單選按鈕組交互小練習

1、效果演示:

 

 

2、activity_main.xml布局設計代碼

 

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     xmlns:app="http://schemas.android.com/apk/res-auto"
 4     xmlns:tools="http://schemas.android.com/tools"
 5     android:layout_width="match_parent"
 6     android:layout_height="match_parent"
 7     tools:context=".MainActivity">
 8 
 9     <TextView
10         android:id="@+id/textView"
11         android:layout_width="wrap_content"
12         android:layout_height="wrap_content"
13         android:text="@string/ts"
14         android:textSize="24sp"
15         app:layout_constraintBottom_toBottomOf="parent"
16         app:layout_constraintHorizontal_bias="0.107"
17         app:layout_constraintLeft_toLeftOf="parent"
18         app:layout_constraintRight_toRightOf="parent"
19         app:layout_constraintTop_toTopOf="parent"
20         app:layout_constraintVertical_bias="0.064" />
21 
22     <EditText
23         android:id="@+id/et"
24         android:layout_width="242dp"
25         android:layout_height="55dp"
26         android:layout_marginStart="84dp"
27         android:layout_marginLeft="84dp"
28         android:layout_marginTop="16dp"
29         android:ems="10"
30         android:inputType="textPersonName"
31         app:layout_constraintStart_toStartOf="parent"
32         app:layout_constraintTop_toBottomOf="@+id/textView" />
33 
34     <RadioGroup
35         android:id="@+id/rg"
36         android:layout_width="wrap_content"
37         android:layout_height="wrap_content"
38         android:layout_marginStart="40dp"
39         android:layout_marginLeft="40dp"
40         android:layout_marginTop="28dp"
41         app:layout_constraintStart_toStartOf="parent"
42         app:layout_constraintTop_toBottomOf="@+id/et">
43 
44         <RadioButton
45             android:id="@+id/nan"
46             android:layout_width="match_parent"
47             android:layout_height="wrap_content"
48             android:text="@string/nan"
49             android:textSize="24sp" />
50 
51         <RadioButton
52             android:id="@+id/nv"
53             android:layout_width="match_parent"
54             android:layout_height="wrap_content"
55             android:text="@string/nv"
56             android:textSize="24sp" />
57     </RadioGroup>
58 
59     <Button
60         android:id="@+id/bt"
61         android:layout_width="wrap_content"
62         android:layout_height="wrap_content"
63         android:layout_marginStart="40dp"
64         android:layout_marginLeft="40dp"
65         android:layout_marginTop="24dp"
66         android:text="@string/queding"
67         app:layout_constraintStart_toStartOf="parent"
68         app:layout_constraintTop_toBottomOf="@+id/rg" />
69 
70     <TextView
71         android:id="@+id/tv"
72         android:layout_width="wrap_content"
73         android:layout_height="wrap_content"
74         android:layout_marginStart="40dp"
75         android:layout_marginLeft="40dp"
76         android:layout_marginTop="36dp"
77         android:hint="@string/show"
78         android:textSize="24sp"
79         app:layout_constraintStart_toStartOf="parent"
80         app:layout_constraintTop_toBottomOf="@+id/bt" />
81 
82 </android.support.constraint.ConstraintLayout>

 

3、MainActivity功能實現代碼

 1 package com.example.myapplication;
 2 
 3 import android.support.v7.app.AppCompatActivity;
 4 import android.os.Bundle;
 5 import android.view.View;
 6 import android.widget.Button;
 7 import android.widget.EditText;
 8 import android.widget.RadioButton;
 9 import android.widget.RadioGroup;
10 import android.widget.TextView;
11 
12 public class MainActivity extends AppCompatActivity implements RadioGroup.OnCheckedChangeListener, View.OnClickListener {
13 EditText et;//聲明輸入姓名文本控件變量為et
14 RadioGroup rg;//聲明單選組控件變量為rg
15 RadioButton nan,nv;//聲明單選按鈕1單選按鈕2變量分別為nan、nv
16 Button bt;//聲明確定按鈕變量為rg
17 TextView tv;//聲明輸出演示文本變量為rg
18 String radio="";//聲明用戶單選存放值變量為rg
19     @Override
20     protected void onCreate(Bundle savedInstanceState) {
21         super.onCreate(savedInstanceState);
22         setContentView(R.layout.activity_main);
23 
24         et=findViewById(R.id.et);//尋找輸入姓名文本框ID
25         rg=findViewById(R.id.rg);//尋找單選組控件ID
26         nan=findViewById(R.id.nan);//尋找單選按鈕1控件ID
27         nv=findViewById(R.id.nv);//尋找單選按鈕2控件ID
28         bt=findViewById(R.id.bt);//尋找確定按鈕控件ID
29         tv=findViewById(R.id.tv);//尋找輸出文本控件ID
30 
31 
32         rg.setOnCheckedChangeListener(this);//給單選組控件建立監聽器(OnCheckedChangeListener)
33         bt.setOnClickListener(this);//給確定按鈕控件建立監聽器(OnClickListener)
34     }
35 
36 
37     //實現用戶單選按鈕功能
38     @Override
39     public void onCheckedChanged(RadioGroup group, int checkedId) {
40       if (nan.isChecked()){                     //判斷是否選中單選按鈕1為男的控件
41           radio+=nan.getText().toString();}     //如果選中了單選按鈕1為男控件,並將獲得該控件的文本屬性內容為“男”作為后面tv(輸出文本控件)輸出
42       if (nv.isChecked()){                      //判斷是否選中單選按鈕2為女的控件
43           radio+=nv.getText().toString();}      //如果選中了單選按鈕2為女控件,並將獲得該控件的文本屬性內容為“女”作為后面tv(輸出文本控件)輸出
44 
45     }
46 
47     //實現點擊按鈕輸出tv(文本控件顯示功能)
48     @Override
49     public void onClick(View v) {
50         String name=et.getText().toString();     //得到用戶輸入文本框(et)姓名
51         tv.setText("您輸入的信息為:\n"+"姓名:"+name+"\n性別:"+radio);//tv(輸出文本框)輸出顯示的內容(用戶輸入的名字內容以及用戶選中的單選內容)
52     }
53 }

二、復選按鈕組交互小練習

1、效果演示

 

2、activity_main.xml布局設計代碼

 

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     xmlns:app="http://schemas.android.com/apk/res-auto"
 4     xmlns:tools="http://schemas.android.com/tools"
 5     android:layout_width="match_parent"
 6     android:layout_height="match_parent"
 7     android:background="#E0FFFF"
 8     tools:context=".MainActivity">
 9 
10     <TextView
11         android:id="@+id/textView2"
12         android:layout_width="wrap_content"
13         android:layout_height="wrap_content"
14         android:hint="@string/choose"
15         android:textColor="#FCFCFC"
16         android:textSize="24sp"
17         app:layout_constraintBottom_toBottomOf="parent"
18         app:layout_constraintHorizontal_bias="0.051"
19         app:layout_constraintLeft_toLeftOf="parent"
20         app:layout_constraintRight_toRightOf="parent"
21         app:layout_constraintTop_toTopOf="parent"
22         app:layout_constraintVertical_bias="0.032" />
23 
24     <CheckBox
25         android:id="@+id/cb1"
26         android:layout_width="wrap_content"
27         android:layout_height="wrap_content"
28         android:layout_marginStart="40dp"
29         android:layout_marginLeft="40dp"
30         android:layout_marginTop="24dp"
31 
32         android:text="@string/gq_1"
33         android:textColor="#9E9E9E"
34         android:textSize="24sp"
35         app:layout_constraintStart_toStartOf="parent"
36         app:layout_constraintTop_toBottomOf="@+id/textView2" />
37 
38     <CheckBox
39         android:id="@+id/cb2"
40         android:layout_width="wrap_content"
41         android:layout_height="wrap_content"
42         android:layout_marginStart="40dp"
43         android:layout_marginLeft="40dp"
44         android:layout_marginTop="12dp"
45 
46         android:text="@string/gq_2"
47         android:textColor="#9E9E9E"
48         android:textSize="24sp"
49         app:layout_constraintStart_toStartOf="parent"
50         app:layout_constraintTop_toBottomOf="@+id/cb1" />
51 
52     <CheckBox
53         android:id="@+id/cb3"
54         android:layout_width="wrap_content"
55         android:layout_height="wrap_content"
56         android:layout_marginStart="40dp"
57         android:layout_marginLeft="40dp"
58         android:layout_marginTop="12dp"
59 
60         android:text="@string/gq_3"
61         android:textColor="#9E9E9E"
62         android:textSize="24sp"
63         app:layout_constraintStart_toStartOf="parent"
64         app:layout_constraintTop_toBottomOf="@+id/cb2" />
65 
66     <Button
67         android:id="@+id/bt"
68         android:layout_width="wrap_content"
69         android:layout_height="wrap_content"
70         android:layout_marginStart="44dp"
71         android:layout_marginLeft="44dp"
72         android:layout_marginTop="24dp"
73         android:text="@string/button"
74         android:textColor="#9E9E9E"
75         android:textSize="24sp"
76         app:layout_constraintStart_toStartOf="parent"
77         app:layout_constraintTop_toBottomOf="@+id/cb3" />
78 
79     <TextView
80         android:id="@+id/tv"
81         android:layout_width="280dp"
82         android:layout_height="152dp"
83         android:layout_marginStart="44dp"
84         android:layout_marginLeft="44dp"
85         android:layout_marginTop="8dp"
86         android:hint="@string/show"
87         android:textSize="24sp"
88         app:layout_constraintStart_toStartOf="parent"
89         app:layout_constraintTop_toBottomOf="@+id/bt" />
90 
91 </android.support.constraint.ConstraintLayout>

 

3、MainActivity功能實現代碼

 1 package com.example.myapp;
 2 
 3 import android.support.v7.app.AppCompatActivity;
 4 import android.os.Bundle;
 5 import android.view.View;
 6 import android.widget.Button;
 7 import android.widget.CheckBox;
 8 import android.widget.CompoundButton;
 9 import android.widget.TextView;
10 
11 public class MainActivity extends AppCompatActivity implements CompoundButton.OnCheckedChangeListener {
12     CheckBox cb1, cb2, cb3;//聲明復選控件1、2 、 3變量分別為cb1, cb2, cb3
13     Button bt;//聲明獲得選項值普通按鈕變量為bt
14     TextView tv;//聲明輸出文本控件變量為tv
15     private String gq1 = " ", gq2 = "", gq3 = "";
16     //聲明私有化字符歌曲1、歌曲2、歌曲3變量分別為gq1、gq2、gq3,便於后面存放數據
17     @Override
18     protected void onCreate(Bundle savedInstanceState) {
19         super.onCreate(savedInstanceState);
20         setContentView(R.layout.activity_main);
21 
22 
23         cb1=findViewById(R.id.cb1);//尋找復選按鈕1控件ID
24         cb2=findViewById(R.id.cb2);//尋找復選按鈕2控件ID
25         cb3=findViewById(R.id.cb3);//尋找復選按鈕3控件ID
26         bt=findViewById(R.id.bt);//尋找獲得選項值普通按鈕控件ID
27         tv=findViewById(R.id.tv);//尋找輸出文本控件ID
28 
29         cb1.setOnCheckedChangeListener(this);//給復選按鈕1控件建立監聽器(OnCheckedChangeListener)
30         cb2.setOnCheckedChangeListener(this);//給復選按鈕2控件建立監聽器(OnCheckedChangeListener)
31         cb3.setOnCheckedChangeListener(this);//給復選按鈕3控件建立監聽器(OnCheckedChangeListener)
32 
33 
34         bt.setOnClickListener(new View.OnClickListener() {//給獲得選項值普通按鈕建立監聽器(OnClickListener)並創建方法
35             @Override
36             public void onClick(View view) {              //實現點擊按鈕輸出文本內容
37             tv.setText("您選中的歌曲:\n"+gq1+"\n"+gq2+"\n"+gq3+"\n");
38             }
39         });
40 
41 
42     }
43 //實現復選安鈕交互功能
44     @Override
45     public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
46         switch (compoundButton.getId()){    //通過多分支選擇用ID作為判斷
47             case R.id.cb1:
48             if (b==true){
49                 gq1+=cb1.getText().toString();break;}    //如果選中的是復選按鈕1,則或者改按鈕對應的文本屬性內容,並將值賦值給gq1自變量;便於后面文本輸出
50 
51 
52             case R.id.cb2:
53                 if (b==true){
54                     gq2+=cb2.getText().toString();break;} //如果選中的是復選按鈕2,則或者改按鈕對應的文本屬性內容,並將值賦值給gq1自變量;便於后面文本輸出
55 
56 
57             case R.id.cb3:
58                 if (b==true){
59                     gq3+=cb3.getText().toString();break;} //如果選中的是復選按鈕3,則或者改按鈕對應的文本屬性內容,並將值賦值給gq1自變量;便於后面文本輸出
60 
61         }
62     }
63 }

4、res/values/color.xml界面演示設計

1 <?xml version="1.0" encoding="utf-8"?>
2 <resources>
3     <color name="colorPrimary">#B2DFEE</color>
4     <color name="colorPrimaryDark">#B2DFEE</color>
5     <color name="colorAccent">#9E9E9E</color>
6 </resources>

三、簡易留言板制作小練習

1、效果演示

2、activity_main.xml布局設計代碼

 

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     xmlns:app="http://schemas.android.com/apk/res-auto"
 4     xmlns:tools="http://schemas.android.com/tools"
 5     android:layout_width="match_parent"
 6     android:layout_height="match_parent"
 7     tools:context=".MainActivity">
 8 
 9     <TextView
10         android:id="@+id/tv"
11         android:layout_width="265dp"
12         android:layout_height="108dp"
13         android:layout_marginEnd="8dp"
14         android:layout_marginLeft="8dp"
15         android:layout_marginRight="8dp"
16         android:layout_marginStart="8dp"
17         android:layout_marginTop="28dp"
18         android:background="@drawable/border"
19         android:hint="演示輸入內容"
20         android:inputType="textMultiLine"
21         app:layout_constraintEnd_toEndOf="parent"
22         app:layout_constraintHorizontal_bias="0.349"
23         app:layout_constraintStart_toStartOf="parent"
24         app:layout_constraintTop_toTopOf="parent" />
25 
26     <TextView
27         android:id="@+id/textView2"
28         android:layout_width="wrap_content"
29         android:layout_height="0dp"
30         android:layout_marginLeft="44dp"
31         android:layout_marginStart="44dp"
32         android:layout_marginTop="28dp"
33         android:text="用戶名:"
34         app:layout_constraintStart_toStartOf="parent"
35         app:layout_constraintTop_toBottomOf="@+id/tv" />
36 
37     <EditText
38         android:id="@+id/yhm"
39         android:layout_width="148dp"
40         android:layout_height="33dp"
41         android:layout_marginLeft="8dp"
42         android:layout_marginStart="8dp"
43         android:layout_marginTop="28dp"
44         android:background="@drawable/border"
45         android:onClick="fsonClick"
46         android:ems="10"
47         android:hint="請輸入用戶名"
48         android:inputType="textPersonName"
49         app:layout_constraintStart_toEndOf="@+id/textView2"
50         app:layout_constraintTop_toBottomOf="@+id/tv" />
51 
52     <EditText
53         android:id="@+id/nr"
54         android:layout_width="270dp"
55         android:layout_height="141dp"
56         android:layout_marginLeft="28dp"
57         android:layout_marginStart="28dp"
58         android:layout_marginTop="20dp"
59         android:background="@drawable/border"
60         android:ems="10"
61         android:gravity="top"
62         android:hint="請輸入您要發送的內容"
63         android:inputType="textMultiLine|textPersonName"
64         app:layout_constraintStart_toStartOf="parent"
65         app:layout_constraintTop_toBottomOf="@+id/yhm" />
66 
67     <Button
68         android:id="@+id/fs"
69         android:layout_width="wrap_content"
70         android:layout_height="wrap_content"
71         android:layout_marginLeft="56dp"
72         android:layout_marginStart="56dp"
73         android:layout_marginTop="20dp"
74         android:onClick="fsonClick"
75         android:text="發送"
76         app:layout_constraintStart_toStartOf="parent"
77         app:layout_constraintTop_toBottomOf="@+id/nr" />
78 
79     <Button
80         android:id="@+id/qk"
81         android:layout_width="wrap_content"
82         android:layout_height="wrap_content"
83         android:layout_marginEnd="8dp"
84         android:layout_marginLeft="8dp"
85         android:layout_marginRight="8dp"
86         android:layout_marginStart="8dp"
87         android:layout_marginTop="20dp"
88         android:text="清空"
89         android:onClick="qkonClick"
90         app:layout_constraintEnd_toEndOf="parent"
91         app:layout_constraintHorizontal_bias="0.294"
92         app:layout_constraintStart_toEndOf="@+id/fs"
93         app:layout_constraintTop_toBottomOf="@+id/nr" />
94 </android.support.constraint.ConstraintLayout>

 

3、MainActivity功能實現代碼

 

 1 package com.example.dell.myapplication;
 2 
 3 import android.graphics.Color;
 4 import android.support.v7.app.AppCompatActivity;
 5 import android.os.Bundle;
 6 import android.view.KeyEvent;
 7 import android.view.View;
 8 import android.widget.Button;
 9 import android.widget.EditText;
10 import android.widget.TextView;
11 
12 public class MainActivity extends AppCompatActivity  {
13 TextView tv;
14 EditText yhm,nr;
15 Button fs,qk;
16 
17 
18 
19 
20 
21     @Override
22     protected void onCreate(Bundle savedInstanceState) {
23         super.onCreate(savedInstanceState);
24         setContentView(R.layout.activity_main);
25 
26         tv=findViewById(R.id.tv);
27         yhm=findViewById(R.id. yhm);
28         nr=findViewById(R.id.nr);
29         fs=findViewById(R.id. fs);
30         qk=findViewById(R.id.qk);
31 
32 
33 
34 
35 
36 
37 
38 
39 
40 
41     }
42 
43 //實現發送功能
44     public void qkonClick(View view) {
45 
46 
47         tv.setText("");//清空演示文本控件的內容
48         yhm.setText("");//清空輸入用戶名控件的內容
49         nr.setText(" ");//清空輸入發送內容控件的內容
50 
51     }
52 
53     public void fsonClick(View view) {
54         String username=yhm.getText().toString();
55         String centent=nr.getText().toString();
56         tv.setText(username+":"+centent);//演示輸出文本控件的內容是:用戶名:發送的內容
57     }
58 
59 
60 }

4、邊框設計drawable/border.xml

 

 1 <selector xmlns:android="http://schemas.android.com/apk/res/android">
 2     <item android:state_pressed="true" >
 3         <shape>
 4             <solid
 5                 android:color="#449def" />
 6             <stroke
 7                 android:width="1dp"
 8                 android:color="#2f6699" />
 9             <corners
10                 android:radius="0dp" />
11             <padding
12                 android:left="5dp"
13                 android:top="5dp"
14                 android:right="5dp"
15                 android:bottom="5dp" />
16         </shape>
17     </item>
18     <item>
19         <shape>
20             <gradient
21                 android:startColor="#ffffff"
22                 android:endColor="#ffffff"
23                 android:angle="270" />
24             <stroke
25                 android:width="1dp"
26                 android:color="#2f6699" />
27             <corners
28                 android:radius="0dp" />
29             <padding
30                 android:left="5dp"
31                 android:top="5dp"
32                 android:right="5dp"
33                 android:bottom="5dp" />
34         </shape>
35     </item>
36 </selector>

 

四、復習意圖顯示跳轉、意圖傳遞數據、Bundle傳遞數據(從一個activity傳遞數據給另一個activity)以及回傳數據(綜合小案例)

1、效果演示:

2、布局設計

2-1、第一個activity布局設計(用戶注冊信息),activity_main.xml布局設計代碼:

 

  1 <?xml version="1.0" encoding="utf-8"?>
  2 <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3     xmlns:app="http://schemas.android.com/apk/res-auto"
  4     xmlns:tools="http://schemas.android.com/tools"
  5     android:layout_width="match_parent"
  6     android:layout_height="match_parent"
  7     tools:context=".MainActivity">
  8 
  9     <TextView
 10         android:id="@+id/textView"
 11         android:layout_width="wrap_content"
 12         android:layout_height="wrap_content"
 13         android:layout_marginStart="44dp"
 14         android:layout_marginLeft="44dp"
 15         android:layout_marginTop="136dp"
 16         android:text="用戶名:"
 17         app:layout_constraintStart_toStartOf="parent"
 18         app:layout_constraintTop_toTopOf="parent" />
 19 
 20     <EditText
 21         android:id="@+id/et1"
 22         android:layout_width="wrap_content"
 23         android:layout_height="wrap_content"
 24         android:layout_marginStart="8dp"
 25         android:layout_marginLeft="8dp"
 26         android:layout_marginTop="136dp"
 27         android:ems="10"
 28         android:hint="請輸入用戶名"
 29         android:inputType="textPersonName"
 30         app:layout_constraintStart_toEndOf="@+id/textView"
 31         app:layout_constraintTop_toTopOf="parent" />
 32 
 33     <TextView
 34         android:id="@+id/textView2"
 35         android:layout_width="wrap_content"
 36         android:layout_height="wrap_content"
 37         android:layout_marginStart="56dp"
 38         android:layout_marginLeft="56dp"
 39         android:layout_marginTop="28dp"
 40         android:text="密碼:"
 41         app:layout_constraintStart_toStartOf="parent"
 42         app:layout_constraintTop_toBottomOf="@+id/et1" />
 43 
 44     <EditText
 45         android:id="@+id/et2"
 46         android:layout_width="wrap_content"
 47         android:layout_height="wrap_content"
 48         android:layout_marginStart="8dp"
 49         android:layout_marginLeft="8dp"
 50         android:layout_marginTop="28dp"
 51         android:ems="10"
 52         android:hint="請輸入密碼"
 53         android:inputType="textPassword"
 54         app:layout_constraintStart_toEndOf="@+id/textView2"
 55         app:layout_constraintTop_toBottomOf="@+id/et1" />
 56 
 57     <Button
 58         android:id="@+id/zc"
 59         android:layout_width="wrap_content"
 60         android:layout_height="wrap_content"
 61         android:layout_marginTop="268dp"
 62         android:text="注冊"
 63         app:layout_constraintEnd_toEndOf="parent"
 64         app:layout_constraintHorizontal_bias="0.498"
 65         app:layout_constraintStart_toStartOf="parent"
 66         app:layout_constraintTop_toBottomOf="@+id/et2" />
 67 
 68     <TextView
 69         android:id="@+id/textView3"
 70         android:layout_width="wrap_content"
 71         android:layout_height="wrap_content"
 72         android:layout_marginStart="40dp"
 73         android:layout_marginLeft="40dp"
 74         android:layout_marginTop="28dp"
 75         android:text="興趣愛好:"
 76         app:layout_constraintStart_toStartOf="parent"
 77         app:layout_constraintTop_toBottomOf="@+id/et2" />
 78 
 79     <CheckBox
 80         android:id="@+id/cb1"
 81         android:layout_width="wrap_content"
 82         android:layout_height="wrap_content"
 83         android:layout_marginStart="40dp"
 84         android:layout_marginLeft="40dp"
 85         android:layout_marginTop="28dp"
 86         android:text="編程"
 87         app:layout_constraintStart_toStartOf="parent"
 88         app:layout_constraintTop_toBottomOf="@+id/textView3" />
 89 
 90     <CheckBox
 91         android:id="@+id/cb2"
 92         android:layout_width="wrap_content"
 93         android:layout_height="wrap_content"
 94         android:layout_marginStart="40dp"
 95         android:layout_marginLeft="40dp"
 96         android:text="下棋"
 97         app:layout_constraintStart_toStartOf="parent"
 98         app:layout_constraintTop_toBottomOf="@+id/cb1" />
 99 
100     <CheckBox
101         android:id="@+id/cb3"
102         android:layout_width="wrap_content"
103         android:layout_height="wrap_content"
104         android:layout_marginStart="40dp"
105         android:layout_marginLeft="40dp"
106         android:text="唱歌"
107         app:layout_constraintStart_toStartOf="parent"
108         app:layout_constraintTop_toBottomOf="@+id/cb2" />
109 </androidx.constraintlayout.widget.ConstraintLayout>

 

2-2、第二個activity布局設計(用戶注冊信息顯示),activity_main2.xml布局設計代碼:

 

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     xmlns:app="http://schemas.android.com/apk/res-auto"
 4     xmlns:tools="http://schemas.android.com/tools"
 5     android:layout_width="match_parent"
 6     android:layout_height="match_parent"
 7     tools:context=".Main2Activity">
 8 
 9     <TextView
10         android:id="@+id/t1"
11         android:layout_width="wrap_content"
12         android:layout_height="wrap_content"
13         android:layout_marginStart="164dp"
14         android:layout_marginLeft="164dp"
15         android:layout_marginTop="56dp"
16         android:hint="用戶名"
17         app:layout_constraintStart_toStartOf="parent"
18         app:layout_constraintTop_toTopOf="parent" />
19 
20     <TextView
21         android:id="@+id/t2"
22         android:layout_width="wrap_content"
23         android:layout_height="wrap_content"
24         android:layout_marginStart="164dp"
25         android:layout_marginLeft="164dp"
26         android:layout_marginTop="16dp"
27         android:hint="興趣愛好"
28         app:layout_constraintStart_toStartOf="parent"
29         app:layout_constraintTop_toBottomOf="@+id/t1" />
30 
31     <Button
32         android:id="@+id/cz"
33         android:layout_width="wrap_content"
34         android:layout_height="wrap_content"
35         android:layout_marginStart="160dp"
36         android:layout_marginLeft="160dp"
37         android:layout_marginTop="40dp"
38         android:onClick="chongzhi"
39         android:text="我要充值"
40         app:layout_constraintStart_toStartOf="parent"
41         app:layout_constraintTop_toBottomOf="@+id/t2" />
42 
43     <TextView
44         android:id="@+id/TVResult"
45         android:layout_width="wrap_content"
46         android:layout_height="wrap_content"
47         android:layout_marginStart="164dp"
48         android:layout_marginLeft="164dp"
49         android:layout_marginTop="40dp"
50         android:hint="充值金額顯示"
51         app:layout_constraintStart_toStartOf="parent"
52         app:layout_constraintTop_toBottomOf="@+id/cz" />
53 </androidx.constraintlayout.widget.ConstraintLayout>

 

2-3、第三個activity布局設計(用戶充值界面),activity_main3.xml布局設計代碼:

 

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     xmlns:app="http://schemas.android.com/apk/res-auto"
 4     xmlns:tools="http://schemas.android.com/tools"
 5     android:layout_width="match_parent"
 6     android:layout_height="match_parent"
 7     tools:context=".Main3Activity">
 8 
 9     <EditText
10         android:id="@+id/shuru"
11         android:layout_width="0dp"
12         android:layout_height="wrap_content"
13         android:layout_weight="3"
14         android:ems="10"
15         android:hint="請輸入與充值金額"
16         android:inputType="textPersonName" />
17 
18     <Button
19         android:id="@+id/cz2"
20         android:layout_width="0dp"
21         android:layout_height="wrap_content"
22         android:layout_weight="1"
23         android:onClick="backzc"
24         android:text="充值" />
25 </LinearLayout>

 

3、功能實現部分

3-1、第一個activity功能實現(用戶注冊信息輸入)MainActivity.java功能實現代碼:

 1 package com.example.back;
 2 
 3 import androidx.appcompat.app.AppCompatActivity;
 4 
 5 import android.content.Intent;
 6 import android.os.Bundle;
 7 import android.view.View;
 8 import android.widget.Button;
 9 import android.widget.CheckBox;
10 import android.widget.EditText;
11 import android.widget.Toast;
12 
13 public class MainActivity extends AppCompatActivity implements View.OnClickListener {
14 EditText et1,et2;
15 CheckBox cb1,cb2,cb3;
16 Button zc;
17 private  String hobby="";
18     @Override
19     protected void onCreate(Bundle savedInstanceState) {
20         super.onCreate(savedInstanceState);
21         setContentView(R.layout.activity_main);
22 
23         et1=findViewById(R.id.et1);
24         et2=findViewById(R.id.et2);
25         cb1=findViewById(R.id.cb1);
26         cb2=findViewById(R.id.cb2);
27         cb3=findViewById(R.id.cb3);
28         zc=findViewById(R.id.zc);
29 
30         zc.setOnClickListener(this);
31 
32     }
33 
34     @Override
35     public void onClick(View v) {
36          String name=et1.getText().toString();
37          String  pwd=et2.getText().toString();
38          if (cb1.isChecked())
39              hobby+=cb1.getText().toString();
40          if (cb2.isChecked())
41              hobby+=cb2.getText().toString();
42          if (cb3.isChecked())
43              hobby+=cb3.getText().toString();
44 
45          if (name.equals("")||pwd.equals(""))
46              Toast.makeText(this, "抱歉,登錄注冊;請重新注冊", Toast.LENGTH_SHORT).show();
47 
48          else{
49              Toast.makeText(this, "恭喜您,注冊成功", Toast.LENGTH_SHORT).show();
50 
51 
52              Intent intent=new Intent(MainActivity.this,Main2Activity.class);//新建立意圖對象(意圖名為intent)
53              Bundle bundle=new Bundle();//新建包對象作為存儲數據(包名為bundle)
54              bundle.putString("username",name);//包名.putString("鍵名",對應的鍵名內容);向第二activity傳輸用戶名
55              bundle.putString("Myhobby",hobby);//包名.putString("鍵名",對應的鍵名內容);向第二activity傳輸密碼
56              intent.putExtras(bundle);//意圖名.putExtras(bundle),通過意圖來打包鍵名一起發送出去
57              startActivity(intent);//開始執行意圖
58          }
59     }
60 }

3-2、第二個activity功能實現(用戶注冊信息顯示)Main2Activity.java功能實現代碼:

 

 1 package com.example.back;
 2 
 3 import androidx.annotation.Nullable;
 4 import androidx.appcompat.app.AppCompatActivity;
 5 
 6 import android.content.Intent;
 7 import android.os.Bundle;
 8 import android.view.View;
 9 import android.widget.Button;
10 import android.widget.TextView;
11 
12 public class Main2Activity extends AppCompatActivity {
13     TextView t1,t2;
14     Button cz;
15     TextView TVResult;
16 
17     @Override
18     protected void onCreate(Bundle savedInstanceState) {
19         super.onCreate(savedInstanceState);
20         setContentView(R.layout.activity_main2);
21 
22         t1=findViewById(R.id.t1);
23         t2=findViewById(R.id.t2);
24         cz=findViewById(R.id.cz);
25         TVResult=findViewById(R.id.TVResult);
26 
27 
28 
29 
30 
31         Intent intent = getIntent();//建立接受意圖
32         Bundle bundle = intent.getExtras();//建立接受包
33         String username = bundle.getString("username");//建立上個activity所傳遞的用戶名數據賦值給username
34         String Myhobby = bundle.getString("Myhobby");//建立上個activity所傳遞的密碼數據賦值給Myhobby
35         t1.setText("用戶名:"+username);
36         t2.setText("興趣愛好:"+Myhobby);
37 
38     }
39 
40     //實現點擊我要充值按鈕跳轉
41     public void chongzhi(View view){
42         Intent intent=new Intent(Main2Activity.this,Main3Activity.class);
43         //創建顯示意圖跳轉(有第二個Activity跳轉到第三個Activity中)
44         startActivityForResult(intent,1);//開始執行意圖跳轉並且返回結果(返回結果請求碼要求必須是唯一的,不能與別的請求碼相同)
45     }
46 
47 
48     //復寫onActivityResult方法來接受第三個activity回傳來的數據
49     @Override
50     protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
51         super.onActivityResult(requestCode, resultCode, data);
52       String scanf= data.getStringExtra("scanf");//得到第三方activity回傳的數據並賦值給scanf
53         TVResult.setText("充值金額為:"+scanf+"");//輸出演示充值金額
54     }
55 }

 

3-3、第三個activity功能實現(用戶實現充值金額功能)Main3Activity.java功能實現代碼:

 

 1 package com.example.back;
 2 
 3 import androidx.appcompat.app.AppCompatActivity;
 4 
 5 import android.content.Intent;
 6 import android.os.Bundle;
 7 import android.view.View;
 8 import android.widget.Button;
 9 import android.widget.EditText;
10 
11 public class Main3Activity extends AppCompatActivity {
12 EditText shuru;
13 
14     @Override
15     protected void onCreate(Bundle savedInstanceState) {
16         super.onCreate(savedInstanceState);
17         setContentView(R.layout.activity_main3);
18         shuru=findViewById(R.id.shuru);
19 
20 
21     }
22 
23     public  void  backzc(View view){
24         String scanf=shuru.getText().toString();
25         Intent intent=new Intent();
26         intent.putExtra("scanf",scanf);
27          setResult(2,intent);//回傳數據
28         finish();//當前Activity完成出棧
29 
30     }
31 }

 

 

 

 

五、總結知識點:

本次通過四次作業小練習,讓我學會了單選按鈕交互、復選按鈕交互、文本編輯交互(數據傳輸),以及如何通過一個activity跳轉到另一個activity中(有兩種形式:①顯示方式②隱式方式),並且能夠實現由一個activity傳送數據到第二個activity數據(有兩種方法:①通過創建意圖對象,意圖名.putExtra("鍵名",對應的傳送值); ②通過在意圖基礎下創建Bundle來實現數據傳送  例如:bundle.putString("鍵名",對應的傳送值);回傳數據由第二個activity數據回傳到第一個activity數據)使用setResult()函數實現回傳數據

 

以上就是本次練習全部總結,我是安德風QQ1652102745,喜歡我的小伙伴,歡迎點擊點贊與關注有問題歡迎在下方留言,看到后會一一答復。感謝大家的信任與支持。

 
        
 
        

 

 

 

 

 

 

 

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM