表單(單選,複選,文本輸入)

Layout


<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"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">
             <LinearLayout
	            android:layout_width="match_parent"
	            android:layout_height="wrap_content"
	            android:orientation="horizontal">
	            <TextView
	                android:layout_width="wrap_content"
	                android:layout_height="wrap_content"
	                android:textSize="20dp"
	                android:text="用戶名:"/>
	            <EditText
	                android:id="@+id/userName"
	                android:layout_width="wrap_content"
	                android:layout_height="wrap_content"
	                android:hint="請輸入用戶名"/>
	         </LinearLayout>
	         <LinearLayout
	            android:layout_width="match_parent"
	            android:layout_height="wrap_content"
	            android:orientation="horizontal">
	            <TextView
	                android:layout_width="wrap_content"
	                android:layout_height="wrap_content"
	                android:textSize="20dp"
	                android:text="密    碼:"/>
	            <EditText
	                android:id="@+id/password"
	                android:layout_width="wrap_content"
	                android:layout_height="wrap_content"
	                android:hint="請輸入密碼"/>
	        </LinearLayout>
	        <LinearLayout
	            android:layout_width="match_parent"
	            android:layout_height="wrap_content"
	            android:orientation="horizontal">
	            <TextView
	                android:layout_width="wrap_content"
	                android:layout_height="wrap_content"
	                android:textSize="20dp"
	                android:text="年    齡:"/>
	            <EditText
	                android:id="@+id/age"
	                android:layout_width="wrap_content"
	                android:layout_height="wrap_content"
	                android:hint="請輸入年齡"/>
	        </LinearLayout>
        </LinearLayout>
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">
	        <TextView
	            android:layout_width="wrap_content"
	            android:layout_height="wrap_content"
	            android:textSize="20dp"
	            android:text="性    別:"/>
	        <RadioGroup
	            android:id="@+id/sex"
	            android:layout_width="wrap_content"
	            android:layout_height="wrap_content"
	            android:orientation="horizontal">
	            <RadioButton
		            android:id="@+id/girl"
		            android:layout_width="wrap_content"
		            android:layout_height="wrap_content"
		            android:textSize="20dp"
		            android:checked="false"
		            android:text="Girl"/>
	            <RadioButton
	                android:id="@+id/boy"
	                android:layout_width="wrap_content"
	                android:layout_height="wrap_content"
	                android:textSize="20dp"
	                android:checked="false"
	                android:text="Boy"/>
	         </RadioGroup>
	     </LinearLayout>
	     <LinearLayout
	         android:layout_width="match_parent"
             android:layout_height="wrap_content"
             android:orientation="horizontal">
	        <TextView
	            android:layout_width="wrap_content"
	            android:layout_height="wrap_content"
	            android:textSize="20dp"
	            android:text="愛    好:"/>
	        <LinearLayout
		         android:layout_width="match_parent"
	             android:layout_height="wrap_content"
	             android:orientation="vertical">
		        <CheckBox
		            android:id="@+id/boxRead"
		            android:layout_width="wrap_content"
		            android:layout_height="wrap_content"
		            android:checked="false"
		            android:textSize="20dp"
		            android:text="讀書"/>
		        <CheckBox
		            android:id="@+id/boxFootball"
		            android:layout_width="wrap_content"
		            android:layout_height="wrap_content"
		            android:checked="false"
		            android:textSize="20dp"
		            android:text="足球"/>
		        <CheckBox
		            android:id="@+id/boxBasketball"
		            android:layout_width="wrap_content"
		            android:layout_height="wrap_content"
		            android:checked="false"
		            android:textSize="20dp"
		            android:text="籃球"/>
		      </LinearLayout>
	     </LinearLayout>
	     <LinearLayout
	         android:layout_width="match_parent"
             android:layout_height="wrap_content"
             android:gravity="center"
             android:orientation="horizontal">
	        <Button
	            android:id="@+id/regedit"
	            android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="註冊"/>
	        <Button
	            android:id="@+id/clean"
	            android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="清空"/>
	    </LinearLayout>
    </LinearLayout>

</RelativeLayout>

java


package com.example.choose;

import android.os.Bundle;
import android.view.View;
import android.app.Activity;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.Toast;

public class MainActivity extends Activity {
	
	EditText edUserName,edPassword,edAge;
	Button btRegedit,btClean;
	RadioButton rbGirl,rbBoy;
	CheckBox cbRead,cbFootball,cbBasketball;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		init();
		btRegedit.setOnClickListener(new View.OnClickListener(){
			@Override
			public void onClick(View v){
				String userName="",password="",age="",sex="",hobby="";
				userName=edUserName.getText().toString().trim();
				password=edPassword.getText().toString().trim();
				age=edAge.getText().toString().trim();
				if(age.equals("")||password.equals("")||userName.equals(""))
					Toast.makeText(getApplicationContext(), "請填寫完整信息",Toast.LENGTH_SHORT).show();
				if(rbGirl.isChecked())
					sex="Girl";
				if(rbBoy.isChecked())
					sex="Boy";
				if(cbRead.isChecked())
					hobby=hobby+"reading"+"\n";
				if(cbFootball.isChecked())
					hobby=hobby+"football"+"\n";
				if(cbBasketball.isChecked())
					hobby=hobby+"basketball";
				Toast.makeText(getApplicationContext(),"你註冊的信息是:\n"+
				        userName+"\n"+
						password+"\n"+
				        age+"\n"+
						sex+"\n"+
				        hobby+"\n", Toast.LENGTH_LONG).show();
			}
		});
		btClean.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				edUserName.setText("");
				edPassword.setText("");
				edAge.setText("");
				rbGirl.setChecked(false);
				rbBoy.setChecked(false);
				cbRead.setChecked(false);
				cbFootball.setChecked(false);
				cbBasketball.setChecked(false);
			}
		});
	}

    private void init(){
    	edUserName=(EditText)findViewById(R.id.userName);
    	edPassword=(EditText)findViewById(R.id.password);
    	edAge=(EditText)findViewById(R.id.age);
    	btRegedit=(Button)findViewById(R.id.regedit);
    	btClean=(Button)findViewById(R.id.clean);
    	rbGirl=(RadioButton)findViewById(R.id.girl);
    	rbBoy=(RadioButton)findViewById(R.id.boy);
    	cbRead=(CheckBox)findViewById(R.id.boxRead);
    	cbFootball=(CheckBox)findViewById(R.id.boxFootball);
    	cbBasketball=(CheckBox)findViewById(R.id.boxBasketball);
    }

}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章