<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" > <TextView android:id="@+id/tv" android:layout_width="match_parent" android:layout_height="50dp" android:clickable="true" android:onClick="onclick" android:text="hello android" /> <ImageView android:id="@+id/iv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="onclick" android:src="@drawable/ic_launcher" /> <RadioGroup android:id="@+id/rg" android:layout_width="match_parent" android:layout_height="wrap_content" android:onClick="onclick" android:orientation="horizontal" > <RadioButton android:id="@+id/rbMale" android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="onclick" android:text="男" /> <RadioButton android:id="@+id/rbFemale" android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="onclick" android:text="女" /> <CheckBox android:id="@+id/chkMale" android:layout_width="wrap_content" android:layout_height="wrap_content" android:checked="true" android:onClick="onclick" android:text="变男" /> <CheckBox android:id="@+id/chkFemale" android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="onclick" android:text="变女" /> </RadioGroup> </LinearLayout>
package com.sxt.day03_01; import android.app.Activity; import android.graphics.Color; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.CheckBox; import android.widget.ImageView; import android.widget.RadioButton; import android.widget.RadioGroup; import android.widget.RadioGroup.OnCheckedChangeListener; import android.widget.TextView; public class MainActivity extends Activity { RadioButton mrbMale,mrbFemale; RadioGroup mrg; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initView(); } private void initView() { mrbMale=(RadioButton) findViewById(R.id.rbMale); mrbFemale=(RadioButton) findViewById(R.id.rbFemale); mrg=(RadioGroup) findViewById(R.id.rg); setListener(); } private void setListener() { mrg.setOnCheckedChangeListener(new OnCheckedChangeListener() { @Override public void onCheckedChanged(RadioGroup group, int checkedId) { switch (checkedId) { case R.id.chkMale: mrbMale.setChecked(true); break; case R.id.chkFemale: mrbFemale.setChecked(true); break; } } }); } public void onclick(View v){ switch (v.getId()) { case R.id.tv: TextView tv=(TextView) v; Log.i("main", tv.getText().toString()); tv.setTextColor(Color.RED); break; case R.id.iv: ImageView iv=(ImageView) v; iv.setImageResource(R.drawable.p01); break; case R.id.chkMale: CheckBox chkMale=(CheckBox) v; mrbMale.setChecked(chkMale.isChecked()); mrbFemale.setChecked(!chkMale.isChecked()); break; case R.id.chkFemale: CheckBox chkFemale=(CheckBox) v; mrbFemale.setChecked(chkFemale.isChecked()); mrbMale.setChecked(!chkFemale.isChecked()); break; } } }
注册:
<?xml version="1.0" encoding="utf-8"?> <GridLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal" android:columnCount="5"> <TextView android:text="用 户 名"/> <EditText android:id="@+id/etUserName" android:hint="2-10个字符" android:layout_columnSpan="4" android:layout_gravity="fill_horizontal"/> <TextView android:text="输入密码"/> <EditText android:id="@+id/etPwd" android:hint="2-10个字符" android:layout_columnSpan="4" android:password="true" android:layout_gravity="fill_horizontal"/> <TextView android:text="确认密码"/> <EditText android:id="@+id/etFirmPwd" android:hint="2-10个字符" android:layout_columnSpan="4" android:password="true" android:layout_gravity="fill_horizontal"/> <TextView android:layout_marginTop="10dp" android:text="选择性别"/> <RadioGroup android:layout_marginLeft="10dp" android:layout_columnSpan="4" android:layout_gravity="fill_horizontal" android:orientation="horizontal"> <RadioButton android:id="@+id/rbMale" android:text="男" android:checked="true"/> <RadioButton android:id="@+id/rbFemale" android:text="女"/> </RadioGroup> <TextView android:text="所在地"/> <Spinner android:id="@+id/spinCity" android:layout_columnSpan="4" android:entries="@array/citys"/> <TextView android:layout_marginTop="10dp" android:text="选择爱好"/> <RadioGroup android:layout_columnSpan="4" android:orientation="horizontal"> <CheckBox android:id="@+id/chkReadBook" android:text="读书" android:checked="true"/> <CheckBox android:id="@+id/chkTour" android:text="旅游"/> <CheckBox android:id="@+id/chkGame" android:text="电玩"/> </RadioGroup> <Button android:visibility="invisible"/> <Button android:id="@+id/btnRegister" android:onClick="onClick" android:text="注册" android:textColor="#fff" android:padding="3dp" android:drawableLeft="@drawable/login32x32" android:background="@drawable/btn_bg"/> <Button android:visibility="invisible"/> <Button android:id="@+id/btnExit" android:onClick="onClick" android:text="退出" android:textColor="#fff" android:padding="3dp" android:drawableLeft="@drawable/exit32x32" android:background="@drawable/btn_bg"/> <Button android:visibility="invisible"/> </GridLayout>
package com.sxt.day03_02_registerdemo; import com.sxt.day03_02_registerdemo.entity.User; import android.os.Bundle; import android.app.Activity; import android.text.TextUtils; import android.util.Log; import android.view.Menu; import android.view.View; import android.widget.CheckBox; import android.widget.EditText; import android.widget.RadioButton; import android.widget.Spinner; import android.widget.Toast; public class MainActivity extends Activity { EditText metUserName,metPwd,metFirmPwd; RadioButton mrbMale,mrbFemale; CheckBox mchkReadBook,mchkGame,mchkTour; Spinner mspinCity; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initView(); } private void initView() { metFirmPwd=(EditText) findViewById(R.id.etFirmPwd); metPwd=(EditText) findViewById(R.id.etPwd); metUserName=(EditText) findViewById(R.id.etUserName); mrbFemale=(RadioButton) findViewById(R.id.rbFemale); mrbMale=(RadioButton) findViewById(R.id.rbMale); mchkGame=(CheckBox) findViewById(R.id.chkGame); mchkReadBook=(CheckBox) findViewById(R.id.chkReadBook); mchkTour=(CheckBox) findViewById(R.id.chkTour); mspinCity=(Spinner) findViewById(R.id.spinCity); } public void onClick(View v){ switch (v.getId()) { case R.id.btnExit: finish();//关闭当前的Activity break; case R.id.btnRegister: String name=metUserName.getText().toString(); if(TextUtils.isEmpty(name)){ Toast.makeText(this, "用户名不能为空", 2000).show(); return ; } String pwd=metPwd.getText().toString(); if(TextUtils.isEmpty(pwd)){ metPwd.setError("请输出密码"); return ; } String firmPwd=metFirmPwd.getText().toString(); if(TextUtils.isEmpty(firmPwd)){ metFirmPwd.setError("确认密码不能为空"); return ; } if(!pwd.equals(firmPwd)){ Toast.makeText(this, "确认密码与密码不一致", 2000).show(); return ; } char sex; if(mrbMale.isChecked()){ sex=mrbMale.getText().toString().charAt(0); }else{ sex=mrbFemale.getText().toString().charAt(0); } StringBuilder sb=new StringBuilder(); if(mchkGame.isChecked()){ sb.append(mchkGame.getText().toString()); } if(mchkReadBook.isChecked()){ sb.append(",").append(mchkReadBook.getText().toString()); } if(mchkTour.isChecked()){ sb.append(",").append(mchkTour.getText().toString()); } String city=mspinCity.getSelectedItem().toString(); User user=new User(name, pwd, sex, sb.toString(), city); Toast.makeText(this, "注册用户:"+user.toString(), 4000).show(); Log.i("main",user.toString()); break; } } }
public class User { private String name; private String pwd; private char sex; private String hobby;//爱好 private String city; public String getName() { retu