安卓编程吧 关注:34,852贴子:335,071
  • 0回复贴,共1

找大神求救~!!下面这个for(CheckBox cb:list){地方出错了...改

只看楼主收藏回复

package com.example.test;
import java.util.ArrayList;
import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
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.SeekBar;
import android.widget.TextView;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.Toast;
public class MainActivity<Checkbox> extends Activity {
private EditText username,password;
private RadioGroup radioGroup;
private RadioButton man,woman;
private CheckBox checkBox1,checkBox2,checkBox3,checkBox4;
private Button button1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.test_layout);
username = (EditText)findViewById(R.id.username);
password = (EditText)findViewById(R.id.password);
man=(RadioButton)findViewById(R.id.man);
woman=(RadioButton)findViewById(R.id.woman);
radioGroup=(RadioGroup)findViewById(R.id.radioGroup);
checkBox1 =(CheckBox)findViewById(R.id.checkBox1);
checkBox2 =(CheckBox)findViewById(R.id.checkBox2);
checkBox3 =(CheckBox)findViewById(R.id.checkBox3);
checkBox4 =(CheckBox)findViewById(R.id.checkBox4);
button1 =(Button)findViewById(R.id.button1);
ArrayList<Checkbox> list = new ArrayList<Checkbox>();
list.add((Checkbox) checkBox1);
list.add((Checkbox) checkBox2);
list.add((Checkbox) checkBox3);
list.add((Checkbox) checkBox4);
username.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(username.length() <= 0 && password.length() <= 0){
Toast toast = Toast.makeText(MainActivity.this, "用户名密码为空",0);
toast.show();
}else if(username.length() <=0){
username.setError("用户名不能为空");
}else if(password.length() <=0){
password.setError("密码不能为空");
}
}});
button1.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
//showInfo.setText("您已完成注册"+button1.getText());
if (checkUser()){
new AlertDialog.Builder(MainActivity.this)
.setTitle("请确认信息")
.setMessage("您确认信息如下:\n"+"姓名:"+username.getText().toString()
+"\n"+"性别:"+getSex()
+"\n"+"爱好:"+getFavourite())
.setCancelable(false)
.setPositiveButton("确定",new DialogInterface.OnClickListener(){
@Override
public void onClick(DialogInterface Dialog,int which){
ProgressDialog.show(MainActivity.this,"用户信息注册中","请等待……")
.setCancelable(true);
}
})
.setNegativeButton("取消",new DialogInterface.OnClickListener(){
@Override
public void onClick(DialogInterface dialog,int which){
dialog.cancel();
}
public void onClick1(DialogInterface arg0, int arg1) {
// TODO Auto-generated method stub
}
})
.show();
}
}});
}
protected String getSex() {
// TODO Auto-generated method stub
RadioButton rb = (RadioButton) findViewById(radioGroup.getCheckedRadioButtonId());
return rb.getText().toString();
}
protected String getFavourite() {
// TODO Auto-generated method stub
StringBuffer sb = new StringBuffer();
for(CheckBox cb:list){
if(cb.isChecked()){
String cont = cb.getText().toString();
sb.append(cont+",");
}
}
if(sb.length()==0){
return"您暂时没有爱好";
}else{
return sb.substring(0,sb.length()-1);
}
}
protected boolean checkUser() {
// TODO Auto-generated method stub
String uname = username.getText().toString();
if(uname.trim().length() == 0){
username.setError("用户名不能为空");
return false;
}
String upass = password.getText().toString();
if(upass.trim().length() == 0){
password.setError("密码不能为空");
return false;
}
return true;
}
protected void DisplayToast(String string){
// TODO Auto-generated method stub
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}I


1楼2013-10-31 19:01回复