完整代码在文末
目录
2.MainActivity主类文件不是java语言,想要新建activity
1.所有的控件都是约束布局,顾名思义就是受限于parent的位置,一般一个控件的约束布局只需要两个方向的约束就可以拉(你不设置,它会flying) 如图:
第二个项目 选课中心已完成:http://t.csdnimg.cn/QQV1t
bug: 点击爱好和取消爱好 没有实时同步
点击性别隐藏,取消后,性别需要再选取
问题已解决,更新在文末完整代码中
功能:
1. 点击radiobutton 男女,赋值提示信息 帅哥/美女
2.点击switch 组件,隐藏radiobutton 按钮,性别提示信息
3.点击checkbox组件,赋值你的兴趣爱好信息
4.点击spinner组件,赋值城市信息
5.点击提交,弹出你的个人信息 如下图
效果图:
新建项目:
1.gradle 使用国内镜像源 加速
点击setting文件
添加如下代码,改为下图效果(速度噌噌上)
maven {
url 'https://maven.aliyun.com/repository/google'
}
maven {
url 'https://maven.aliyun.com/repository/public'
}
2.新建主类文件、布局文件:
1.如果已有主类文件但没有layout文件
点击res文件右键 新建layout directionary目录 再新建布局文件xml
在你的主类里添加布局文件,如图所示:
2.MainActivity主类文件不是java语言,想要新建activity
点击第一个com.example,右键新建empty activity 选择语言java ,其中的layout文件也会初始化完成。
当然后续要设置该项目为启动项目(另外如果模拟器闪退了,兄弟! 模拟器没问题,去看看你滴locat的报错吧)
3.布局文件代码:
接下来我来讲讲该讲的重要控件代码:
1.所有的控件都是约束布局,顾名思义就是受限于parent的位置,一般一个控件的约束布局只需要两个方向的约束就可以拉(你不设置,它会flying) 如图:
2.Switch 控件(点击隐藏性别button)
//选中性别,绑定性别信息
boy.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
if (b){
text_Sex = "帅哥";
}
}
});
girl.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
if (b){
text_Sex = "美女";
}
}
});
//隐藏性别switch控件,并且隐藏信息text
swi.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
if (b) {
radiogp.setVisibility(View.INVISIBLE);
}else{
radiogp.setVisibility(View.VISIBLE);
}
}
}
);
3.Sppiner控件
首先决定好约束后,设置下拉列表中的item咯,在res 文件下,点击Strings.xml,在文件中添加如下代码。
再回到布局文件设置entries为string-array 的Id.
Spinner代码文件(选择item城市,赋值城市):
//City模块
spin.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
text_City += getResources().getStringArray(R.array.city)[position];
}
@Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
完整代码:
MainAcitivity2.java:
package com.example.myapplication;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
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.Spinner;
import android.widget.Switch;
import android.widget.Toast;
public class MainActivity2 extends Activity {
private Button subbtn;
private EditText name;
private EditText password;
private Switch swi;
private RadioGroup radiogp;
private RadioButton boy;
private RadioButton girl;
public String text_Sex;
private CheckBox basbtn;
private CheckBox footbtn;
private CheckBox badbtn;
private CheckBox readbtn;
private String text_Aihao;
private Spinner spin;
private String text_City;
private EditText name_;
private EditText password_;
private String text_last;
boolean b =true;
String a = " ";
@Override
protected void onCreate(Bundle savedInstanceState) {
text_Sex="";
text_Aihao="";
text_City="";
text_last="";
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
swi=(Switch) findViewById(R.id.switch1);
radiogp=(RadioGroup)findViewById((R.id.radioGroup));
subbtn = (Button)findViewById(R.id.submitbtn);
boy=(RadioButton)findViewById(R.id.boybutton);
girl=(RadioButton)findViewById(R.id.girlbutton);
spin=(Spinner)findViewById(R.id.spinner);
name_=(EditText) findViewById(R.id.editTextText);
password_=(EditText) findViewById(R.id.editTextText2);
//选中性别,绑定性别信息
boy.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
if (b){
text_Sex = "帅哥";
}
}
});
girl.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
if (b){
text_Sex = "美女";
}
}
});
//隐藏性别switch控件,并且隐藏信息text
swi.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
if (b) {
radiogp.setVisibility(View.INVISIBLE);
}else{
radiogp.setVisibility(View.VISIBLE);
}
}
}
);
//兴趣爱好模块
basbtn = (CheckBox) findViewById(R.id.baseketballbtn);
footbtn = (CheckBox) findViewById(R.id.footballbtn);
badbtn = (CheckBox) findViewById(R.id.badminballbtn);
readbtn = (CheckBox) findViewById(R.id.readbtn);
basbtn.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
if (b){
text_Aihao +="篮球";
}
else{
text_Aihao = text_Aihao.replace("篮球","");
}
}
});
footbtn.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
if (b){
text_Aihao +="足球";
}
else{
text_Aihao = text_Aihao.replace("足球","");
}
}
});
badbtn.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
if (b){
text_Aihao +="羽毛球";
}
else{
text_Aihao = text_Aihao.replace("羽毛球","");
}
}
});
readbtn.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
if (b){
text_Aihao +="阅读";
}
else{
text_Aihao = text_Aihao.replace("阅读","");
}
}
});
//City模块
spin.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
text_City = getResources().getStringArray(R.array.city)[position];
}
@Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
//submit 按钮
subbtn.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
if(swi.isChecked()){
text_last="";
}else{
text_last=text_Sex;
}
Toast.makeText(MainActivity2.this,name_.getText()+text_last+"你好!你所在的城市是"+text_City+"!你的兴趣爱好是"+text_Aihao,Toast.LENGTH_SHORT).show();
}
});
}
}
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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity2">
<TextView
android:id="@+id/textView"
android:layout_width="128dp"
android:layout_height="39dp"
android:layout_marginStart="130dp"
android:layout_marginTop="50dp"
android:text="用户注册"
android:textSize="30dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/textView2"
android:layout_width="79dp"
android:layout_height="27dp"
android:layout_marginStart="50dp"
android:layout_marginTop="50dp"
android:text="用户名:"
android:textSize="16dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView" />
<EditText
android:id="@+id/editTextText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="28dp"
android:layout_marginTop="40dp"
android:ems="10"
android:inputType="text"
android:text="Name"
app:layout_constraintStart_toEndOf="@+id/textView2"
app:layout_constraintTop_toBottomOf="@+id/textView" />
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="50dp"
android:layout_marginTop="50dp"
android:text="密码:"
android:textSize="16dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView2" />
<EditText
android:id="@+id/editTextText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="60dp"
android:layout_marginTop="25dp"
android:ems="10"
android:inputType="text"
android:text="Password"
app:layout_constraintStart_toEndOf="@+id/textView3"
app:layout_constraintTop_toBottomOf="@+id/editTextText" />
<TextView
android:id="@+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="50dp"
android:layout_marginTop="20dp"
android:text="你的性别:"
android:textSize="16dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView3" />
<Switch
android:id="@+id/switch1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="12dp"
android:layout_marginEnd="44dp"
android:text="是否隐藏性别"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="@+id/editTextText2" />
<RadioGroup
android:id="@+id/radioGroup"
android:layout_width="200dp"
android:layout_height="38dp"
android:layout_marginStart="68dp"
android:layout_marginTop="44dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/switch1">
<RadioButton
android:id="@+id/boybutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="0dp"
android:rotationX="-1"
android:text="男" />
<RadioButton
android:id="@+id/girlbutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="120dp"
android:layout_marginTop="-32dp"
android:text="女" />
</RadioGroup>
<TextView
android:id="@+id/textView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="48dp"
android:layout_marginTop="36dp"
android:text="你的兴趣爱好:"
android:textSize="16dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/radioGroup" />
<CheckBox
android:id="@+id/baseketballbtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
android:layout_marginTop="40dp"
android:text="篮球"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView5" />
<CheckBox
android:id="@+id/footballbtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="44dp"
android:layout_marginTop="40dp"
android:text="足球"
app:layout_constraintStart_toEndOf="@+id/baseketballbtn"
app:layout_constraintTop_toBottomOf="@+id/textView5" />
<CheckBox
android:id="@+id/badminballbtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="44dp"
android:layout_marginTop="40dp"
android:text="羽毛球"
app:layout_constraintStart_toEndOf="@+id/footballbtn"
app:layout_constraintTop_toBottomOf="@+id/textView5" />
<CheckBox
android:id="@+id/readbtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="30dp"
android:layout_marginTop="40dp"
android:text="阅读"
app:layout_constraintStart_toEndOf="@+id/badminballbtn"
app:layout_constraintTop_toBottomOf="@+id/textView5" />
<TextView
android:id="@+id/textView6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="24dp"
android:layout_marginTop="52dp"
android:text="你所在的城市:"
android:textSize="18dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/baseketballbtn" />
<Spinner
android:id="@+id/spinner"
android:layout_width="216dp"
android:layout_height="20dp"
android:layout_marginStart="48dp"
android:layout_marginTop="56dp"
android:entries="@array/city"
android:spinnerMode="dropdown"
app:layout_constraintStart_toEndOf="@+id/textView6"
app:layout_constraintTop_toBottomOf="@+id/badminballbtn" />
<Button
android:id="@+id/submitbtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="156dp"
android:layout_marginTop="48dp"
android:text="提交"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView6" />
</androidx.constraintlayout.widget.ConstraintLayout>