话不多说,首先展示最终效果图:
主要涉及的组件有TextView,EditText,RadioButton,CheckBox,Spinner等
整个程序实现的内容:先把注册信息填写完毕,点击确认按钮,开始检查,填写是否符合要
求,包括:1.所有空都不能为空
2.密码长度不能小于8,联系电话长度只能为11
符合要求后,将整个界面的注册信息传给第二个界面
主要的会卡住的点有(1)如何判断单选框是否被选中
private int GetRadioButtonId(){
int i;
int count=sex.getChildCount();//获取radiogroup中子组件数目(radiobutton)
for( i = 0 ;i < count; i++)
{
RadioButton rb = (RadioButton)sex.getChildAt(i);//根据索引获取当前索引对应的RadioButton
if(rb.isChecked())//检查是否被选中
{
return i;
}
}
Toast.makeText(MainActivity.this,"请选择性别",Toast.LENGTH_SHORT).show();
return -1;//没有被选中的
}
主要是利用了三个方法:(1)getChildCount() 获取radiogroup中子组件数目
(2) getChildAt(i) 据索引获取当前索引对应的RadioButton
(3)isChecked 检查是否被选中
(2)获取多选框中值
private ArrayList<CheckBox> favs;
favs = new ArrayList<CheckBox>();
//将各项爱好对象存入数组
favs.add(book);
favs.add(movie);
favs.add(sport);
favs.add(music);
public String getFavorite(){
String favo="";
for(CheckBox cb : favs)
{
if(cb.isChecked())
{
favo += cb.getText().toString();
favo+=",";
}
}
return favo;
}
最后上代码:activity_main.xml
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/name"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<EditText
android:id="@+id/name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:autofillHints=""
android:inputType="date|textUri|textShortMessage|textLongMessage|textAutoCorrect|numberSigned|textVisiblePassword|textWebEditText|textMultiLine|textNoSuggestions|textFilter|number|datetime|textWebEmailAddress|textPersonName|text|textPhonetic|textCapSentences|textPassword|textAutoComplete|textImeMultiLine|textPostalAddress|numberDecimal|textEmailAddress|numberPassword|textCapWords|phone|textEmailSubject|textCapCharacters|time|textWebPassword" tools:targetApi="o"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/password"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<EditText
android:id="@+id/password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:autofillHints=""
android:inputType="textPassword" tools:targetApi="o"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/sex"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<RadioGroup
android:id="@+id/sex"
android:orientation="horizontal"
android:layout_height="wrap_content"
android:layout_width="match_parent">
<RadioButton
android:id="@+id/man"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="@string/man"
android:visibility="visible" />
<RadioButton
android:id="@+id/woman"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="@string/woman"
android:visibility="visible" />
</RadioGroup>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/phone"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<EditText
android:id="@+id/phone"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:autofillHints=""
android:inputType="text|phone" tools:targetApi="o"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/dept"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Spinner
android:id="@+id/dept"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:autofillHints=""
android:entries="@array/dept" tools:targetApi="o"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/hobby"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<LinearLayout
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<CheckBox
android:id="@+id/book"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/book"/>
<CheckBox
android:id="@+id/sport"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/sport"/>
<CheckBox
android:id="@+id/music"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/music"/>
<CheckBox
android:id="@+id/movie"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/movie"/>
</LinearLayout>
<Button
android:id="@+id/sure"
android:layout_gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/sure" />
</LinearLayout>
activity_second.xml代码如下:
<TextView
android:id="@+id/res"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:importantForAutofill="no" />
<Button
android:id="@+id/ok"
android:layout_gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/ok"
/>
MainActivity.xml代码如下:
private Button sure;
private EditText name,password,phone;
private RadioGroup sex;
private CheckBox book,music,movie,sport;
private Spinner dept;
private ArrayList<CheckBox> favs;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
this.sure=(Button) findViewById(R.id.sure);
this.sex = (RadioGroup)findViewById(R.id.sex);
this.name=(EditText)findViewById(R.id.name);
this.password=(EditText)findViewById(R.id.password);
this.phone=(EditText)findViewById(R.id.phone);
this.book=(CheckBox)findViewById(R.id.book);
this.music=(CheckBox)findViewById(R.id.music);
this.movie=(CheckBox)findViewById(R.id.movie);
this.sport=(CheckBox)findViewById(R.id.sport);
this.dept=(Spinner)findViewById(R.id.dept);
favs = new ArrayList<CheckBox>();
//将各项爱好对象存入数组
favs.add(book);
favs.add(movie);
favs.add(sport);
favs.add(music);
//监听确认键,检查是否填写正确
sure.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(IsChecked()==1)
{
StringBuilder sb = new StringBuilder();
sb.append("用户名:"+name.getText().toString()+"\\n");
if(GetRadioButtonId()==1)sb.append("性别:女\\n");
else sb.append("性别:男\\n");
sb.append("联系电话:"+phone.getText().toString()+"\\n");
sb.append("部门:"+dept.getSelectedItem()+"\\n");
sb.append("爱好:"+getFavorite()+"\\n");
Toast.makeText(MainActivity.this,sb.toString(),Toast.LENGTH_SHORT).show();
Intent intent = new Intent();
intent.setClass(MainActivity.this,SecondActivity.class);
intent.putExtra("info",sb.toString());
MainActivity.this.startActivity(intent);
}
}
});
}
//获取爱好
public String getFavorite(){
String favo="";
for(CheckBox cb : favs)
{
if(cb.isChecked())
{
favo += cb.getText().toString();
favo+=",";
}
}
return favo;
}
//判断用户名是否为空 0为空,1不为空
private int Checkname()
{
String na = name.getText().toString();
if(na.isEmpty())
{
Toast.makeText(MainActivity.this,"用户名不能为空",Toast.LENGTH_SHORT).show();
return 0;
}
return 1;
}
//判断密码是否为空,并且超过八位 0为不满足,1为满足条件
private int Checkpassword()
{
String na = password.getText().toString();
if(na.isEmpty())
{
Toast.makeText(MainActivity.this,"密码不能为空",Toast.LENGTH_SHORT).show();
return 0;
}
if(na.length()<8)
{
Toast.makeText(MainActivity.this,"密码长度不能小于8",Toast.LENGTH_SHORT).show();
return 0;
}
return 1;
}
//判断联系电话不为空且为长度为11
private int Checkphone()
{
String na = phone.getText().toString();
if(na.isEmpty())
{
Toast.makeText(MainActivity.this,"联系电话不能为空",Toast.LENGTH_SHORT).show();
return 0;
}
if(na.length()!=11)
{
Toast.makeText(MainActivity.this,"请输入11位数字的联系电话",Toast.LENGTH_SHORT).show();
return 0;
}
return 1;
}
//获取所选的单选按钮序号,没选则返回0
private int GetRadioButtonId(){
int i;
int count=sex.getChildCount();//获取radiogroup中子组件数目(radiobutton)
for( i = 0 ;i < count; i++)//0为男 1为女按钮顺序
{
RadioButton rb = (RadioButton)sex.getChildAt(i);//根据索引获取当前索引对应的radioButton
if(rb.isChecked())//检查是否被选中
{
return i;
}
}
Toast.makeText(MainActivity.this,"请选择性别",Toast.LENGTH_SHORT).show();
return -1;//没有被选中的
}
//判断多选框
private int CheckCheckBox(){
if((book.isChecked())||(music.isChecked())||(sport.isChecked())||(movie.isChecked()))
return 1;
else
{
Toast.makeText(MainActivity.this,"请选择爱好",Toast.LENGTH_SHORT).show();
return 0;
}
}
//总的判断函数
private int IsChecked( )
{
if((Checkname()==1)&&(Checkpassword()==1)&&(GetRadioButtonId()!=-1)&&(Checkphone()==1)&&(CheckCheckBox()==1))
return 1;
return 0;
}
SecondActivity.xml代码如下:
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
Intent intent =getIntent();
String string =new String();
string =intent.getStringExtra("info");
this.result = (TextView) findViewById(R.id.res);
result.setText("注册内容如下:\\n\\n"+string);
this.ok = (Button)findViewById(R.id.ok);
ok.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
finish();
}
});
}
整个实验就是把之前的做了一个总结,易错点:
(1)新建activit时,不要忘了在文件中注册
(2)用到的每个组件都不要忘了findViewById(R.id.xml文件中对应的id)定位一下
(3)另外,TextView可以实现自动换行,而EditText则还需要进行一些操作才可以
(4)在xml文件中用到android:OnClicked=XXX时,记得方法一定是public
好啦,结束啦!