Android组件RadioButton、checkBox、listview、spiner综合实例及Intent传值

作者: 淡蓝海域 分类: 安卓开发 发布时间: 2012-04-18 13:11

组件解释:

RadioButton单选按钮

checkBox复选框是一种有双状态按钮的特殊类型,可以选中或者不选中。

listview列表

列表的显示需要三个元素:1.ListVeiw 用来展示列表的View。2.适配器 用来把数据映射到ListView上的中。3.数据    具体的将被映射的字符串,图片,或者基本组件。 

根据列表的适配器类型,列表分为三种,ArrayAdapter,SimpleAdapter和SimpleCursorAdapter其中以ArrayAdapter最为简单,只能展示一行字。SimpleAdapter有最好的扩充性,可以自定义出各种效果。SimpleCursorAdapter可以认为是SimpleAdapter对数据库的简单结合,可以方面的把数据库的内容以列表的形式展示出来。

spiner下拉列表(Spinner)是一个每次只能选择所有项中一项的部件。它的项来自于与之相关联的适配器中。

Intent英文里 Intent是“意向、打算”的意思,其实就是告诉别人你的意图的意思了,这么理解Android里面的Intent也就不难了。

 

费话不多说,直接先看效果图:

main.xml布局文件:

  1. <?xml version=”1.0″ encoding=”utf-8″?>  
  2. <LinearLayout xmlns:android=”http://schemas.android.com/apk/res/android”  
  3.     android:layout_width=”fill_parent”  
  4.     android:layout_height=”fill_parent”  
  5.     android:orientation=”vertical” >  
  6.     <TextView  
  7.         android:layout_width=”fill_parent”  
  8.         android:layout_height=”wrap_content”  
  9.         android:layout_gravity=”center”  
  10.         android:gravity=”center”  
  11.         android:text=”@string/hello”  
  12.         android:textSize=”30dp”  
  13.         android:textStyle=”bold” />  
  14.     <TableLayout  
  15.         android:layout_width=”fill_parent”  
  16.         android:layout_height=”wrap_content”  
  17.         android:stretchColumns=”1″ >  
  18.         <TableRow >  
  19.             <TextView  
  20.                 android:layout_width=”wrap_content”  
  21.                 android:layout_height=”wrap_content”  
  22.                 android:layout_gravity=”center”  
  23.                 android:text=”用户名:” />  
  24.             <EditText  
  25.                 android:id=”@+id/username”  
  26.                 android:layout_width=”fill_parent”  
  27.                 android:layout_height=”wrap_content”  
  28.                 android:hint=”请输入用户名!!!” />  
  29.         </TableRow>  
  30.         <TableRow >  
  31.             <TextView  
  32.                 android:layout_width=”wrap_content”  
  33.                 android:layout_height=”wrap_content”  
  34.                 android:layout_gravity=”center”  
  35.                 android:text=”密码:” />  
  36.             <EditText  
  37.                 android:id=”@+id/password”  
  38.                 android:layout_width=”fill_parent”  
  39.                 android:layout_height=”wrap_content”  
  40.                 android:hint=”请输入密码!!!” />  
  41.         </TableRow>  
  42.         <TableRow >  
  43.             <TextView  
  44.                 android:layout_width=”wrap_content”  
  45.                 android:layout_height=”wrap_content”  
  46.                 android:layout_gravity=”center”  
  47.                 android:text=”性别:”  
  48.                 android:textSize=”20dp” />  
  49.             <RadioGroup  
  50.                 android:id=”@+id/sex”  
  51.                 android:layout_width=”fill_parent”  
  52.                 android:layout_height=”wrap_content”  
  53.                 android:checkedButton=”@+id/woman”  
  54.                 android:orientation=”horizontal” >  
  55.                 <RadioButton  
  56.                     android:id=”@+id/nan”  
  57.                     android:text=”男” />  
  58.                 <RadioButton  
  59.                     android:id=”@id/woman”  
  60.                     android:text=”女” />  
  61.             </RadioGroup>  
  62.         </TableRow>  
  63.         <TableRow >  
  64.             <TextView  
  65.                 android:layout_width=”fill_parent”  
  66.                 android:layout_height=”wrap_content”  
  67.                 android:layout_gravity=”center”  
  68.                 android:text=”爱好:”  
  69.                 android:textSize=”20dp” />  
  70.             <TableLayout  
  71.                 android:id=”@+id/table”  
  72.                 android:layout_width=”fill_parent”  
  73.                 android:layout_height=”wrap_content”  
  74.                 android:stretchColumns=”*” >  
  75.                 <TableRow >  
  76.                     <CheckBox  
  77.                         android:id=”@+id/cb1″  
  78.                         android:layout_width=”match_parent”  
  79.                         android:layout_height=”wrap_content”  
  80.                         android:text=”足球” />  
  81.                     <CheckBox  
  82.                         android:id=”@+id/cb2″  
  83.                         android:layout_width=”match_parent”  
  84.                         android:layout_height=”wrap_content”  
  85.                         android:text=”篮球” />  
  86.                 </TableRow>  
  87.                 <TableRow >  
  88.                     <CheckBox  
  89.                         android:id=”@+id/cb3″  
  90.                         android:layout_width=”match_parent”  
  91.                         android:layout_height=”wrap_content”  
  92.                         android:text=”游戏” />  
  93.                     <CheckBox  
  94.                         android:id=”@+id/cb4″  
  95.                         android:layout_width=”match_parent”  
  96.                         android:layout_height=”wrap_content”  
  97.                         android:text=”游泳” />  
  98.                 </TableRow>  
  99.             </TableLayout>  
  100.         </TableRow>  
  101.         <TableRow >  
  102.             <TextView  
  103.                 android:layout_width=”fill_parent”  
  104.                 android:layout_height=”wrap_content”  
  105.                 android:text=”学历:” />  
  106.             <Spinner  
  107.                 android:id=”@+id/sports”  
  108.                 android:layout_width=”match_parent”  
  109.                 android:layout_height=”wrap_content”  
  110.                 android:entries=”@array/educate”  
  111.                 android:prompt=”@string/educate” />  
  112.         </TableRow>      
  113.     </TableLayout>  
  114.     <Button  
  115.         android:layout_width=”100dp”  
  116.         android:layout_height=”wrap_content”  
  117.         android:layout_gravity=”center”  
  118.         android:id=”@+id/button”  
  119.         android:text=”注册” />  
  120. </LinearLayout>  

RegisterActivity.java实现代码:

  1. package cn.csdn.activity;  
  2.   
  3. import java.util.ArrayList;  
  4.   
  5. import android.app.Activity;  
  6. import android.content.Intent;  
  7. import android.os.Bundle;  
  8. import android.view.View;  
  9. import android.view.View.OnClickListener;  
  10. import android.widget.AdapterView;  
  11. import android.widget.Button;  
  12. import android.widget.CheckBox;  
  13. import android.widget.EditText;  
  14. import android.widget.RadioButton;  
  15. import android.widget.RadioGroup;  
  16. import android.widget.Spinner;  
  17. import android.widget.TextView;  
  18.   
  19. public class RegisterActivity extends Activity implements OnClickListener{  
  20.     String[] values=new String[5];//存放所有的值   
  21.     Button button=null;  
  22.     EditText username=null;//用户名       
  23.     EditText password=null;//密码   
  24.     RadioGroup rg=null;  
  25.     private CheckBox cb1,cb2,cb3,cb4;  
  26.     Spinner educate=null;  
  27.     private ArrayList<CheckBox> list=new ArrayList<CheckBox>();  
  28.     public void onCreate(Bundle savedInstanceState) {  
  29.         super.onCreate(savedInstanceState);  
  30.         setContentView(R.layout.main);  
  31.         findViews();  
  32.     }  
  33.     private void findViews() {  
  34.         button=(Button) findViewById(R.id.button);  
  35.         button.setOnClickListener(this);  
  36.         username=(EditText) findViewById(R.id.username);//用户名          
  37.         password=(EditText) findViewById(R.id.password);//密码   
  38.         rg=(RadioGroup) findViewById(R.id.sex);//性别   
  39.           
  40.         cb1=(CheckBox) findViewById(R.id.cb1);//爱好 复选框   
  41.         cb2=(CheckBox) findViewById(R.id.cb2);  
  42.         cb3=(CheckBox) findViewById(R.id.cb3);  
  43.         cb4=(CheckBox) findViewById(R.id.cb4);  
  44.         list.add(cb1);  
  45.         list.add(cb2);  
  46.         list.add(cb3);  
  47.         list.add(cb4);    
  48.         educate=(Spinner) findViewById(R.id.sports);//学历 下拉列表   
  49.     }  
  50.     @Override  
  51.     public void onClick(View v) {  
  52.         values[0]=username.getText().toString();  
  53.         values[1]=password.getText().toString();  
  54.         values[2]=((RadioButton)this.findViewById(rg.getCheckedRadioButtonId())).getText().toString();  
  55.           
  56.         String fav=””;  
  57.         for(CheckBox cb:list){  
  58.             if(cb.isChecked()){  
  59.                 fav+=cb.getText()+” “;  
  60.             }  
  61.         }  
  62.         values[3]=fav;  
  63.         values[4]=educate.getSelectedItem().toString();  
  64.         //往第二个activity传值   
  65.         Intent intent=new Intent();  
  66.         intent.setClass(RegisterActivity.this, OtherIntentActivity.class);  
  67.         //intent.setClass(第一个Activity.this, 要跳转的Activity.class);   
  68.         intent.putExtra(“values”, values);  
  69.         //intent.putExtra(“参数”, “参数值”);   
  70.         startActivity(intent);  
  71.     }  
  72. }  

第二个页面:OtherIntentActivity

  1. package cn.csdn.activity;  
  2.   
  3. import android.app.Activity;  
  4. import android.content.Intent;  
  5. import android.os.Bundle;  
  6. import android.util.Log;  
  7. import android.widget.ArrayAdapter;  
  8. import android.widget.LinearLayout;  
  9. import android.widget.ListView;  
  10. import android.widget.TextView;  
  11.   
  12. public class OtherIntentActivity extends Activity {  
  13.     ListView list=null;  
  14.     String value[]=new String[5];  
  15.     @Override  
  16.     protected void onCreate(Bundle savedInstanceState) {  
  17.         super.onCreate(savedInstanceState);  
  18.         this.setContentView(R.layout.other);  
  19.         //接值   
  20.         Intent intent = getIntent();  
  21.         value = intent.getStringArrayExtra(“values”);  
  22.           
  23.         value[0]=”用户名:”+value[0];  
  24.         value[1]=”密码:”+value[1];  
  25.         value[2]=”性别:”+value[2];  
  26.         value[3]=”爱好:”+value[3];  
  27.         value[4]=”学历:”+value[4];  
  28.         list=(ListView) findViewById(R.id.list);  
  29.         ArrayAdapter<String> adapter=new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,value);       
  30.         list.setAdapter(adapter);  
  31.     }     
  32. }  
下面说一下intent传值:

//传值

Intent _intent = new Intent(); _intent.setClass(第一个Activity.this, 要跳转的Activity.class); _intent.putExtra(“参数”, “参数值”);

this.startActivity(_intent); 

//第二个页面接收

Intent _intent = getIntent(); String _value = _intent.getStringExtra(“参数”);

下载原码地址:http://download.csdn.net/detail/rhljiayou/3921402

如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!

发表评论