您的位置:首页 > 移动开发 > Android开发

android 点击按钮实现页面跳转并显示以选择信息

2011-12-14 20:49 866 查看
感觉今天所学的 radio listcheckbox spinner 基础内容都比较简单

目前只写了单选的信息显示。

checkBox 和 Spinner 还没实现

源码如下

(注意要写第二个Activity的清单

即添加Activity02的activity标签)

Activity01Activity

package yzy.cxt.com;

import android.app.Activity;

import android.content.Intent;

import android.os.Bundle;

import android.view.View;

import android.view.View.OnClickListener;

import android.widget.Button;

import android.widget.CheckBox;

import android.widget.CompoundButton;

import android.widget.EditText;

import android.widget.RadioButton;

import android.widget.Toast;

import android.widget.CompoundButton.OnCheckedChangeListener;

public class Activity01Activity extends Activity {

// 定义控件变量

private RadioButton radioButton01;

private RadioButton radioButton02;

private CheckBox cb1;

private CheckBox cb2;

private CheckBox cb3;

private CheckBox cb4;

private EditText editText;

private Button button;

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.one_layout);

// 绑定变量

radioButton01 = (RadioButton) findViewById(R.id.radiobutton01);

radioButton02 = (RadioButton) findViewById(R.id.radiobutton02);

cb1 = (CheckBox) findViewById(R.id.cb1);

cb2 = (CheckBox) findViewById(R.id.cb2);

cb3 = (CheckBox) findViewById(R.id.cb3);

cb4 = (CheckBox) findViewById(R.id.cb4);

editText = (EditText) findViewById(R.id.edit);

button = (Button) findViewById(R.id.button);

// 为radioButton01设置监听事件

radioButton01.setOnCheckedChangeListener(new OnCheckedChangeListener() {

@Override

public void onCheckedChanged(CompoundButton buttonView,

boolean isChecked) {

radioButton01.setChecked(isChecked);

if (radioButton02.isChecked()) {

radioButton02.setChecked(false);

}

}

});

// 为radioButton02设置监听事件

radioButton02.setOnCheckedChangeListener(new OnCheckedChangeListener() {

@Override

public void onCheckedChanged(CompoundButton buttonView,

boolean isChecked) {

radioButton02.setChecked(isChecked);

if (radioButton01.isChecked()) {

radioButton01.setChecked(false);

}

}

});

// 为Button设置监听事件

button.setOnClickListener(new OnClickListener() {

@Override

public void onClick(View arg0) {

String quantityType = "";

String descri = "";

String db = "";

// 获取信息

if (radioButton01.isChecked() && !radioButton02.isChecked()) {

quantityType = "twenty";

} else if (radioButton02.isChecked()

&& !radioButton01.isChecked()) {

quantityType = "thirty";

} else {

Toast.makeText(Activity01Activity.this, "请正确选择",

Toast.LENGTH_LONG);

}

descri = editText.getText().toString();

// 将信息放入Bundle

Bundle bundle = new Bundle();

bundle.putString("quantityType", quantityType);

bundle.putString("desc", descri);

Intent intent = new Intent();

intent.setClass(Activity01Activity.this, Activity02.class);

intent.putExtras(bundle);

// 开始。。。。跳转

startActivity(intent);

}

});

}

}

Activity02

package yzy.cxt.com;

import android.app.Activity;

import android.os.Bundle;

import android.widget.TextView;

public class Activity02 extends Activity{

private TextView message;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.two_layout);

Bundle bundle = this.getIntent().getExtras();

String quantity = "";

// 接收信息并显示

message = (TextView)findViewById(R.id.message);

String quantityType = bundle.getString("quantityType");

String desc = bundle.getString("desc");

// 判断

if ("twenty".equals(quantityType)) {

quantity = "20";

} else {

quantity = "30";

}

message.setText("你选择的每页帖数是" + quantity + ", 您对本站的看法为:" + desc);

}

}

one_layout

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

>

<TextView

Android:layout_width="fill_parent"

Android:layout_height="wrap_content"

Android:text="界面风格"

Android:textSize="20px" />

<Spinner

Android:id="@+id/styleSp"

Android:layout_width="fill_parent"

Android:layout_height="wrap_content"

Android:entries="@array/style"

Android:prompt="@string/style_prompt" />

<TextView

Android:layout_width="fill_parent"

Android:layout_height="wrap_content"

Android:text="提醒"

Android:textSize="20px" />

<CheckBox

Android:id="@+id/cb1"

Android:layout_width="fill_parent"

Android:layout_height="wrap_content"

Android:text="私人消息" />

<CheckBox

Android:id="@+id/cb2"

Android:layout_width="fill_parent"

Android:layout_height="wrap_content"

Android:text="公共消息" />

<CheckBox

Android:id="@+id/cb3"

Android:layout_width="fill_parent"

Android:layout_height="wrap_content"

Android:text="论坛任务" />

<CheckBox

Android:id="@+id/cb4"

Android:layout_width="fill_parent"

Android:layout_height="wrap_content"

Android:text="系统消息"/>

<TextView

Android:layout_width="wrap_content"

Android:layout_height="wrap_content"

Android:textSize="20sp"

Android:layout_marginLeft="30px"

Android:layout_marginRight="30px"

Android:text="每页帖数"

/>

<RadioButton

Android:id="@+id/radiobutton01"

Android:layout_width="wrap_content"

Android:layout_height="wrap_content"

Android:text="20"

/>

<RadioButton

Android:id="@+id/radiobutton02"

Android:layout_width="wrap_content"

Android:layout_height="wrap_content"

Android:text="30"

/>

<TextView

Android:layout_width="wrap_content"

Android:layout_height="wrap_content"

Android:textSize="20sp"

Android:text="您对本站的看法"

/>

<EditText

Android:id="@+id/edit"

Android:layout_width="wrap_content"

Android:layout_height="wrap_content"

Android:textSize="20px"

Android:width="200sp"

/>

<Button

Android:id="@+id/button"

Android:layout_width="wrap_content"

Android:layout_height="wrap_content"

Android:text="提交"

/>

</LinearLayout>

two_layout

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

>

<TextView

Android:id="@+id/message"

Android:layout_width="wrap_content"

Android:layout_height="wrap_content"

/>

</LinearLayout>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐