<LinearLayout 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" android:orientation="vertical"> <EditText android:id="@+id/userName" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="请输入用户名"/> <EditText android:id="@+id/password" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="请输入密码"/> <RadioGroup android:id="@+id/sex" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal" android:layout_gravity="center_horizontal"> <RadioButton android:id="@+id/man" android:layout_width="wrap_content" android:layout_height="wrap_content" android:checked="true" android:text="男生" android:layout_marginRight="50dp"/> <RadioButton android:id="@+id/woman" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="女生"/> </RadioGroup> <LinearLayout android:id="@+id/birthDate" android:layout_width="match_parent" android:layout_height="50dp" android:orientation="horizontal" android:gravity="center_horizontal"> <EditText android:id="@+id/birth_year" android:layout_width="80dp" android:layout_height="wrap_content" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="年"/> <Spinner android:id="@+id/birth_month" android:layout_width="30dp" android:layout_height="wrap_content"></Spinner> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="月"/> <Spinner android:id="@+id/birth_day" android:layout_width="30dp" android:layout_height="wrap_content"></Spinner> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="日"/> </LinearLayout> <LinearLayout android:id="@+id/loveBall" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:gravity="center_horizontal"> <CheckBox android:id="@+id/basketball" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="篮球"/> <CheckBox android:id="@+id/football" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="足球"/> <CheckBox android:id="@+id/pingpang" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="乒乓球"/> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center_horizontal"> <Button android:id="@+id/login" android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="login" android:text="登录"/> <Button android:id="@+id/reset" android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="reset" android:text="重置"/> </LinearLayout> </LinearLayout>
public class MainActivity extends AppCompatActivity { private TextView textView_userName, textView_password, textview_birthyear; private RadioGroup radioGroup_sex; private Spinner spinner_month; private Spinner spinner_day; private CheckBox[] checkbox_loveball; private RadioButton radioButton_man, radioButton_woman; private CheckBox checkBox_basketball, checkBox_football, checkBox_pingpang; private String userName, password, sex, birth_year, birth_month, birth_day, love_ball; private List<String> birthMonth_list; private List<String> birthDay_list; private ArrayAdapter<String> month_adapter; private ArrayAdapter<String> day_adapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initView(); } //初始化页面 private void initView() { textView_userName = (TextView) findViewById(R.id.userName); textView_password = (TextView) findViewById(R.id.password); radioGroup_sex = (RadioGroup) findViewById(R.id.sex); radioButton_man = (RadioButton) findViewById(R.id.man); radioButton_woman = (RadioButton) findViewById(R.id.woman); checkBox_basketball = (CheckBox) findViewById(R.id.basketball); checkBox_football = (CheckBox) findViewById(R.id.football); checkBox_pingpang = (CheckBox) findViewById(R.id.pingpang); textview_birthyear = (TextView) findViewById(R.id.birth_year); //初始化月 spinner_month = (Spinner) findViewById(R.id.birth_month); birthMonth_list = new ArrayList<String>(); for (int i = 1; i <= 12; i++) { birthMonth_list.add(i + ""); } month_adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, birthMonth_list); spinner_month.setAdapter(month_adapter); spinner_month.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { @Override public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { birth_month = ((TextView) view).getText().toString(); } @Override public void onNothingSelected(AdapterView<?> parent) { } }); //初始化日 spinner_day = (Spinner) findViewById(R.id.birth_day); birthDay_list = new ArrayList<String>(); for (int i = 0; i <= 31; i++) { birthDay_list.add(i + ""); } day_adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, birthDay_list); spinner_day.setAdapter(day_adapter); spinner_day.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { @Override public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { birth_day = ((TextView) view).getText().toString(); } @Override public void onNothingSelected(AdapterView<?> parent) { } }); //初始化多选框 checkbox_loveball = new CheckBox[3]; checkbox_loveball[0] = (CheckBox) findViewById(R.id.basketball); checkbox_loveball[1] = (CheckBox) findViewById(R.id.football); checkbox_loveball[2] = (CheckBox) findViewById(R.id.pingpang); } //登录 public void login(View view) { userName = textView_userName.getText().toString(); password = textView_password.getText().toString(); for (int i = 0; i < radioGroup_sex.getChildCount(); i++) { RadioButton rb = (RadioButton) radioGroup_sex.getChildAt(i); if (rb.isChecked()) { sex = rb.getText().toString(); } } birth_year = textview_birthyear.getText().toString(); love_ball = ""; for (int i = 0; i < checkbox_loveball.length; i++) { if (checkbox_loveball[i].isChecked()) { love_ball += checkbox_loveball[i].getText().toString() + " "; } } if ("admin".equals(userName) && "123456".equals(password)) { Toast.makeText(this, "登录成功" + "\n用户名:" + userName + "\n密码:" + password + "\n性别:" + sex + "\n出生日期:" + birth_year + "年" + birth_month + "月" + birth_day + "日" + "\n爱好:" + love_ball, Toast.LENGTH_SHORT).show(); } else { Toast.makeText(this, "登录失败", Toast.LENGTH_SHORT).show(); } } //重置 public void reset(View view) { textView_userName.setText(""); textView_password.setText(""); textview_birthyear.setText(""); radioButton_man.setChecked(true); radioButton_woman.setChecked(false); checkBox_basketball.setChecked(false); checkBox_football.setChecked(false); checkBox_pingpang.setChecked(false); } }
时间: 2024-10-14 11:16:38