前端界面(布局文件) :ScrollView(滚动视图布局)+TextView(设置跑马灯)+EditText(编辑框)+TextView(文本框)+RadioGroup(单选按钮组)+Spinner(下拉列表)+CheckBox(多选框)+RatingBar(评分条)
java代码:setOnFocusChangeListener(EditText离开编辑框时监听事件)+setOnClickListener(一般的按钮监听事件)+setOnCheckedChangeListener(单选按钮监听事件)+setOnItemSelectedListener(多选框选中监听事件)+setOnRatingBarChangeListener(单击评分条监听事件)
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:paddingLeft="20dp"
android:text="返回"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:paddingLeft="120dp"
android:text="用户登录"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:paddingLeft="130dp"
android:text="注册"/>
LinearLayout>
<TextView
android:padding="10dp"
android:background="#ff00ff00"
android:layout_centerInParent="true"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:singleLine="true"
android:ellipsize="marquee"
android:focusable="true"
android:focusableInTouchMode="true"
android:marqueeRepeatLimit="marquee_forever"
android:text="加油,未来可期!加油,你是最棒的!加油,享受当下时光!加油加油加油加油加油加油" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="用户名:"
android:textSize="20sp"/>
<EditText
android:id="@+id/edtUserName"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2"
android:hint="请输入用户名"/>
LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="密码:"
android:textSize="20sp"/>
<EditText
android:id="@+id/edtPassWd"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2"
android:hint="请输入密码6-12位"/>
LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="确认密码:"
android:textSize="20sp"/>
<EditText
android:id="@+id/edtConfirmPassWd"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2"
android:hint="请确认输入密码6-12位"/>
LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<RadioGroup
android:id="@+id/rdgGender"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="性别:"
android:textSize="20sp"/>
<RadioButton
android:id="@+id/rdMale"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textSize="20sp"
android:text="男"/>
<RadioButton
android:id="@+id/rdFeMale"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textSize="20sp"
android:text=" 女"/>
RadioGroup>
LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:layout_width="0dp"
android:layout_weight="2"
android:layout_height="wrap_content"
android:text="出生地:"
android:textSize="20sp"/>
<Spinner
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/s_main_spinner1"
>
Spinner>
<Spinner android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/s_main_spinner2" > Spinner>
LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:textSize="20sp"
android:text="@string/title" />
LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="horizontal">
<CheckBox
android:id="@+id/checkbox1"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_width="0dp"
android:textSize="16sp"
android:text="@string/reading"/>
<CheckBox
android:id="@+id/checkbox2"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_width="0dp"
android:text="@string/coding"/>
<CheckBox
android:id="@+id/checkbox3"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_width="0dp"
android:text="@string/sing"/>
<CheckBox
android:id="@+id/checkbox4"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_width="0dp"
android:text="@string/game"/>
LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="horizontal">
<CheckBox
android:id="@+id/checkbox5"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_width="0dp"
android:textSize="16sp"
android:text="@string/dacing"/>
<CheckBox
android:id="@+id/checkbox6"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_width="0dp"
android:text="@string/Kongfu"/>
<CheckBox
android:id="@+id/checkbox7"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_width="0dp"
android:text="@string/running"/>
<CheckBox
android:id="@+id/checkbox8"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_width="0dp"
android:text="@string/swimming"/>
LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="自我评价:"
android:textSize="20sp"/>
LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="勤劳"
android:textSize="20sp"/>
<RatingBar
android:id="@+id/rtMarks1"
style="@style/RadingStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clickable="true"
android:isIndicator="false"
android:stepSize="0.1"
android:numStars="5" />
LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="上进"
android:textSize="20sp"/>
<RatingBar
android:id="@+id/rtMarks2"
style="@style/RadingStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clickable="true"
android:isIndicator="false"
android:stepSize="0.1"
android:numStars="5" />
LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="勇敢"
android:textSize="20sp"/>
<RatingBar
android:id="@+id/rtMarks3"
style="@style/RadingStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clickable="true"
android:isIndicator="false"
android:stepSize="0.1"
android:numStars="5" />
LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="诚实"
android:textSize="20sp"/>
<RatingBar
android:id="@+id/rtMarks4"
style="@style/RadingStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clickable="true"
android:isIndicator="false"
android:stepSize="0.1"
android:numStars="5" />
LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:id="@+id/btnReg"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:text="注册" />
LinearLayout>
LinearLayout>
ScrollView>
package com.example.qq.myregisterapplication;
import android.content.DialogInterface;
import android.support.annotation.IdRes;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
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.RatingBar;
import android.widget.SimpleAdapter;
import android.widget.Spinner;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class MainActivity extends AppCompatActivity implements CompoundButton.OnCheckedChangeListener{
//声明组件
EditText edtUserName, edtPassWd, edtConfirmPassWd;
RadioButton rdMale, rdFeMale;
RadioGroup rdgGender;
RatingBar rtMarks1,rtMarks2,rtMarks3,rtMarks4;
Button btnReg;
boolean isYes=true;//状态判断是否可以注册
ArrayList<CompoundButton> selected = new ArrayList<>();//用来存储已选取项的集合对象
//省份城市的下拉列表
private Spinner spinner2;
private Spinner spinner1;
private List<Map<String, Object>> list;
private String[] province;
private String[] city0;
private String[] city1;
private String[] city2;
private int[] images;
//初始化组件
public void initView() {
btnReg=(Button)findViewById(R.id.btnReg);
edtUserName = (EditText) findViewById(R.id.edtUserName);
edtPassWd = (EditText) findViewById(R.id.edtPassWd);
edtConfirmPassWd = (EditText) findViewById(R.id.edtConfirmPassWd);
rdMale = (RadioButton) findViewById(R.id.rdMale);
rdFeMale = (RadioButton) findViewById(R.id.rdFeMale);
rdgGender = (RadioGroup) findViewById(R.id.rdgGender);
//注册按钮初始化
btnReg=(Button)findViewById(R.id.btnReg);
//评分条初始化
rtMarks1 = (RatingBar) findViewById(R.id.rtMarks1);
rtMarks2 = (RatingBar) findViewById(R.id.rtMarks2);
rtMarks3 = (RatingBar) findViewById(R.id.rtMarks3);
rtMarks4 = (RatingBar) findViewById(R.id.rtMarks4);
//省份城市的下拉列表
spinner1 = (Spinner) findViewById(R.id.s_main_spinner1);
spinner2 = (Spinner) findViewById(R.id.s_main_spinner2);
//多选框的初始化,注册监听事件
int chk_id[]={
R.id.checkbox1, R.id.checkbox2, R.id.checkbox3, R.id.checkbox4, R.id.checkbox5, R.id.checkbox6, R.id.checkbox7, R.id.checkbox8};
for (int id:chk_id) {
CheckBox chk= (CheckBox) findViewById(id);
chk.setOnCheckedChangeListener(this);
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.register);
initView();
rtb();//调用评分条方法
//用户名判断监听事件
edtUserName.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
String name = edtUserName.getText().toString();
if (name.length() <= 2) {
Toast.makeText(MainActivity.this, "用户名长度不能小于2", Toast.LENGTH_LONG).show();
}
}
});
//密码判断监听事件
edtPassWd.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
String firstPwd = edtPassWd.getText().toString();
if (firstPwd.length() < 6 || firstPwd.length() > 12) {
Toast.makeText(MainActivity.this, "密码长度必须在6~12位之间", Toast.LENGTH_LONG).show();
}
}
});
edtConfirmPassWd.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
String firstPwd = edtPassWd.getText().toString();
if (firstPwd.length() < 6 || firstPwd.length() > 12) {
Toast.makeText(MainActivity.this, "密码长度必须在6~12位之间", Toast.LENGTH_LONG).show();
}
}
});
//注册按钮的监听事件
btnReg.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (!(edtPassWd.getText().toString().equals(edtConfirmPassWd.getText().toString()))) {
Toast.makeText(MainActivity.this, "两次密码必须一致", Toast.LENGTH_SHORT).show();
isYes = false;
}
if (isYes) {
Toast.makeText(MainActivity.this, "注册成功!", Toast.LENGTH_SHORT).show();
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setMessage("确认注册?");
builder.setPositiveButton("是", new AlertDialog.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
} else {
Toast.makeText(MainActivity.this, "注册不成功!", Toast.LENGTH_SHORT).show();
}
}
});
//单选按钮的监听事件
rdgGender.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, @IdRes int checkedId) {
switch (checkedId) {
case R.id.rdMale:
Toast.makeText(MainActivity.this, "男", Toast.LENGTH_LONG).show();
case R.id.rdFeMale:
Toast.makeText(MainActivity.this, "女", Toast.LENGTH_LONG).show();
}
}
});
//获取省份城市数据
images = new int[]{
R.mipmap.ic_launcher, R.mipmap.ic_launcher, R.mipmap.ic_launcher};
province = new String[]{
"云南省", "河南省", "海南省"};
city0 = new String[]{
"昆明市", "丽江市", "大理市"};
city1 = new String[]{
"郑州市", "洛阳市", "信阳市"};
city2 = new String[]{
"海口市", "三亚市", "儋州市"};
list = new ArrayList<>();//数组列表存放数据
for (int i = 0; i < province.length; i++) {
//数据以hashMap的形式保存
Map<String, Object> map = new HashMap<>();
map.put("titles", province[i]);
map.put("images", images[i]);
map.put(province[0], city0[i]);
map.put(province[1], city1[i]);
map.put(province[2], city2[i]);
list.add(map);//数据源
}
//实例化适配器(simpleAdapter是一个装的类型比较宽泛的容器)
SimpleAdapter simpleAdapter = new SimpleAdapter(this, list, android.R.layout.activity_list_item, new String[]{
"titles", "images"}, new int[]{
android.R.id.text1, android.R.id.icon});
spinner1.setAdapter(simpleAdapter);//容器与下拉列表控件绑定在一起
//给下拉框设置点击事件
spinner1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
SimpleAdapter simpleAdapter2 = new SimpleAdapter(MainActivity.this, list, android.R.layout.activity_list_item, new String[]{
"images", province[i]}, new int[]{
android.R.id.icon, android.R.id.text1});
spinner2.setAdapter(simpleAdapter2);
}
@Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
}
//复选框按钮的监听事件(继承了接口并重写了方法implements CompoundButton.OnCheckedChangeListener)
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {
if(isChecked){
//选项被选取
selected.add(compoundButton); //添加到集合中
String msg = "";
for(CompoundButton chk : selected){
msg += chk.getText()+",";
}
Toast.makeText(this, "爱好:"+msg, Toast.LENGTH_SHORT).show();}
}
//评分条的方法
public void rtb(){
RatingBar rt[]={
rtMarks1,rtMarks2,rtMarks3,rtMarks4};
for(int i=0;i<4;i++) {
rt[i].setOnRatingBarChangeListener(new RatingBar.OnRatingBarChangeListener() {
@Override
public void onRatingChanged(RatingBar ratingBar, float rating, boolean fromUser) {
rating+=rating;
Toast.makeText(MainActivity.this, "星级:" + String.valueOf(rating),
Toast.LENGTH_LONG).show();
}
});
}
}
}