Android 基础UI编程2

Android 基础UI编程
标题、状态栏的隐藏
标题栏隐藏
在Activity.setCurrentView();之前调用此方法
private void HideTitle() {
// TODO Auto-generated method stub
requestWindowFeature(Window.FEATURE_NO_TITLE);
}

状态栏隐藏(全屏)
在Activity.setCurrentView();之前调用此方法
private void HideStatusBar() {
// TODO Auto-generated method stub
//隐藏标题
requestWindowFeature(Window.FEATURE_NO_TITLE);
//定义全屏参数
int flag=WindowManager.LayoutParams.FLAG_FULLSCREEN;
//获得窗口对象
Window myWindow=this.getWindow();
//设置Flag标识
myWindow.setFlags(flag,flag);
}

样式化的定型对象
Style 样式的定义
① 新建工程
② 定义一个style.xml 存放样式
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="myStyle_Text1">
<item name="android:textSize">25sp</item>
<item name="android:textColor">#80FF00</item>
</style>
<style name="myStyle_Text2">
<item name="android:textSize">18sp</item>
<item name="android:textColor">#0C688E</item>
<item name="android:fromAlpha">0.0</item>
<item name="android:toAlpha" >0.0</item>
</style>
</resources>

③ 在string.xml 中添加字符串
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="string_A">应用myStyle_Text1</string>
<string name="string_B">应用myStyle_Text2</string>
</resources>

④ 修改布局main.xml,添加两个TextView
<TextView
android:id="@+id/TextView01"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:gravity="center_vertical|center_horizontal"
android:text="@string/string_A"></TextView>
<TextView
android:id="@+id/TextView02"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:gravity="center_vertical|center_horizontal"
android:text="@string/string_B"></TextView>

⑤ 加入Style

<?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/TextView01"
style="@style/myStyle_Text1"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:gravity="center_vertical|center_horizontal"
android:text="@string/string_A"></TextView>
<TextView
android:id="@+id/TextView02"
style="@style/myStyle_Text2"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:gravity="center_vertical|center_horizontal"
android:text="@string/string_B"></TextView>
</LinearLayout>

简易的按钮事件
Button 事件处理
① 创建新工程
② 修改main.xml 布局,添加一个TextView 和一个Button
<?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/show_TextView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<Button
android:id="@+id/Click_Button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="点击"
/>
</LinearLayout>

③ 在mainActivity.java 中findViewByID()获取TextView 和Button 资源
show= (TextView)findViewById(R.id.show_TextView);
press=(Button)findViewById(R.id.Click_Button);

④ 给Button 添加事件监听器Button.OnClickListener()
press.setOnClickListener(new Button.OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
});

⑤ 处理事件
press.setOnClickListener(new Button.OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
show.setText("Hi , Google Android!");
}
});

手机页面的转换
setContentView 的应用
① 新建工程
② string 添加两个提示字符串
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="layout1">this is Layout 1</string>
<string name="layout2">This is Layout 2</string>
<string name="app_name">Ex8_UI</string>
</resources>

③ 新建color.xml 保存两个颜色值
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="black">#000000</color>
<color name="white">#FFFFFFFF</color>
</resources>

④ 修改main.xml 布局,添加一个TextView 和一个Button
<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@color/black"
xmlns:android="http://schemas.android.com/apk/res/android"
> <
TextView
android:id="@+id/text1"
android:textSize="24sp"
android:layout_width="186px"
android:layout_height="29px"
android:layout_x="70px"
android:layout_y="32px"
android:text="@string/layout1"
></TextView>
<Button
android:id="@+id/button1"
android:layout_width="118px"
android:layout_height="wrap_content"
android:layout_x="100px"
android:layout_y="82px"
android:text="Go to Layout2"
></Button>
</AbsoluteLayout>

⑤ 新建mylayout.xml 布局文件,并添加两个View:TextView 和Button
<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@color/white"
xmlns:android="http://schemas.android.com/apk/res/android"
> <
TextView
android:id="@+id/text2"
android:textSize="24sp"
android:layout_width="186px"
android:layout_height="29px"
android:layout_x="70px"
android:layout_y="32px"
android:textColor="@color/black"
android:text="@string/layout2"
> </TextView>
<Button
android:id="@+id/button2"
android:layout_width="118px"
android:layout_height="wrap_content"
android:layout_x="100px"
android:layout_y="82px"
android:text="Go to Layout1"
></Button>
</AbsoluteLayout>

⑥ 编写mainActivity.java
package zyf.Ex8_UI;
import android.app.Activity;/* import 相关class */
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class Ex8_UI extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
/* 载入main.xml Layout */
setContentView(R.layout.main);// 默认启动布局
/* 以findViewById()取得Button 对象,并添加onClickListener */
Button b1 = (Button) findViewById(R.id.button1);
b1.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
jumpToLayout2();// 调用跳转方法jumpToLayout2()
}
});
} /* method jumpToLayout2:将
layout 由main.xml 切换成mylayout.xml */
public void jumpToLayout2() {
/* 将layout 改成mylayout.xml */
setContentView(R.layout.mylayout);
/* 以findViewById()取得Button 对象,并添加onClickListener */
Button b2 = (Button) findViewById(R.id.button2);
b2.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
jumpToLayout1();// 调用跳转方法jumpToLayout1()
}
});
} /* method jumpToLayout1:将
layout 由mylayout.xml 切换成main.xml */
public void jumpToLayout1() {
/* 将layout 改成main.xml */
setContentView(R.layout.main);
/* 以findViewById()取得Button 对象,并添加onClickListener */
Button b1 = (Button) findViewById(R.id.button1);
b1.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
jumpToLayout2();// 调用跳转方法jumpToLayout2()
}
});
}
}

调用另一个Activity
Intent 对象的使用
① 新建工程
② 在string.xml 中添加两个字符串
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">Hello World, Ex9_UI!</string>
<string name="app_name">Ex9_UI</string>
<string name="act1">This is Activity 1!</string>
<string name="act2">This is Activity 2!</string>
</resources>

③ 新建color.xml 存放颜色值
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="black">#000000</color>
<color name="white">#FFFFFFFF</color>
</resources>

④ 修改main.xml 布局,添加一个TextView 和一个Button
<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@color/black"
xmlns:android="http://schemas.android.com/apk/res/android"
> <
TextView
android:id="@+id/text1"
android:textSize="24sp"
android:layout_width="186px"
android:layout_height="29px"
android:layout_x="70px"
android:layout_y="32px"
android:text="@string/act1"
></TextView>
<Button
android:id="@+id/button1"
android:layout_width="118px"
android:layout_height="wrap_content"
android:layout_x="100px"
android:layout_y="82px"
android:text="Go to Activity2"
></Button>
</AbsoluteLayout>

⑤ 新建一个secondlayout.xml 布局,并添加一个TextView 和一个Button
<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@color/white"
xmlns:android="http://schemas.android.com/apk/res/android"
> <
TextView
android:id="@+id/text2"
android:textSize="24sp"
android:layout_width="186px"
android:layout_height="29px"
android:layout_x="70px"
android:layout_y="32px"
android:textColor="@color/black"
android:text="@string/act2"
></TextView>
<Button
android:id="@+id/button2"
android:layout_width="118px"
android:layout_height="wrap_content"
android:layout_x="100px"
android:layout_y="82px"
android:text="Go to Activity1"
></Button>
</AbsoluteLayout>

⑥ 新建SecondActivity.java 文件,添加内容
package zyf.Ex9_UI;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class SecondActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
/* 载入mylayout.xml Layout */
setContentView(R.layout.mylayout);
/* 以findViewById()取得Button 对象,并添加onClickListener */
Button b2 = (Button) findViewById(R.id.button2);
b2.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
/* new 一个Intent 对象,并指定要启动的class */
Intent intent = new Intent();
intent.setClass(SecondActivity.this, Ex9_UI.class);
/* 调用一个新的Activity */
startActivity(intent);
/* 关闭原本的Activity */
SecondActivity.this.finish();
}
});
}
}

⑦ 修改mainActivity.java,添加代码
package zyf.Ex9_UI;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class Ex9_UI extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
/* 载入main.xml Layout */
setContentView(R.layout.main);
/* 以findViewById()取得Button 对象,并添加onClickListener */
Button b1 = (Button) findViewById(R.id.button1);
b1.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
/* new 一个Intent 对象,并指定要启动的class */
Intent intent = new Intent();
intent.setClass(Ex9_UI.this, SecondActivity.class);
/* 调用一个新的Activity */
startActivity(intent);
/* 关闭原本的Activity */
Ex9_UI.this.finish();
}
});
}
}

⑧ 在AndroidManifest.xml 文件中添加SecondActivity
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="zyf.Ex9_UI"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/icon"
android:label="@string/app_name" >
<activity android:name=".Ex9_UI"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name="SecondActivity"></activity>
</application>
<uses-sdk android:minSdkVersion="2" />
</manifest>

不同Activity之间的数据传递
Bundle 对象的实现
① 新建工程
② 修改main.xml 布局,添加UI 元素
<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout
android:id="@+id/widget0"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android" >
<TextView
android:id="@+id/showText"
android:layout_width="wrap_content"
android:layout_height="26px"
android:text="计算你的标准体重!"
android:textSize="25px"
android:layout_x="65px"
android:layout_y="21px">
</TextView>
<TextView
android:id="@+id/text_Sex"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="性别:"
android:layout_x="71px"
android:layout_y="103px">
</TextView>
<TextView
android:id="@+id/text_Height"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="身高:"
android:layout_x="72px"
android:layout_y="169px">
</TextView>
<RadioGroup
android:id="@+id/radioGroup"
android:layout_width="wrap_content"
android:layout_height="37px"
android:orientation="horizontal"
android:layout_x="124px"
android:layout_y="101px">
<RadioButton
android:id="@+id/Sex_Man"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="男">
</RadioButton>
<RadioButton
android:id="@+id/Sex_Woman"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="女">
</RadioButton>
</RadioGroup>
<EditText
android:id="@+id/height_Edit"
android:layout_width="123px"
android:layout_height="wrap_content"
android:text=""
android:textSize="18sp"
android:layout_x="124px"
android:layout_y="160px">
</EditText>
<Button
android:id="@+id/button_OK"
android:layout_width="80px"
android:layout_height="wrap_content"
android:text="计算"
android:layout_x="125px"
android:layout_y="263px">
</Button>
</AbsoluteLayout>

③ 新建mylayout.xml,并添加UI 元素
<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
> <
TextView
android:id="@+id/text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:layout_x="50px"
android:layout_y="72px"
></TextView>
</AbsoluteLayout>

④ 新建一个BMIActivity.java
package zyf.Ex10_UI;
import android.app.Activity;
import android.os.Bundle;
public class BMIActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
}

⑤ 在AndroidManifest.xml 添加Activity 定义
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="zyf.Ex10_UI"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/icon"
android:label="@string/app_name" >
<activity android:name=".Ex10_UI"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name="BMIActivity"></activity>
</application>
<uses-sdk android:minSdkVersion="2" />
</manifest>

⑥ 修改BMIActivity.java 内容
package zyf.Ex10_UI;
/* import 相关class */
import java.text.DecimalFormat;
import java.text.NumberFormat;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
public class BMIActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
/* 加载main.xml Layout */
setContentView(R.layout.mylayout);
/* 取得Intent 中的Bundle 对象*/
Bundle bunde = this.getIntent().getExtras();
/* 取得Bundle 对象中的数据*/
String sex = bunde.getString("sex");
double height = bunde.getDouble("height");
/* 判断性别*/
String sexText = "";
if (sex.equals("M")) {
sexText = "男性";
} else {
sexText = "女性";
} /* 取得标准体重*/
String weight = this.getWeight(sex, height);
/* 设置输出文字*/
TextView tv1 = (TextView) findViewById(R.id.text1);
tv1.setText("你是一位" + sexText + "\n你的身高是" + height +
"厘米\n你的标准体重是"+ weight + "公斤");
}
/* 四舍五入的method */
private String format(double num) {
NumberFormat formatter = new DecimalFormat("0.00");
String s = formatter.format(num);
return s;
}
/* 以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 内容
package zyf.Ex10_UI;
/* import 相关class */
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
public class Ex10_UI extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
/* 载入main.xml Layout */
setContentView(R.layout.main);
/* 以findViewById()取得Button 对象,并添加onClickListener */
Button ok = (Button) findViewById(R.id.button_OK);
ok.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
/* 取得输入的身高*/
EditText et = (EditText) findViewById(R.id.height_Edit);
double height = Double.parseDouble(et.getText().toString());
/* 取得选择的性别*/
String sex = "";
RadioButton rb1 = (RadioButton) findViewById(R.id.Sex_Man);
if (rb1.isChecked()) {
sex = "M";
} else {
sex = "F";
} /* new 一个Intent 对象,并指定class */
Intent intent = new Intent();
intent.setClass(Ex10_UI.this, BMIActivity.class);
/* new 一个Bundle对象,并将要传递的数据传入*/
Bundle bundle = new Bundle();
bundle.putDouble("height", height);
bundle.putString("sex", sex);
/* 将Bundle 对象assign 给Intent */
intent.putExtras(bundle);
/* 调用Activity EX03_10_1 */
startActivity(intent);
}
});
}
}

返回数据到前一个Activity
startActivityForResult 方法
① 新建工程
② 修改main.xml 布局,添加UI 元素
<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout
android:id="@+id/widget0"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android" >
<TextView
android:id="@+id/showText"
android:layout_width="wrap_content"
android:layout_height="26px"
android:text="计算你的标准体重!"
android:textSize="25px"
android:layout_x="65px"
android:layout_y="21px">
</TextView>
<TextView
android:id="@+id/text_Sex"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="性别:"
android:layout_x="71px"
android:layout_y="103px">
</TextView>
<TextView
android:id="@+id/text_Height"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="身高:"
android:layout_x="72px"
android:layout_y="169px">
</TextView>
<RadioGroup
android:id="@+id/radioGroup"
android:layout_width="wrap_content"
android:layout_height="37px"
android:orientation="horizontal"
android:layout_x="124px"
android:layout_y="101px">
<RadioButton
android:id="@+id/Sex_Man"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="男">
</RadioButton>
<RadioButton
android:id="@+id/Sex_Woman"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="女">
</RadioButton>
</RadioGroup>
<EditText
android:id="@+id/height_Edit"
android:layout_width="123px"
android:layout_height="wrap_content"
android:text=""
android:numeric="decimal"
android:textSize="18sp"
android:layout_x="124px"
android:layout_y="160px">
</EditText>
<Button
android:id="@+id/button_OK"
android:layout_width="80px"
android:layout_height="wrap_content"
android:text="计算"
android:layout_x="125px"
android:layout_y="263px">
</Button>
</AbsoluteLayout>

③ 新建一个mylayout.xml 布局,添加UI 元素
<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
> <
TextView
android:id="@+id/text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:layout_x="50px"
android:layout_y="72px"
></TextView>
<Button
android:id="@+id/button_back"
android:layout_width="100px"
android:layout_height="48px"
android:text="回上一页"
android:layout_x="110px"
android:layout_y="180px"
></Button>
</AbsoluteLayout>

④ 新建一个SecondActivity.java 的Activity 子类
package zyf.Ex11_UI_A;
import android.app.Activity;
import android.os.Bundle;
public class BMIActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
}

⑤ 在AndroidManifest.xml 中添加SecondActivity 这个Activity
必须在AndroidManifest 中注册新的
Activity,否则程序出错
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="zyf.Ex11_UI_A"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/icon"
android:label="@string/app_name" >
<activity android:name=".Ex11_UI_A"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name="BMIActivity"></activity>
</application>
<uses-sdk android:minSdkVersion="2" />
</manifest>

上一例子中新添加
⑥ 修改mainActivity.java 代码
package zyf.Ex11_UI_A;
import android.app.Activity;/* import 相关class */
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.Toast;
public class Ex11_UI_A extends Activity {
protected int my_requestCode = 1550;
private EditText edit_height;
private RadioButton radiobutton_Man, radiobutton_Woman;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
/* 载入main.xml Layout */
setContentView(R.layout.main);
/* 以findViewById()取得Button 对象,并添加onClickListener */
Button ok = (Button) findViewById(R.id.button_OK);
edit_height = (EditText) findViewById(R.id.height_Edit);
radiobutton_Man = (RadioButton) findViewById(R.id.Sex_Man);
radiobutton_Woman = (RadioButton) findViewById(R.id.Sex_Woman);
ok.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
try {
/* 取得输入的身高*/
double height = Double.parseDouble(edit_height.getText()
.toString());
/* 取得选择的性别*/
String sex = "";
if (radiobutton_Man.isChecked()) {
sex = "M";
} else {
sex = "F";
}
/* new 一个Intent 对象,并指定class */
Intent intent = new Intent();
intent.setClass(Ex11_UI_A.this, BMIActivity.class);
/* new 一个Bundle对象,并将要传递的数据传入*/
Bundle bundle = new Bundle();
bundle.putDouble("height", height);
bundle.putString("sex", sex);
/* 将Bundle 对象assign 给Intent */
intent.putExtras(bundle);
/* 调用Activity EX03_10_1 */
startActivityForResult(intent, my_requestCode);
} catch (Exception e) {
// TODO: handle exception
Toast.makeText(Ex11_UI_A.this,
R.string.errorString, Toast.LENGTH_LONG).show();
}
}
});
} @Override
protected void onActivityResult(int requestCode, int resultCode,
Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
switch (resultCode) {
case RESULT_OK:
/* 取得来自Activity2 的数据,并显示于画面上*/
Bundle bunde = data.getExtras();
String sex = bunde.getString("sex");
double height = bunde.getDouble("height");
edit_height.setText("" + height);
if (sex.equals("M")) {
radiobutton_Man.setChecked(true);
} else {
radiobutton_Woman.setChecked(true);
} break break;
default:
break;
}
}
}

新重写方法,等待返回结果
⑦ 修改SecondActivity.java 代码
package zyf.Ex11_UI_A;
/* import 相关class */
import java.text.DecimalFormat;
import java.text.NumberFormat;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class BMIActivity extends Activity {
private Intent intent;
private Bundle bunde;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
/* 加载main.xml Layout */
setContentView(R.layout.mylayout);
/* 取得Intent 中的Bundle 对象*/
intent = this.getIntent();
bunde = intent.getExtras();
/* 取得Bundle 对象中的数据*/
String sex = bunde.getString("sex");
double height = bunde.getDouble("height");
/* 判断性别*/
String sexText = "";
if (sex.equals("M")) {
sexText = "男性";
} else {
sexText = "女性";
} /* 取得标准体重*/
String weight = this.getWeight(sex, height);
/* 设置输出文字*/
TextView tv1 = (TextView) findViewById(R.id.text1);
tv1.setText("你是一位" + sexText + "\n你的身高是" + height +
"厘米\n你的标准体重是"+ weight + "公斤");
/* 以findViewById()取得Button 对象,并添加onClickListener */
Button b1 = (Button) findViewById(R.id.button_back);
b1.setOnClickListener(new Button.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
/* 返回result 回上一个activity */
BMIActivity.this.setResult(RESULT_OK, intent);
/* 结束这个activity */
BMIActivity.this.finish();
}
});
} /* 四舍五入的method */
private String format(double num) {
NumberFormat formatter = new DecimalFormat("0.00");
String s = formatter.format(num);
return s;
} /* 以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;
}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值