package com.example.demo;
import android.os.Bundle;
import android.app.Activity;
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.CompoundButton.OnCheckedChangeListener;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Spinner;
public class MainActivity extends Activity {
private EditText account;
private EditText psw;
private RadioGroup gender;
private Spinner classroom;
private Button loginButton;
private CheckBox ck1;
private CheckBox ck2;
private CheckBox ck3;
private String myac;
private String mypsw;
private String mygender;
private String myclassroom;
private int ckTag1 = 0;
private int ckTag2 = 0;
private int ckTag3 = 0;
private String toastMsg;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
}
private void init() {
account = (EditText) this.findViewById(R.id.account);
psw = (EditText) this.findViewById(R.id.psw);
gender = (RadioGroup) this.findViewById(R.id.gender);
classroom = (Spinner) this.findViewById(R.id.classroom);
loginButton = (Button) this.findViewById(R.id.loginBtn);
ck1 = (CheckBox) this.findViewById(R.id.ck1);
ck2 = (CheckBox) this.findViewById(R.id.ck2);
ck3 = (CheckBox) this.findViewById(R.id.ck3);
ck1.setOnCheckedChangeListener(ckListener);
ck2.setOnCheckedChangeListener(ckListener);
ck3.setOnCheckedChangeListener(ckListener);
gender.setOnCheckedChangeListener(genderListener);
loginButton.setOnClickListener(loginBtnListener);
}
OnCheckedChangeListener ckListener = new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton arg0, boolean arg1) {
if (ck1.isChecked()) {
ckTag1 = 1;
}
if (!ck1.isChecked()) {
ckTag1 = 0;
}
if (ck2.isChecked()) {
ckTag2 = 1;
}
if (!ck2.isChecked()) {
ckTag2 = 0;
}
if (ck3.isChecked()) {
ckTag3 = 1;
}
if (!ck3.isChecked()) {
ckTag3 = 0;
}
}
};
android.widget.RadioGroup.OnCheckedChangeListener genderListener = new android.widget.RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup arg0, int arg1) {
int id = arg0.getCheckedRadioButtonId();
RadioButton rad = (RadioButton) findViewById(id);
mygender = rad.getText().toString();
}
};
OnClickListener loginBtnListener = new OnClickListener() {
@Override
public void onClick(View arg0) {
if (arg0.getId() == R.id.loginBtn) {
StringBuffer sa = new StringBuffer();
if (ckTag1 == 1) {
sa.append(ck1.getText().toString()).append(",");
}
if (ckTag2 == 1) {
sa.append(ck2.getText().toString()).append(",");
}
if (ckTag3 == 1) {
sa.append(ck3.getText().toString()).append(",");
}
toastMsg = sa.toString().substring(0,
sa.toString().length() - 1);
}
}
};
@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;
}
}
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:orientation="horizontal" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="账号" />
<EditText
android:id="@+id/account"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="请输入账号" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:orientation="horizontal" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="密码" />
<EditText
android:id="@+id/psw"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="请输入密码" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:orientation="horizontal" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="性别" />
<RadioGroup
android:id="@+id/gender"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="男" />
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="男" />
</RadioGroup>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:orientation="horizontal" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="爱好" />
<CheckBox
android:id="@+id/ck1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="唱" />
<CheckBox
android:id="@+id/ck2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="跳" />
<CheckBox
android:id="@+id/ck3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Rap" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:orientation="horizontal" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="班级" />
<Spinner
android:id="@+id/classroom"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:orientation="horizontal" >
<Button
android:id="@+id/loginBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="登录" />
</LinearLayout>
</LinearLayout>
</RelativeLayout>