下拉列表(Spinner),多选框(CheckBox),单选按钮(RadioButton)的使用

一个简单的测试功能,一个界面提交数据,一个界面显示

先看运行效果:


布局文件activity_main.xml代码:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TableLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <TableRow
            android:id="@+id/tableRow1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" >
            <TextView
           		 android:id="@+id/textView1"
           		 android:layout_width="wrap_content"
           		 android:layout_height="wrap_content"
           		 android:text="性别" />
                  <RadioGroup
                android:orientation="horizontal"
                android:id="@+id/radioGroup1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" >

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

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

                
            </RadioGroup>
        </TableRow>

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

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

            <CheckBox
                android:id="@+id/football"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="足球" />

            <CheckBox
                android:id="@+id/basketball"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="篮球" />

        </TableRow>

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

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

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

        </TableRow>
    </TableLayout>

    <Button
        	
        android:id="@+id/submit"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="提交" />

</LinearLayout>

MainActivity代码:

import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.SimpleAdapter;
import android.widget.Spinner;
import android.os.Build;

public class MainActivity extends ActionBarActivity {
	private Spinner position;//下拉列表
	private CheckBox football,basketball;//多选框
	private RadioButton man,woman;//单选按钮
	private Button submit;
	

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		//控件初始化
		position = (Spinner) findViewById(R.id.position);
		football = (CheckBox) findViewById(R.id.football);
		basketball = (CheckBox) findViewById(R.id.basketball);
		man = (RadioButton) findViewById(R.id.man);
		woman = (RadioButton) findViewById(R.id.woman);
		submit = (Button)findViewById(R.id.submit);
		//下拉框列表数组
		String[] str = {"程序员","项目经理","架构师"};
		//数组下拉列表适配器
		ArrayAdapter adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, str);
		//ArrayAdapter Simpleadapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, str);
		//设置下拉列表适配器
		position.setAdapter(adapter);
		
		//为提交按钮注册监听
		submit.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				//Bundle类用于在两个activity间传递数据
				Bundle b = new Bundle();
				String aihao = "爱好:";
				if(football.isChecked()){
					aihao += football.getText().toString();
				}
				if(basketball.isChecked()){
					aihao += " ";
					aihao += basketball.getText().toString();
				}
				b.putString("hobby", aihao);
				if(man.isChecked()){
					b.putString("gender", "性别:"+man.getText().toString());
				}if(woman.isChecked()){
					b.putString("gender", "性别:"+woman.getText().toString());
				}
				b.putString("position", "职位:"+position.getSelectedItem().toString());
				
				Intent intent = new Intent();
				//将数据传递到ResultActivity中
				intent.setClass(MainActivity.this, ResultActivity.class);
				intent.putExtras(b);
				startActivity(intent);
			}
		});
		
		
	}
	
		
	}



阅读更多
文章标签: android
个人分类: Android
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭
关闭