目录
1.EditText
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<!--ImageView 需要放在前面,不然会覆盖掉文字-->
<!-- match_parent background 背景-->
<!-- wrap_content src 前景图片-->
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@mipmap/bg">
</ImageView>
<EditText
android:id="@+id/et1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
android:textSize="20sp"
android:hint="输入姓名"
android:inputType="text">
</EditText>
<TextView
android:id="@+id/tv1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="用户名"
android:textSize="20sp"
app:layout_constraintRight_toLeftOf="@id/et1"
app:layout_constraintBaseline_toBaselineOf="@id/et1"
android:layout_marginRight="10sp"/>
<EditText
android:id="@+id/et2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintBaseline_toBaselineOf="@+id/tv2"
app:layout_constraintLeft_toLeftOf="@+id/et1"
android:textSize="20sp"
android:hint="输入密码"
android:inputType="textPassword">
</EditText>
<TextView
android:id="@+id/tv2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="密 码"
android:textSize="20sp"
app:layout_constraintLeft_toLeftOf="@id/tv1"
app:layout_constraintTop_toBottomOf="@id/tv1"
android:layout_marginRight="10sp"/>
<TextView
android:id="@+id/tv3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
app:layout_constraintBottom_toTopOf="@id/tv1"
app:layout_constraintLeft_toLeftOf="@id/tv1"
android:layout_marginTop="10sp" />
<!--前景图片-->
<ImageView
android:layout_width="60sp"
android:layout_height="60sp"
app:layout_constraintBottom_toTopOf="@id/tv1"
app:layout_constraintLeft_toLeftOf="@id/tv1"
android:src="@mipmap/pic">
</ImageView>
</androidx.constraintlayout.widget.ConstraintLayout>
将输入内容同步显示在TextView中
package com.example.myapplication;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.widget.EditText;
import android.widget.CheckBox;
import android.widget.RadioGroup;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity{
private EditText editText;
private TextView textView;
private RadioGroup radioGroup;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editText = findViewById(R.id.et1);
textView = findViewById(R.id.tv3); //把信息显示在这里
// 匿名内部类
editText.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
//获取完成信息
@Override
public void afterTextChanged(Editable s) {
textView.setText("input: "+s.toString());
}
});
}
}
2.单选框(RadioButton、RadioGroup)
选项垂直放置或水平放置
android:orientation="horizontal"
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<!--ImageView 需要放在前面,不然会覆盖掉文字-->
<!-- match_parent background 背景-->
<!-- wrap_content src 前景图片-->
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@mipmap/bg">
</ImageView>
<EditText
android:id="@+id/et1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
android:textSize="20sp"
android:hint="输入姓名"
android:inputType="text">
</EditText>
<TextView
android:id="@+id/tv1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="用户名"
android:textSize="20sp"
app:layout_constraintRight_toLeftOf="@id/et1"
app:layout_constraintBaseline_toBaselineOf="@id/et1"
android:layout_marginRight="10sp"/>
<EditText
android:id="@+id/et2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintBaseline_toBaselineOf="@+id/tv2"
app:layout_constraintLeft_toLeftOf="@+id/et1"
android:textSize="20sp"
android:hint="输入密码"
android:inputType="textPassword">
</EditText>
<TextView
android:id="@+id/tv2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="密 码"
android:textSize="20sp"
app:layout_constraintLeft_toLeftOf="@id/tv1"
app:layout_constraintTop_toBottomOf="@id/tv1"
android:layout_marginRight="10sp"/>
<TextView
android:id="@+id/tv3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
app:layout_constraintBottom_toTopOf="@id/tv1"
app:layout_constraintLeft_toLeftOf="@id/tv1"
android:layout_marginTop="10sp" />
<!--前景图片-->
<ImageView
android:layout_width="60sp"
android:layout_height="60sp"
app:layout_constraintBottom_toTopOf="@id/tv1"
app:layout_constraintRight_toLeftOf="@id/tv1"
android:src="@mipmap/pic">
</ImageView>
<RadioGroup
android:id="@+id/rg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintTop_toBottomOf="@id/tv2"
app:layout_constraintLeft_toLeftOf="@id/tv2"
android:layout_marginTop="10sp"
android:orientation="horizontal">
<RadioButton
android:id="@+id/rb1"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="Girl"
android:textSize="20sp">
</RadioButton>
<RadioButton
android:id="@+id/rb2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Boy"
android:textSize="20sp">
</RadioButton>
</RadioGroup>
</androidx.constraintlayout.widget.ConstraintLayout>
package com.example.myapplication;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.widget.EditText;
import android.widget.CheckBox;
import android.widget.RadioGroup;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity implements RadioGroup.OnCheckedChangeListener,TextWatcher{
private EditText editText;
private TextView textView;
private RadioGroup radioGroup;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editText = findViewById(R.id.et1);
textView = findViewById(R.id.tv3); //把信息显示在这里
radioGroup = findViewById(R.id.rg);
//监听事件
editText.addTextChangedListener(this);
radioGroup.setOnCheckedChangeListener(this);
(获取用户名名称) 方法一:匿名内部类
/* editText.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
//获取完成信息
@Override
public void afterTextChanged(Editable s) {
textView.setText("input: "+s.toString());
}
});*/
(获取选项内容) 方法一:匿名内部类
/* radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
if(checkedId==R.id.rb1)
textView.setText("choosed: Gril");
else
textView.setText("choosed: Boy");
}
});*/
}
(获取用户名名称) 方法二:匿名内部类 [ 需要implements+TextWatcher]
//所有的重载需要全部实现
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) { }
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) { }
@Override
public void afterTextChanged(Editable s) {
textView.setText("input: "+s.toString());
}
(获取选项内容) 方法二:实现接口 [ 需要implements+RadioGroup.OnCheckedChangeListener]
//一般建议用接口来实现
@Override
public void onCheckedChanged(RadioGroup groupmint ,int checkedId) {
switch (checkedId) {
case R.id.rb1:
textView.setText("choosed: Gril");
break;
case R.id.rb2:
textView.setText("choosed: Boy");
break;
}
}
}
3.复选框 (CheckBox)
package com.example.myapplication;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.CheckBox;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity implements RadioGroup.OnCheckedChangeListener,TextWatcher,CompoundButton.OnCheckedChangeListener{
private EditText editText;
private TextView textView;
private RadioGroup radioGroup;
private CompoundButton compoundButton;
private CheckBox checkBox1,checkBox2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editText = findViewById(R.id.et1);
textView = findViewById(R.id.tv3); //把信息显示在这里
radioGroup = findViewById(R.id.rg);
compoundButton = findViewById(R.id.cb1);
checkBox1 = findViewById(R.id.cb1);
checkBox2 = findViewById(R.id.cb2);
//监听事件
editText.addTextChangedListener(this);
radioGroup.setOnCheckedChangeListener(this);
checkBox1.setOnCheckedChangeListener(this);
checkBox2.setOnCheckedChangeListener(this);
(显示复选框内容) 方法一:匿名内部类 但是只能显示其中一个
/* checkBox1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked)
textView.setText("Choosed: 篮球");
}
});
checkBox2.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked)
textView.setText("Choosed: 足球");
}
});*/
(获取用户名名称) 方法一:匿名内部类
/* editText.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
//获取完成信息
@Override
public void afterTextChanged(Editable s) {
textView.setText("input: "+s.toString());
}
});*/
(获取选项内容) 方法一:匿名内部类
/* radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
if(checkedId==R.id.rb1)
textView.setText("choosed: Gril");
else
textView.setText("choosed: Boy");
}
});*/
}
(获取用户名名称) 方法二:匿名内部类 [ 需要implements+TextWatcher]
//所有的重载需要全部实现
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) { }
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) { }
@Override
public void afterTextChanged(Editable s) {
textView.setText("input: "+s.toString());
}
(获取选项内容) 方法二:实现接口 [ 需要implements+RadioGroup.OnCheckedChangeListener]
//一般建议用接口来实现
@Override
public void onCheckedChanged(RadioGroup groupmint ,int checkedId) {
switch (checkedId) {
case R.id.rb1:
textView.setText("choosed: Gril");
break;
case R.id.rb2:
textView.setText("choosed: Boy");
break;
}
}
String str =new String();
(获取复选框内容) 方法二:通过继承实现 [ 需要implements+ CompoundButton.OnCheckedChangeListener]
@Override
public void onCheckedChanged(CompoundButton button,boolean isChecked)
{
String text = button.getText().toString();
if(isChecked) {
if(!str.contains(text))
str = str +" "+text;
}
else {
if(str.contains(text))
str = str.replace(text,"");
}
//textView.setText(str);
//提示信息
Toast.makeText(this,str,Toast.LENGTH_LONG).show();
}
}