android初级三 -button响应与intent

一、Button 事件处理

1.创建新工程

2.修改main.xml 布局,添加一个TextView 和一个Button

  

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3 android:orientation="vertical"
 4 android:layout_width="fill_parent"
 5 android:layout_height="fill_parent"
 6 >
 7 <TextView
 8 android:id="@+id/show_TextView"
 9 android:layout_width="fill_parent"
10 android:layout_height="wrap_content"
11 android:text="@string/hello"
12 />
13 <Button
14 android:id="@+id/Click_Button"
15 android:layout_width="wrap_content"
16 android:layout_height="wrap_content"
17 android:text="点击"
18 />
19 </LinearLayout>

3. 在mainActivity.java 中findViewByID()获取TextView 和Button 资源

1 show= (TextView)findViewById(R.id.show_TextView);
2 press=(Button)findViewById(R.id.Click_Button);

4.给Button 添加事件监听器Button.OnClickListener()

1 press.setOnClickListener(new Button.OnClickListener(){
2 @Override
3 public void onClick(View v) {
4 // TODO Auto-generated method stub
5 show.setText("Hi , Google Android!");
6 }
7 });

二、手机页面的转换

需要定义二个xml 布局文,一个为mylayout,另一个为main

其JAVA代码如下:

 1 import android.app.Activity;/* import 相关class */
 2 import android.os.Bundle;
 3 import android.view.View;
 4 import android.widget.Button;
 5 public class Ex8_UI extends Activity {
 6 /** Called when the activity is first created. */
 7 @Override
 8 public void onCreate(Bundle savedInstanceState) {
 9 super.onCreate(savedInstanceState);
10 /* 载入main.xml Layout */
11 setContentView(R.layout.main);// 默认启动布局
12 /* 以findViewById()取得Button 对象,并添加onClickListener */
13 Button b1 = (Button) findViewById(R.id.button1);
14 b1.setOnClickListener(new Button.OnClickListener() {
15 public void onClick(View v) {
16 jumpToLayout2();// 调用跳转方法jumpToLayout2()
17 }
18 });
19 } /* method jumpToLayout2:将
20 layout 由
21 main.xml 切
22 23 24 mylayout.xml */
25 public void jumpToLayout2() {
26 /* 将layout 改成mylayout.xml */
27 setContentView(R.layout.mylayout);
28 /* 以findViewById()取得Button 对象,并添加onClickListener */
29 Button b2 = (Button) findViewById(R.id.button2);
30 b2.setOnClickListener(new Button.OnClickListener() {
31 public void onClick(View v) {
32 jumpToLayout1();// 调用跳转方法jumpToLayout1()
33 }
34 });
35 } /* method jumpToLayout1:将
36 layout 由
37 mylayout.xml 切
38 39 成main.xml */
40 public void jumpToLayout1() {
41 /* 将layout 改成main.xml */
42 setContentView(R.layout.main);
43 /* 以findViewById()取得Button 对象,并添加onClickListener */
44 Button b1 = (Button) findViewById(R.id.button1);
45 b1.setOnClickListener(new Button.OnClickListener() {
46 public void onClick(View v) {
47 jumpToLayout2();// 调用跳转方法jumpToLayout2()
48 }
49 });
50 }
51 }

三、调用加一个activity

Intent 对象的使用

① 新建工程
② 在string.xml 中添加两个字符串

1 <?xml version="1.0" encoding="utf-8"?>
2 <resources>
3 <string name="hello">Hello World, Ex9_UI!</string>
4 <string name="app_name">Ex9_UI</string>
5 <string name="act1">This is Activity 1!</string>
6 <string name="act2">This is Activity 2!</string>
7 </resources>

③ 新建color.xml 存放颜色值

1 <?xml version="1.0" encoding="utf-8"?>
2 <resources>
3 <color name="black">#000000</color>
4 <color name="white">#FFFFFFFF</color>
5 </resources>

④ 修改main.xml 布局,添加一个TextView 和一个Button

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <AbsoluteLayout
 3 android:layout_width="fill_parent"
 4 android:layout_height="fill_parent"
 5 android:background="@color/black"
 6 xmlns:android="http://schemas.android.com/apk/res/android"
 7 > <
 8 TextView
 9 android:id="@+id/text1"
10 android:textSize="24sp"
11 android:layout_width="186px"
12 android:layout_height="29px"
13 android:layout_x="70px"
14 android:layout_y="32px"
15 android:text="@string/act1"
16 ></TextView>
17 <Button
18 android:id="@+id/button1"
19 android:layout_width="118px"
20 android:layout_height="wrap_content"

android:layout_x="100px"
android:layout_y="82px"
android:text="Go to Activity2"
></Button>
</AbsoluteLayout>

 

⑤ 新建一个secondlayout.xml 布局,并添加一个TextView 和一个Button

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <AbsoluteLayout
 3 android:layout_width="fill_parent"
 4 android:layout_height="fill_parent"
 5 android:background="@color/white"
 6 xmlns:android="http://schemas.android.com/apk/res/android"
 7 > <
 8 TextView
 9 android:id="@+id/text2"
10 android:textSize="24sp"
11 android:layout_width="186px"
12 android:layout_height="29px"
13 android:layout_x="70px"
14 android:layout_y="32px"
15 android:textColor="@color/black"
16 android:text="@string/act2"
17 ></TextView>
18 <Button
19 android:id="@+id/button2"
20 android:layout_width="118px"
21 android:layout_height="wrap_content"
22 android:layout_x="100px"
23 android:layout_y="82px"
24 android:text="Go to Activity1"
25 ></Button>
26 </AbsoluteLayout>

⑥ 新建SecondActivity.java 文件,添加内容

 1 package zyf.Ex9_UI;
 2 import android.app.Activity;
 3 import android.content.Intent;
 4 import android.os.Bundle;
 5 import android.view.View;
 6 import android.widget.Button;
 7 public class SecondActivity extends Activity {
 8 /** Called when the activity is first created. */
 9 @Override
10 public void onCreate(Bundle savedInstanceState) {
11 super.onCreate(savedInstanceState);
12 /* 载入mylayout.xml Layout */
13 setContentView(R.layout.mylayout);
14 /* 以findViewById()取得Button 对象,并添加onClickListener */
15 Button b2 = (Button) findViewById(R.id.button2);
16 b2.setOnClickListener(new Button.OnClickListener() {
17 public void onClick(View v) {
18 /* new 一个Intent 对象,并指定要启动的class */
19 Intent intent = new Intent();
20 intent.setClass(SecondActivity.this, Ex9_UI.class);
21 /* 调用一个新的Activity */
22 startActivity(intent);
23 /* 关闭原本的Activity */
24 SecondActivity.this.finish();
25 }
26 });
27 }
28 }

⑦ 修改mainActivity.java,添加代码

 1 package zyf.Ex9_UI;
 2 import android.app.Activity;
 3 import android.content.Intent;
 4 import android.os.Bundle;
 5 import android.view.View;
 6 import android.widget.Button;
 7 public class Ex9_UI extends Activity {
 8 /** Called when the activity is first created. */
 9 @Override
10 public void onCreate(Bundle savedInstanceState) {
11 super.onCreate(savedInstanceState);
12 /* 载入main.xml Layout */
13 setContentView(R.layout.main);
14 /* 以findViewById()取得Button 对象,并添加onClickListener */
15 Button b1 = (Button) findViewById(R.id.button1);
16 b1.setOnClickListener(new Button.OnClickListener() {
17 public void onClick(View v) {
18 /* new 一个Intent 对象,并指定要启动的class */
19 Intent intent = new Intent();
20 intent.setClass(Ex9_UI.this, SecondActivity.class);
21 /* 调用一个新的Activity */
22 startActivity(intent);
23 /* 关闭原本的Activity */
24 Ex9_UI.this.finish();
25 }
26 });
27 }
28 }

⑧ 在AndroidManifest.xml 文件中添加SecondActivity

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
 3 package="zyf.Ex9_UI"
 4 android:versionCode="1"
 5 android:versionName="1.0">
 6 <application android:icon="@drawable/icon"
 7 android:label="@string/app_name" >
 8 <activity android:name=".Ex9_UI"
 9 android:label="@string/app_name" >
10 <intent-filter>
11 <action android:name="android.intent.action.MAIN" />
12 <category android:name="android.intent.category.LAUNCHER" />
13 </intent-filter>
14 </activity>
15 <activity android:name="SecondActivity"></activity>
16 </application>
17 <uses-sdk android:minSdkVersion="2" />
18 </manifest>

四、不同Activity之间数据传递。

在AndroidManifest.xml 添加Activity 定义

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
 3 package="zyf.Ex10_UI"
 4 android:versionCode="1"
 5 android:versionName="1.0">
 6 <application android:icon="@drawable/icon"
 7 android:label="@string/app_name" >
 8 <activity android:name=".Ex10_UI"
 9 android:label="@string/app_name" >
10 <intent-filter>
11 <action android:name="android.intent.action.MAIN" />
12 <category android:name="android.intent.category.LAUNCHER" />
13 </intent-filter>
14 </activity>
15 <activity android:name="BMIActivity"></activity>
16 </application>
17 <uses-sdk android:minSdkVersion="2" />
18 </manifest>

修改BMIActivity.java 内容

 1 package zyf.Ex10_UI;
 2 /* import 相关class */
 3 import java.text.DecimalFormat;
 4 import java.text.NumberFormat;
 5 import android.app.Activity;
 6 import android.os.Bundle;
 7 import android.widget.TextView;
 8 public class BMIActivity extends Activity {
 9 /** Called when the activity is first created. */
10 @Override
11 public void onCreate(Bundle savedInstanceState) {
12 super.onCreate(savedInstanceState);
13 /* 加载main.xml Layout */
14 setContentView(R.layout.mylayout);
15 /* 取得Intent 中的Bundle 对象*/
16 Bundle bunde = this.getIntent().getExtras();
17 /* 取得Bundle 对象中的数据*/
18 String sex = bunde.getString("sex");
19 double height = bunde.getDouble("height");
20 /* 判断性别*/
21 String sexText = "";
22 if (sex.equals("M")) {
23 sexText = "男性";
24 } else {
25 sexText = "女性";
26 } /*27 28 29 30 31 32 */
33 String weight = this.getWeight(sex, height);
34 /* 设置输出文字*/
35 TextView tv1 = (TextView) findViewById(R.id.text1);
36 tv1.setText("你是一位" + sexText + "\n你的身高是" + height +
37 "厘米\n你的标准体重是"+ weight + "公斤");
38 }
39 /* 四舍五入的method */
40 private String format(double num) {
41 NumberFormat formatter = new DecimalFormat("0.00");
42 String s = formatter.format(num);
43 return s;
44 }

/* 以findViewById()取得Button 对象,并添加onClickListener */
private String getWeight(String sex, double height) {
String weight = "";
if (sex.equals("M")) {
weight = format((height - 80) * 0.7);
} else {
weight = format((height - 70) * 0.6);
} return weight;
}
}

修改mainActivity.java 内容

 1 import 相关class */
 2 import android.app.Activity;
 3 import android.content.Intent;
 4 import android.os.Bundle;
 5 import android.view.View;
 6 import android.widget.Button;
 7 import android.widget.EditText;
 8 import android.widget.RadioButton;
 9 public class Ex10_UI extends Activity {
10 /** Called when the activity is first created. */
11 @Override
12 public void onCreate(Bundle savedInstanceState) {
13 super.onCreate(savedInstanceState);
14 /* 载入main.xml Layout */
15 setContentView(R.layout.main);
16 /* 以findViewById()取得Button 对象,并添加onClickListener */
17 Button ok = (Button) findViewById(R.id.button_OK);
18 ok.setOnClickListener(new Button.OnClickListener() {
19 public void onClick(View v) {
20 /* 取得输入的身高*/
21 EditText et = (EditText) findViewById(R.id.height_Edit);
22 double height = Double.parseDouble(et.getText().toString());
23 /* 取得选择的性别*/
24 String sex = "";
25 RadioButton rb1 = (RadioButton) findViewById(R.id.Sex_Man);
26 if (rb1.isChecked()) {
27 sex = "M";
28 } else {
29 sex = "F";
30 } /* new 一
31 32 Intent 对
33 34 35 36 37 38 class */
39 Intent intent = new Intent();
40 intent.setClass(Ex10_UI.this, BMIActivity.class);
41 /* new 一个Bundle对象,并将要传递的数据传入*/
42 Bundle bundle = new Bundle();
43 bundle.putDouble("height", height);
44 bundle.putString("sex", sex);
45 /* 将Bundle 对象assign 给Intent */
46 intent.putExtras(bundle);
47 /* 调用Activity EX03_10_1 */
48 startActivity(intent);
49 }
50 });
51 }
52 }

五、返回数据到前一个Activity

 

 1 @
 2 Override
 3 protected void onActivityResult(int requestCode, int resultCode,
 4 Intent data) {
 5 // TODO Auto-generated method stub
 6 super.onActivityResult(requestCode, resultCode, data);
 7 switch (resultCode) {
 8 case RESULT_OK:
 9 /* 取得来自Activity2 的数据,并显示于画面上*/
10 Bundle bunde = data.getExtras();
11 String sex = bunde.getString("sex");
12 double height = bunde.getDouble("height");
13 edit_height.setText("" + height);
14 if (sex.equals("M")) {
15 radiobutton_Man.setChecked(true);
16 } else {
17 radiobutton_Woman.setChecked(true);
18 } break break;
19 default:
20 break;
21 }
22 }
23 }
 1 Intent intent = new Intent();
 2 intent.setClass(Ex11_UI_A.this, BMIActivity.class);
 3 /* new 一个Bundle对象,并将要传递的数据传入*/
 4 Bundle bundle = new Bundle();
 5 bundle.putDouble("height", height);
 6 bundle.putString("sex", sex);
 7 /* 将Bundle 对象assign 给Intent */
 8 intent.putExtras(bundle);
 9 /* 调用Activity EX03_10_1 */
10 startActivityForResult(intent, my_requestCode);
 1 Button b1 = (Button) findViewById(R.id.button_back);
 2 b1.setOnClickListener(new Button.OnClickListener() {
 3 @Override
 4 public void onClick(View v) {
 5 // TODO Auto-generated method stub
 6 /* 返回result 回上一个activity */
 7 BMIActivity.this.setResult(RESULT_OK, intent);
 8 /* 结束这个activity */
 9 BMIActivity.this.finish();
10 }
11 });

 六、返回数据到前一个Activity

Toast 显示一个TextView

 1 package zyf.EX_Ctrl_3_B;
 2 import android.app.Activity;
 3 import android.os.Bundle;
 4 import android.widget.TextView;
 5 import android.widget.Toast;
 6 public class EX_Ctrl_3_B extends Activity {
 7 /** Called when the activity is first created. */
 8 @Override
 9 public void onCreate(Bundle savedInstanceState) {
10 super.onCreate(savedInstanceState);
11 /* 设置主屏布局*/
12 setContentView(R.layout.main);
13 /* 创建新Toast对象*/
14 Toast showImageToast = new Toast(this);
15 /*新建TextView对象*/
16 TextView text=new TextView(this);
17 /*设置TextView内容*/
18 text.setText("显示在Toast中的TextView");
19 /* 设置Toast上的View--(TextView) */
20 showImageToast.setView(text);
21 /* 设置Toast显示时间*/
22 showImageToast.setDuration(Toast.LENGTH_LONG);
23 /* 显示Toast */
24 showImageToast.show();
25 }
26 }
27 
28 
29 
30 package zyf.EX_Ctrl_3_B;
31 import android.app.Activity;
32 import android.os.Bundle;
33 import android.view.View;
34 import android.widget.Button;
35 import android.widget.Toast;
36 public class EX_Ctrl_3_B extends Activity {
37 /** Called when the activity is first created. */
38 @Override
39 public void onCreate(Bundle savedInstanceState) {
40 super.onCreate(savedInstanceState);
41 /* 设置主屏布局*/
42 setContentView(R.layout.main);
43 /* 创建新Toast对象*/
44 Toast showImageToast = new Toast(this);
45 // /*新建Button对象*/
46 Button button = new Button(this);
47 button.setText("OK");
48 /* 设置Toast上的View--(Button) */
49 showImageToast.setView(button);
50 /* 设置Toast显示时间*/
51 showImageToast.setDuration(Toast.LENGTH_LONG);
52 /* 显示Toast */
53 showImageToast.show();
54 }
55 }


七   AlertDialog.Builder提示对话框

 1 package zyf.EX_Ctrl_3_B;
 2 import android.app.Activity;
 3 import android.app.AlertDialog;
 4 import android.os.Bundle;
 5 public class EX_Ctrl_3_B extends Activity {
 6 /** Called when the activity is first created. */
 7 @Override
 8 public void onCreate(Bundle savedInstanceState) {
 9 super.onCreate(savedInstanceState);
10 /* 设置主屏布局*/
11 setContentView(R.layout.main);
12 /*新建一个AlertDialog.Builder对象*/
13 AlertDialog.Builder my_ADialog =new AlertDialog.Builder(this);
14 /*设置标题*/
15 my_ADialog.setTitle("Android 提示");
16 /*设置显示消息*/
17 my_ADialog.setMessage("AlertDialog.Builder提示对话框消息!!");
18 /*显示*/
19 my_ADialog.show();
20 }
21 }

 

posted @ 2013-04-22 14:49  程序world  阅读(170)  评论(0)    收藏  举报