组件(Widget)的复合使用

随时随地阅读更多技术实战干货,获取项目源码、学习资料,请关注源代码社区公众号(ydmsq666)

android中的组件可谓应有尽有,从简单的按钮(Button)到复杂的浏览器(WebView)都是无所不能,无所不有。

下面用一个用户注册会员为例来演示组件的使用,实现页面之间的切换:

TestWidgetActivity:

package com.lovo;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
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 TestWidgetActivity extends Activity {
	// 声明按钮Button
	private Button register, cancel;
	// 声明ToggleButton
	private ToggleButton marriged;
	// 声明单选按钮
	private RadioButton male, female;
	// 声明文本编辑框EditText
	private EditText username, password;
	// 声明下拉列表
	private Spinner position;
	// 声明多选按钮
	private CheckBox reading, swimming;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_test_widget_layout);
		// 获得EditText对象
		username = (EditText) findViewById(R.id.username);
		password = (EditText) findViewById(R.id.password);
		// 获得RadioButton对象
		male = (RadioButton) findViewById(R.id.male);
		female = (RadioButton) findViewById(R.id.female);
		// 获得CheckBox实例
		reading = (CheckBox) findViewById(R.id.reading);
		swimming = (CheckBox) findViewById(R.id.swimming);
		// 获得ToggleButton实例
		marriged = (ToggleButton) findViewById(R.id.marriged);
		// 获得Spinner实例
		position = (Spinner) findViewById(R.id.position);
		// 下拉列表项数组
		String[] str = { "CEO", "CFO", "PM" };
		// 数组下拉列表适配器
		ArrayAdapter adapter = new ArrayAdapter(this,
				android.R.layout.simple_spinner_item, str);
		// 设置数组下拉列表适配器
		position.setAdapter(adapter);
		// 获得Button实例
		register = (Button) findViewById(R.id.register);
		cancel = (Button) findViewById(R.id.cancel);
		register.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// 实例化Bundle对象,保存属性
				Bundle b = new Bundle();
				// 在Bundle中添加用户名和密码
				b.putString("username", "用户名:" + username.getText() + "");
				b.putString("password", "用户名:" + password.getText() + "");
				// 在Bundle中添加性别
				if (male.isChecked()) {
					b.putString("gender", "性别:男");
				} else {
					b.putString("gender", "性别:女");
				}
				String temp = "爱好:";
				if (reading.isChecked()) {
					temp += "阅读";
				}
				if (swimming.isChecked()) {
					temp += " ";
					temp += "游泳";
				}
				// 在Bundle中添加爱好
				b.putString("hobby", temp);
				// 在Bundle中添加婚否
				if (marriged.isChecked()) {
					b.putString("marriged", "婚否:已婚");
				} else {
					b.putString("marriged", "婚否:未婚");
				}
				// 在Bundle中添加职位
				b.putString("position", "职位:" + position.getSelectedItem() + "");
				// 实例化Intent,跳转到ResultActivity
				Intent intent = new Intent(TestWidgetActivity.this,
						ResultActivity.class);
				// 将Bundle 添加到Intent
				intent.putExtra("data", b);
				// 启动Activity
				startActivity(intent);
			}
		});
	}

}

ResultActivity:

package com.lovo;

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 {
	// 声明ListView
	private ListView listView;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.result);
		// 获得ListView实例
		listView = (ListView) findViewById(R.id.ListView01);
		// 获得Intent
		Intent intent = this.getIntent();
		// 从Intent中获得Bundle
		Bundle b = intent.getBundleExtra("data");
		// 实例化List集合
		List list = new ArrayList();
		// 从Bundle中获得属性,添加到List
		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 arrayAdapter = new ArrayAdapter(this,
				android.R.layout.simple_list_item_checked, list);
		// 为ListView设置适配器
		listView.setAdapter(arrayAdapter);
	}
}


activity_test_widget_layout.xml:

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

    <TableLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"
        android:collapseColumns="3"
        android:stretchColumns="1" >

        <TableRow
            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: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: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"
                android:orientation="horizontal" >

                <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:layout_marginLeft="20dp"
                    android:text="女" />
            </RadioGroup>
        </TableRow>

        <TableRow
            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:textOff="未婚"
                android:textOn="已婚" />
        </TableRow>

        <TableRow
            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:layout_marginRight="20dp"
                android:text="游泳" />
        </TableRow>

        <TableRow
            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: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>


 


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" />

</LinearLayout>


附上图片效果:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

u010142437

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值