zoukankan      html  css  js  c++  java
  • 复习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("您输入的信息为:
    "+"姓名:"+name+"
    性别:"+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("您选中的歌曲:
    "+gq1+"
    "+gq2+"
    "+gq3+"
    ");
    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,喜欢我的小伙伴,欢迎点击点赞与关注有问题欢迎在下方留言,看到后会一一答复。感谢大家的信任与支持。

    
    
    
    
    
    
    
    
    
    
    

     

  • 相关阅读:
    javascript 事件冒泡
    Java 理论与实践: 正确使用 Volatile 变量
    Concurrency,Java 并发
    POJ2379 ACM Rank Table 模拟题
    HDU1711Number Sequence KMP
    POJ1061 青蛙的约会 扩展GCD
    HDU2523 SORT AGAIN HASH
    HDU2087剪花布条 KMP
    HDU3736 Cyclic Nacklace KMP
    HDU1709The Balance 母函数
  • 原文地址:https://www.cnblogs.com/adf520/p/12547095.html
Copyright © 2011-2022 走看看