通過Android studio編寫用戶注冊信息表單(實現用戶交互)小demo,話不多說直接上小demo
————安德風QQ1652102745
1、activity_ main.xml中的約束布局設計原型樣式圖:

2、在模擬器中演示效果:

3、實現約束布局代碼,代碼存放在activity_ main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/CL"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="30dp"
android:layout_marginTop="30dp"
android:text="用戶名:"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="0.047"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.064" />
<EditText
android:id="@+id/et1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginLeft="16dp"
android:layout_marginTop="72dp"
android:ems="10"
android:hint="請輸入你的用戶名"
android:inputType="textPersonName"
app:layout_constraintStart_toEndOf="@+id/textView"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="56dp"
android:layout_marginLeft="56dp"
android:layout_marginTop="28dp"
android:text="密碼:"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/et1" />
<EditText
android:id="@+id/et2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="20dp"
android:layout_marginLeft="20dp"
android:layout_marginTop="30dp"
android:ems="10"
android:hint="請輸入你的密碼"
android:inputType="textPassword"
app:layout_constraintStart_toEndOf="@+id/textView2"
app:layout_constraintTop_toBottomOf="@+id/et1" />
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="28dp"
android:layout_marginLeft="28dp"
android:layout_marginTop="24dp"
android:text="性別"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/et2" />
<RadioGroup
android:id="@+id/rg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="30dp"
android:layout_marginLeft="30dp"
android:layout_marginTop="60dp"
android:orientation="horizontal"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/et2">
<RadioButton
android:id="@+id/rb1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="男" />
<RadioButton
android:id="@+id/rb2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="女" />
</RadioGroup>
<TextView
android:id="@+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="30dp"
android:layout_marginLeft="30dp"
android:layout_marginTop="30dp"
android:text="興趣愛好"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/rg" />
<CheckBox
android:id="@+id/cb1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="28dp"
android:layout_marginLeft="28dp"
android:layout_marginTop="16dp"
android:text="編程"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView4" />
<CheckBox
android:id="@+id/cb2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="28dp"
android:layout_marginLeft="28dp"
android:text="下棋"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/cb1" />
<CheckBox
android:id="@+id/cb3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="28dp"
android:layout_marginLeft="28dp"
android:text="唱歌"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/cb2" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="36dp"
android:text="注冊"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/cb3" />
<TextView
android:id="@+id/tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="40dp"
android:text="執行結果"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/button" />
</androidx.constraintlayout.widget.ConstraintLayout>
4、實現約束布局代碼,代碼存放在MainActivity.java
package com.example.myapplication;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity implements View.OnClickListener,RadioGroup.OnCheckedChangeListener, CompoundButton.OnCheckedChangeListener{
private Button zc;//聲明注冊按鈕的變量
RadioGroup rg;//聲明單選組的變量
RadioButton rb1,rb2;//聲明單選1,單選2的變量
CheckBox cb1,cb2,cb3;//聲明復選框1,2,3的變量
EditText et1,et2;////聲明輸入文本框1,2的變量
TextView tv;//聲明結果文本的變量
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
zc=findViewById(R.id.button);//尋找注冊按鈕id
zc.setOnClickListener(this);//給注冊按鈕安裝監聽器
rg=findViewById(R.id.rg);//尋找單選組控件id
rg.setOnCheckedChangeListener(this);//給單選組安裝監聽器
rb1=findViewById(R.id.rb1);//尋找單選控件1id
rb1.setOnCheckedChangeListener(this);//給單選控件1安裝監聽器
rb2=findViewById(R.id.rb2);//尋找單選控件2id
rb2.setOnCheckedChangeListener(this);//給單選控件2安裝監聽器
cb1=findViewById(R.id.cb1);//尋找復選框1控件id
cb1.setOnCheckedChangeListener(this);//給復選框控件1安裝監聽器
cb2=findViewById(R.id.cb2);//尋找復選框2控件id
cb2.setOnCheckedChangeListener(this);//給復選框控件2安裝監聽器
cb3=findViewById(R.id.cb3);//尋找復選框3控件id
cb3.setOnCheckedChangeListener(this);//給復選框控件3安裝監聽器
et1=findViewById(R.id.et1);//尋找輸入框1控件id
et2=findViewById(R.id.et2);//尋找輸入框2控件id
tv=findViewById(R.id.tv);//尋找輸入框2控件id
}
//實現選項按鈕組交互功能
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
switch (checkedId){
case R.id.rb1:
System.out.println(rb1.getText().toString());
break;
case R.id.rb2:
System.out.println(rb2.getText().toString());
break;
}
}
//實現復選框交互功能
@Override //CompoundButton選中或未選中按鈕
public void onCheckedChanged(CompoundButton CompoundButton, boolean b) {
switch (CompoundButton.getId()) //得到選中或未選中按鈕id
{
case R.id.cb1: //復選框1id
if (b==true)//判斷復選框1是否為真
System.out.println(cb1.getText().toString());
//如果是真執行復選框按鈕輸出的結果是得到該文本(cb1對應的text屬性文本字符串)字符串
break;
case R.id.cb2:
if (b==true)
System.out.println(cb2.getText().toString());
break;
case R.id.cb3:
if (b==true)
System.out.println(cb3.getText().toString());
break;
}
}
//注冊按鈕實現交互功能
@Override
public void onClick(View view){
String strname=et1.getText().toString();//獲取用戶名(ID綁定用戶名)
String strPassword=et2.getText().toString();//獲取密碼(ID綁定密碼)
if (strname.equals("adf520")&&strPassword.equals("123456"))//判斷用戶名是否等於"adf520"並且滿足密碼等於"123456"
tv.setText("注冊成功");//如果滿足條件的話執行結果文本框輸出內容為"注冊成功"
else
{
tv.setText("注冊失敗,請重新修改信息后再來注冊");//否則執行結果文本框輸出內容為"注冊失敗,請重新修改信息后再來注冊"
}
}
}
