Test1

1、新建工程Test1

2、新建布局文件,采用线性布局,控件垂直排列。控件包括:

(1)一个TextView:自定义id为text1;内边距上下左右均为15dp;外边距上下各30dp,左边距为90dp;背景色绿色;字体大小为15sp;显示文字内容为“我的第一个activity”。

(2)一个按钮:自定义id为button1;内边距上下左右均为15dp;左外边距为120px;字体大小为15sp;显示文字内容为“click me”。

3、新建activity,完成下列功能:

(1)读取TextView和Button对象;

(2)为Button对象添加事件监听器(OnClickListener),当按钮被单击时,TextView对象背景色变为红色,且文字变为“你好我好大家好!!”。


实现代码:

     *activity_main.xml代码:

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

<TextView
android:id="@+id/text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="15dp"
android:layout_marginTop="30dp"
android:layout_marginBottom="30dp"
android:layout_marginLeft="90dp"
android:textSize="15sp"
android:background="#0AA770"
android:text="我的第一个activity" />

<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="15dp"
android:layout_marginLeft="120dp"
android:textSize="15sp"
android:text="click me" />
</LinearLayout>

     *MainActivity.java代码:

package com.thxy.test1;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends Activity {
private TextView textView;
private Button button;
private int count = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = (TextView) findViewById(R.id.text1);
button = (Button) findViewById(R.id.button1);
ButtoneListener buttoneListener = new ButtoneListener();// 生成监听对象
button.setOnClickListener(buttoneListener);// 按钮绑定一个监听器
}

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

class ButtoneListener implements OnClickListener// 创建一个类实现监听事件的接口
{
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
if (count == 0) {
textView.setText("你好我好大家好 哈!!!");
textView.setBackgroundColor(Color.RED);
count = 1;
}else if(count == 1) {
textView.setText("我的第一个Activity!!");
textView.setBackgroundColor(Color.GREEN);
count = 0;
}
}
}
}


Test2

一、复习回顾

1、获取复选框选取状态及其值的方法有哪些?

          OnClickListener  OnCheckedChangeListener

2、获取单选框选取状态及其值的方法有哪些?

          使用ButtonGroup进行分组,以便实现单项选择。但是监听时既可以对ButtonGroup进行监听,也可以通过监听组中的某个按钮实现判断。

3、ImageView用于呈现图片的,是否可以进行监听,产生某些动作?

4、ImageView、ImageButton、Button的区别?

二、应用练习

1、新建工程Test2

2、新建布局文件,采用线性布局,控件垂直排列。控件包括:

(1)一组复选框,实现兴趣爱好的选择:分别采用OnClickListener和OnCheckedChangeListener来实现对所选择项目的判定和取值。

(2)一组单选按钮,用于实现性别选择:分别对ButtonGroup和单个按钮的选择进行判定,获取其选择方式及值。

(3)建立一个ImageView控件,用于显示图片:初始显示某张图片,使用监听器对图片进行监听,当单击该图片时,实现两张图片之间的切换。

(4)建立一个按钮控件:监听按钮,当按钮被按下时,跳到第二个activity。

注:本操作是为activity的参数传递做准备,暂时还不能把选择的数据传过去。


实现代码:

     *activity_main.xml代码:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
tools:context=".MainActivity" >
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="兴趣爱好"/>

<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<CheckBox
android:id="@+id/readId"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="看书"/>
<CheckBox
android:id="@+id/swimId"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="游泳"/>
<CheckBox
android:id="@+id/playballId"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="打球"/>
</LinearLayout>

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="性别"/>

<RadioGroup
android:id="@+id/radioGroupId"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<RadioButton
android:id="@+id/femaleButtonId"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="男"/>
<RadioButton
android:id="@+id/maleButtonId"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="女"/>
</RadioGroup>

<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:id="@+id/pictureId"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:src="@drawable/xiaohuihui"/>

<Button
android:id="@+id/buttonId"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="跳转到下一个activity"/>
</LinearLayout>
</LinearLayout>


     *MainActivity.java代码:


package com.thxy.test2;

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.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;

public class MainActivity extends Activity {

private CheckBox readBox;
private CheckBox swimBox;
private CheckBox playballBox;
private RadioGroup radioGroup;
private RadioButton femaleButton;
private RadioButton maleButton;
private ImageView imageView;
private Button button;
int count=0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

readBox = (CheckBox) findViewById(R.id.readId);
swimBox = (CheckBox) findViewById(R.id.swimId);
playballBox = (CheckBox) findViewById(R.id.playballId);
radioGroup = (RadioGroup) findViewById(R.id.radioGroupId);
femaleButton = (RadioButton) findViewById(R.id.femaleButtonId);
maleButton = (RadioButton) findViewById(R.id.maleButtonId);
imageView = (ImageView) findViewById(R.id.pictureId);
button = (Button) findViewById(R.id.buttonId);

ButtonListener listener3 = new ButtonListener();
button.setOnClickListener(listener3);

ImageViewListner listner2 = new ImageViewListner();
imageView.setOnClickListener(listner2);

RadioGroupListener l = new RadioGroupListener();
radioGroup.setOnCheckedChangeListener(l);

CheckBoxListener listener = new CheckBoxListener();
readBox.setOnCheckedChangeListener(listener);
swimBox.setOnCheckedChangeListener(listener);
playballBox.setOnCheckedChangeListener(listener);

/*
OnBoxClickListener listener = new OnBoxClickListener();
readBox.setOnClickListener(listener);
swimBox.setOnClickListener(listener);
playballBox.setOnClickListener(listener);
*/
}

class ButtonListener implements OnClickListener{

@Override
public void onClick(View v) {
Intent intent = new Intent();
intent.setClass(MainActivity.this, SecondActivity.class);
startActivity(intent);
}
}

//OnClickListener监听方法实现图片切换
class ImageViewListner implements OnClickListener {

@Override
public void onClick(View v) {
ImageView image = (ImageView) v;
count++;
//判断如果点击了控件

if(v.getId() == R.id.pictureId && count%2==0){

//setImageDrawable(Drawable drawable)使用Drawable对象设置该ImageView显示的图片
image.setImageDrawable(getResources().getDrawable(R.drawable.panda));
}else {
image.setImageDrawable(getResources().getDrawable(R.drawable.xiaohuihui));
}
}
}

//OnCheckedChangeListener对单选按钮进行监听
class RadioGroupListener implements android.widget.RadioGroup.OnCheckedChangeListener{

@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
if (checkedId == femaleButton.getId()) {
System.out.println("男");
}else if (checkedId == maleButton.getId()) {
System.out.println("女");
}
}
}

class CheckBoxListener implements OnCheckedChangeListener{

@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (buttonView.getId() == R.id.readId) {
System.out.println("read");
}else if (buttonView.getId() == R.id.swimId) {
System.out.println("swim");
}else if (buttonView.getId() == R.id.playballId) {
System.out.println("playball");
}
if (isChecked) {
System.out.println("checked");
}else {
System.out.println("Uncheck");
}
}

}

/*
* OnClickListener实现监听
*/
/*
class OnBoxClickListener implements OnClickListener{

@Override
public void onClick(View v) {
CheckBox box = (CheckBox) v;
if (box.getId() == R.id.readId) {
System.out.println("readId");
}else if (box.getId() == R.id.swimId) {
System.out.println("swimId");
}else if (box.getId() == R.id.playballId) {
System.out.println("playballId");
}
if (box.isChecked()) {
System.out.println("checked");
}else {
System.out.println("UnChecked");
}
}

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

*注意:AndroidManifest.xml省略,实现时候必须添加代码


Test3

一、复习回顾

1、列出Activity生命周期中7个常用方法。

2、Activity有哪三个状态?各状态之间是如何换的?

3、Activity如何进行堆栈的?

4、列出使用Intent对象在两个Activity之间传递数据的步骤。

5、你还知道哪些在Activity之间传递数据的方式?

二、应用练习

1、Activity的参数传递:

(1)Activity1跳至Activity2,将复选按钮、单选按钮的值以字符串的形式传递到Activity2,并显示在屏幕上。

(2)在Activity中添加一个EditText组件,输入数字5,单击按钮时将结果返回给Activity1。


实现代码:

     *第一个Activity的activity_main.xml代码:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<TextView
android:id="@+id/text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="第一个Activity"/>
<TextView
android:id="@+id/textViewId"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/text1"/>
<RadioGroup
android:id="@+id/radioGroupId"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/textViewId">
<RadioButton
android:id="@+id/radioButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="男"/>
<RadioButton
android:id="@+id/radioButton2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="女" />
</RadioGroup>
<Button
android:id="@+id/buttonId"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/radioGroupId"
android:text="跳转到第二个Activity"/>
</RelativeLayout>


      *第一个Activity的MainActivity.java代码:


package com.thxy.test4;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.TextView;

public class MainActivity extends Activity {

private TextView textView;
private RadioGroup radioGroup;
private RadioButton man;
private RadioButton women;
private Button button;
private String sex;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

textView = (TextView) findViewById(R.id.textViewId);
radioGroup = (RadioGroup) findViewById(R.id.radioGroupId);
man = (RadioButton) findViewById(R.id.radioButton1);
women = (RadioButton) findViewById(R.id.radioButton2);
button = (Button) findViewById(R.id.buttonId);

radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
if (checkedId == man.getId()) {
sex = (String) man.getText();
}else if (checkedId == women.getId()) {
sex = (String) women.getText();
}
}
});
button.setOnClickListener(new ButtonListener());
}

class ButtonListener implements OnClickListener{
@Override
public void onClick(View v) {
Intent intent = new Intent();
intent.setClass(MainActivity.this, SecondActivity.class);
intent.putExtra("com.thxy.test4.sex", sex);
MainActivity.this.startActivityForResult(intent, 0);
}
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 0) {
if (resultCode == RESULT_OK) {
/* 得到另一个Activity处理后返回的值 */
String value = data.getStringExtra("com.thxy.test4.Value");
//System.out.println(value);
textView.setText(value);
}
}
}

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

      *第二个Activity的activity2.xml代码:


<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="@+id/text2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/text2"/>
<EditText
android:id="@+id/editText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/textView"/>
</RelativeLayout>

      *第二个Activity的SecondActivity.java代码:


package com.thxy.test4;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class SecondActivity extends Activity {
private TextView textView;
private EditText editText;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity2);
textView = (TextView) findViewById(R.id.textView);
editText = (EditText) findViewById(R.id.editText);

Intent intent = getIntent();
String sex = intent.getStringExtra("com.thxy.test4.sex");

textView.setText(sex);
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK ) {
Intent intent = new Intent();
intent.setClass(SecondActivity.this, MainActivity.class);
String value = editText.getText().toString();
intent.putExtra("com.thxy.test4.Value", value);
SecondActivity.this.setResult(RESULT_OK, intent);
//当调用finsh()方法时,这个intent就会传递回调用这个Activity的源Activity的onActivityResult()方法里
SecondActivity.this.finish();
}
return false;
}
}

*注意:AndroidManifest.xml省略,实现时候必须添加代码


(3)试用剪贴板和全局变量在两个Activity之间传递数据。

实现代码(简单数据传递):

     *第一个Activity的java代码:

package com.thxy.test3_3;

import com.thxy.test3.R;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.ClipboardManager;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class ActivityOne extends Activity {

private Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activityone);

button = (Button) findViewById(R.id.button);
button.setOnClickListener(new OnClickListener() {

@SuppressLint("NewApi")
@Override
public void onClick(View v) {
ClipboardManager clipboardManager = (ClipboardManager)getSystemService(Context.CLIPBOARD_SERVICE);
String name = "NIKE";
clipboardManager.setText(name);
Intent intent = new Intent(ActivityOne.this,ActivityTwo.class);
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.main, menu);
return true;
}
}


      *第二个Activity的java代码:


package com.thxy.test3_3;

import com.thxy.test3.R;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.ClipboardManager;
import android.content.Context;
import android.os.Bundle;
import android.widget.EditText;

public class ActivityTwo extends Activity {

private EditText editText;
@SuppressLint("NewApi")
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activitytwo);

editText = (EditText) findViewById(R.id.editText);
ClipboardManager clipboardManager = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
String value = (String) clipboardManager.getText();
editText.setText(value);
}
}