android用户界面-组件Widget-常用组件

用户会员注册实例  

介绍控件 

文本框TextView

编辑框EditText

密码文本框EditText

单选按钮RadioButton

复选框CheckBox

开关按钮ToggleButton

下拉列表Spinner

实例:

注册页面

/Chapter04_UI_CommonWidget/src/com/amaker/test/MainActivity.java

代码
   
     
package com.amaker.test;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.Spinner;
import android.widget.ToggleButton;

public class MainActivity extends Activity {

private Button register,cancel;
private ToggleButton marriged;
private RadioButton male,female;
private EditText username,password;
private Spinner position;
private CheckBox reading,swimming;

@Override
public void onCreate(Bundle savedInstanceState) {
super .onCreate(savedInstanceState);
setContentView(R.layout.main);

username
= (EditText)findViewById(R.id.username);
password
= (EditText)findViewById(R.id.password);

male
= (RadioButton)findViewById(R.id.male);
female
= (RadioButton)findViewById(R.id.female);

reading
= (CheckBox)findViewById(R.id.reading);
swimming
= (CheckBox)findViewById(R.id.swimming);

marriged
= (ToggleButton)findViewById(R.id.marriged);

position
= (Spinner)findViewById(R.id.position);

String[] str
= { " CEO " , " CFO " , " PM " };

ArrayAdapter aa
= new ArrayAdapter( this , android.R.layout.simple_spinner_dropdown_item,str);

position.setAdapter(aa);

register
= (Button)findViewById(R.id.register);
cancel
= (Button)findViewById(R.id.cancel);

register.setOnClickListener(
new OnClickListener() {
public void onClick(View v) {
Bundle b
= new Bundle();
b.putString(
" username " , " 用户名称: " + username.getText().toString());
b.putString(
" password " , " 用户密码: " + password.getText().toString());

if (male.isChecked()){
b.putString(
" gender " , " 性别:男 " );
}
else {
b.putString(
" gender " , " 性别:女 " );
}
String temp
= " 爱好: " ;
if (reading.isChecked()){
temp
+= " 阅读 " ;
}
if (swimming.isChecked()){
temp
+= " " ;
temp
+= " 游泳 " ;
}

b.putString(
" hobby " , temp);

if (marriged.isChecked()){
b.putString(
" marriged " , " 婚否:已婚 " );
}
else {
b.putString(
" marriged " , " 婚否:未婚 " );
}

b.putString(
" position " , " 职位: " + position.getSelectedItem().toString());

Intent intent
= new Intent(MainActivity. this ,ResultActivity. class );

intent.putExtra(
" data " , b);

startActivity(intent);
}
});

}
}

注册结果页面

/Chapter04_UI_CommonWidget/src/com/amaker/test/ResultActivity.java

代码
   
     
package com.amaker.test;

import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class ResultActivity extends Activity{
private ListView listView;

protected void onCreate(Bundle savedInstanceState) {
super .onCreate(savedInstanceState);

setContentView(R.layout.result);
listView
= (ListView) findViewById(R.id.ListView01);

Intent intent
= this .getIntent();

Bundle b
= intent.getBundleExtra( " data " );

System.out.println(b.getString(
" username " ));

List list
= new ArrayList();

list.add(b.getString(
" username " ));
list.add(b.getString(
" password " ));
list.add(b.getString(
" position " ));

list.add(b.getString(
" gender " ));
list.add(b.getString(
" hobby " ));
list.add(b.getString(
" marriged " ));

ArrayAdapter adapter
= new ArrayAdapter( this ,android.R.layout.simple_list_item_1,list);

listView.setAdapter(adapter);

}
}

 

布局文件

/Chapter04_UI_CommonWidget/res/layout/main.xml

代码
   
     
<? xml version="1.0" encoding="utf-8" ?>
< LinearLayout xmlns:android ="http://schemas.android.com/apk/res/android"
android:orientation
="vertical"
android:layout_width
="fill_parent"
android:layout_height
="fill_parent"
>



< TableLayout
android:id ="@+id/TableLayout01"
android:layout_width
="wrap_content"
android:layout_height
="wrap_content"
android:stretchColumns
="1"
>


< TableRow
android:id ="@+id/TableRow01"
android:layout_width
="wrap_content"
android:layout_height
="wrap_content" >
< TextView
android:text ="用户名称"
android:id
="@+id/TextView01"
android:layout_width
="wrap_content"
android:layout_height
="wrap_content" ></ TextView >

< EditText
android:text =""
android:id
="@+id/username"
android:layout_width
="wrap_content"
android:layout_height
="wrap_content"

></ EditText >
</ TableRow >

< TableRow
android:id ="@+id/TableRow02"
android:layout_width
="wrap_content"
android:layout_height
="wrap_content" >
< TextView
android:text ="用户密码"
android:id
="@+id/TextView02"
android:layout_width
="wrap_content"
android:layout_height
="wrap_content" ></ TextView >

< EditText
android:text =""
android:id
="@+id/password"
android:layout_width
="wrap_content"
android:layout_height
="wrap_content"
android:password
="true"

></ EditText >
</ TableRow >

< TableRow
android:id ="@+id/TableRow03"
android:layout_width
="wrap_content"
android:layout_height
="wrap_content" >
< TextView
android:text ="性别"
android:id
="@+id/TextView03"
android:layout_width
="wrap_content"
android:layout_height
="wrap_content" ></ TextView >

< RadioGroup
android:id ="@+id/gender_g"
android:layout_width
="wrap_content"
android:layout_height
="wrap_content" >

< RadioButton
android:text ="男"
android:id
="@+id/male"
android:layout_width
="wrap_content"
android:layout_height
="wrap_content" ></ RadioButton >

< RadioButton
android:text ="女"
android:id
="@+id/female"
android:layout_width
="wrap_content"
android:layout_height
="wrap_content" ></ RadioButton >


</ RadioGroup >
</ TableRow >


< TableRow
android:id ="@+id/TableRow04"
android:layout_width
="wrap_content"
android:layout_height
="wrap_content" >
< TextView
android:text ="婚否"
android:id
="@+id/TextView04"
android:layout_width
="wrap_content"
android:layout_height
="wrap_content" ></ TextView >


< ToggleButton
android:text ="@+id/ToggleButton01"
android:id
="@+id/marriged"
android:layout_width
="wrap_content"
android:layout_height
="wrap_content" ></ ToggleButton >
</ TableRow >

< TableRow
android:id ="@+id/TableRow05"
android:layout_width
="wrap_content"
android:layout_height
="wrap_content"
>
< TextView
android:text ="爱好"
android:id
="@+id/hobby"
android:layout_width
="wrap_content"
android:layout_height
="wrap_content" ></ TextView >

< CheckBox
android:text ="阅读"
android:id
="@+id/reading"
android:layout_width
="wrap_content"
android:layout_height
="wrap_content"
android:layout_column
="1"
></ CheckBox >
< CheckBox
android:text ="游泳"
android:id
="@+id/swimming"
android:layout_width
="wrap_content"
android:layout_height
="wrap_content"
android:layout_column
="1"
></ CheckBox >


</ TableRow >


< TableRow
android:id ="@+id/TableRow06"
android:layout_width
="wrap_content"
android:layout_height
="wrap_content" >
< TextView
android:text ="职务"
android:id
="@+id/TextView05"
android:layout_width
="wrap_content"
android:layout_height
="wrap_content" ></ TextView >

< Spinner
android:id ="@+id/position"
android:layout_width
="wrap_content"
android:layout_height
="wrap_content" ></ Spinner >
</ TableRow >


< TableRow
android:id ="@+id/TableRow07"
android:layout_width
="wrap_content"
android:layout_height
="wrap_content" >
< Button
android:text ="取消"
android:id
="@+id/cancel"
android:layout_width
="wrap_content"
android:layout_height
="wrap_content" ></ Button >

< Button
android:text ="注册"
android:id
="@+id/register"
android:layout_width
="wrap_content"
android:layout_height
="wrap_content" ></ Button >

</ TableRow >

</ TableLayout >
</ LinearLayout >

 

/Chapter04_UI_CommonWidget/res/layout/result.xml

代码
   
     
<? xml version="1.0" encoding="utf-8" ?>
< LinearLayout xmlns:android ="http://schemas.android.com/apk/res/android"
android:orientation
="vertical"
android:layout_width
="fill_parent"
android:layout_height
="fill_parent"
>
< ListView
android:id ="@+id/ListView01"
android:layout_width
="wrap_content"
android:layout_height
="wrap_content" ></ ListView >
</ LinearLayout >

 

 

 

 

 

你可能感兴趣的:(android)