先上效果图:
验证码采用PHP返回的imageview来实现,xml如下:
<?xml version="1.0" encoding="utf-8"?> <ScrollView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="#f3f3f9"> <LinearLayout android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> <RelativeLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_margin="10dp"> <TextView android:id="@+id/tvUsrname" android:text="用户名" android:layout_width="wrap_content" android:layout_height="48dp" android:gravity="center_vertical" android:textColor="@android:color/black"/> <EditText android:id="@+id/etUsrname" android:layout_width="fill_parent" android:layout_height="48dp" android:layout_marginLeft="5dp" android:textSize="16sp" android:layout_toRightOf="@id/tvUsrname" android:textColor="@android:color/black" android:selectAllOnFocus="true" /> </RelativeLayout> <RelativeLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_margin="10dp"> <TextView android:id="@+id/tvPassword" android:text="密码" android:layout_width="wrap_content" android:layout_height="48dp" android:gravity="center_vertical" android:textColor="@android:color/black"/> <EditText android:id="@+id/etPassword" android:layout_width="fill_parent" android:layout_height="48dp" android:layout_marginLeft="5dp" android:layout_toRightOf="@id/tvPassword" android:textSize="16sp" android:password="true" android:textColor="@android:color/black" /> </RelativeLayout> <RelativeLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_margin="10dp"> <TextView android:id="@+id/tvPasswordConfirm" android:text="密码确认" android:layout_width="wrap_content" android:layout_height="48dp" android:gravity="center_vertical" android:textColor="@android:color/black"/> <EditText android:id="@+id/etPasswordConfirm" android:layout_width="fill_parent" android:layout_height="48dp" android:textSize="16sp" android:layout_toRightOf="@id/tvPasswordConfirm" android:password="true" android:textColor="@android:color/black" /> </RelativeLayout> <RelativeLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_margin="10dp"> <TextView android:id="@+id/tvNickName" android:text="nickname" android:layout_width="wrap_content" android:layout_height="48dp" android:gravity="center_vertical" android:textColor="@android:color/black"/> <EditText android:id="@+id/etNickName" android:layout_width="fill_parent" android:layout_height="48dp" android:layout_marginLeft="5dp" android:maxLines="2" android:textSize="16sp" android:layout_toRightOf="@id/tvNickName" android:textColor="@android:color/black" /> </RelativeLayout> <RelativeLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_margin="10dp"> <TextView android:id="@+id/tvGender" android:text="sex" android:textColor="#484848" android:layout_centerVertical="true" android:textSize="16dp" android:layout_width="wrap_content" android:layout_height="wrap_content"/> <RadioGroup android:id="@+id/rgSex" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginLeft="5dip" android:layout_toRightOf="@id/tvGender" android:layout_marginBottom="10dp" android:orientation="horizontal" > <RadioButton android:id="@+id/rbrgMale" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="@android:color/black" android:text="male" /> <RadioButton android:id="@+id/rbrgFemale" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="@android:color/black" android:text="female" android:layout_marginLeft="50dp" /> </RadioGroup> </RelativeLayout> <RelativeLayout android:layout_width="fill_parent" android:layout_height="wrap_content"> <ImageView android:id="@+id/ivAuthPic" android:src="@drawable/image1" android:layout_marginLeft="40dp" android:layout_alignParentLeft="true" android:layout_centerVertical="true" android:layout_width="100dp" android:layout_height="30dp"/> <Button android:id="@+id/btnChangeaAuthPic" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_gravity="right" android:layout_marginRight="10dp" android:text="看不清" android:textColor="#000"/> </RelativeLayout> <EditText android:id="@+id/etAuthCode" android:layout_width="fill_parent" android:layout_height="48dp" android:layout_margin="10dp" android:inputType="textVisiblePassword" android:textColor="@android:color/black"/> <Button android:id="@+id/btnOk" android:textSize="15sp" android:textColor="#fff" android:text="确认" android:background="@drawable/button_item_selector" android:layout_gravity="center" android:layout_height="wrap_content" android:layout_width="wrap_content"/> </LinearLayout> </ScrollView>
代码:
int[] array = new int[]{R.drawable.image1,R.drawable.image2,R.drawable.image3}; boolean isMale; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.registerquick); RadioButton rbMale = (RadioButton) findViewById(R.id.rbrgMale); RadioButton rbfMale = (RadioButton) findViewById(R.id.rbrgFemale); rbMale.setOnClickListener(new OnClickListener() { public void onClick(View v) { // TODO Auto-generated method stub isMale = true; } }); rbfMale.setOnClickListener(new OnClickListener() { public void onClick(View v) { // TODO Auto-generated method stub isMale = false; } }); final ImageView ivAuthPic = (ImageView)findViewById(R.id.ivAuthPic); Button btnChangeaAuthPic = (Button)findViewById(R.id.btnChangeaAuthPic); btnChangeaAuthPic.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Random d = new Random(); int k = Math.abs(d.nextInt()) % 3; ivAuthPic.setImageResource(array[k]); } }); }