android应用开发详解(八)-------------常用组件

TextView、EditText、密码EditText、RadioButton、Button、Spinner、CheckBox、ToggleButton

1、工程目录


2、Test_changyongzujian.java

package com.example.test_changyongzujian;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
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 Test_changyongzujian_Activity extends Activity {
	private Button cancelButton, registerButton;
	private EditText usernameEdit, passwordEdit;
	private RadioButton maleRadio, femaleRadio;
	private ToggleButton marrigeToggle;
	private CheckBox readingCheck, swimmingCheck;
	private Spinner positionSpinner;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.test_changyongzujian);
		usernameEdit = (EditText) findViewById(R.id.username);
		passwordEdit = (EditText) findViewById(R.id.password);
		maleRadio = (RadioButton) findViewById(R.id.male);
		femaleRadio = (RadioButton) findViewById(R.id.female);
		marrigeToggle = (ToggleButton) findViewById(R.id.marriged);
		readingCheck = (CheckBox) findViewById(R.id.reading);
		swimmingCheck = (CheckBox) findViewById(R.id.swimming);
		// Spinner
		positionSpinner = (Spinner) findViewById(R.id.position);
		String[] str = { "CEO", "CFO", "PM" };
		ArrayAdapter aa = new ArrayAdapter(this,
				android.R.layout.simple_spinner_item, str);
		positionSpinner.setAdapter(aa);
		// Button
		cancelButton = (Button) findViewById(R.id.cancel);
		registerButton = (Button) findViewById(R.id.register);
		registerButton.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				Bundle b = new Bundle();
				b.putString("username", "用户名称:"
						+ usernameEdit.getText().toString());
				b.putString("password", "用户密码:"
						+ passwordEdit.getText().toString());
				if (maleRadio.isChecked())
					b.putString("gender", "性别:男");
				else if (femaleRadio.isChecked())
					b.putString("gender", "性别:女");
				String temp = "爱好:";
				if (readingCheck.isChecked()) {
					temp += "阅读";
				} else if (swimmingCheck.isChecked()) {
					temp += " 游泳";
				}
				b.putString("hobby", temp);
				if (marrigeToggle.isChecked())
					b.putString("marrige", "婚否:已婚");
				else
					b.putString("marrige", "婚否:未婚");
				b.putString("position", positionSpinner.getSelectedItem()
						.toString());
				Intent intent = new Intent(Test_changyongzujian_Activity.this,
						Result.class);
				intent.putExtra("data", b);
				startActivity(intent);

			}
		});
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.test_changyongzujian_, menu);
		return true;
	}

}

3、Result.java

package com.example.test_changyongzujian;

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 Result extends Activity {
	private ListView listview;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.result);
		listview = (ListView) findViewById(R.id.listview01);
		// 获取上个页面传递过来的值
		Intent intent = this.getIntent();
		Bundle b = intent.getBundleExtra("data");
		List list = new ArrayList();
		list.add(b.getString("username"));
		list.add(b.getString("password"));
		list.add(b.getString("gender"));
		list.add(b.getString("hobby"));
		list.add(b.getString("marrige"));
		list.add(b.getString("position"));
		// listview设置适配器
		ArrayAdapter adapter = new ArrayAdapter(this,
				android.R.layout.simple_list_item_checked, list);
		listview.setAdapter(adapter);
	}

}

4、test_changyongzujian.xml

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

    <TableLayout
        android:id="@+id/tableLayout01"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

            <CheckBox
                android:id="@+id/reading"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="阅读" />

            <CheckBox
                android:id="@+id/swimming"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="游泳" />
        </TableRow>

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

            <TextView
                android:id="@+id/textView05"
                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>

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

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

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

</LinearLayout>

5、result.xml

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

    <ListView
        android:id="@+id/listview01"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >
    </ListView>

</LinearLayout>

6、AndroidMenifest.xml

需要添加Result.java的Activity

7、结果显示


郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。