JavaShuo
Android 小項目之---标准体重测量仪:Bundle对象的实现
时间 2020-08-07
标签
android
标准
体重
测量仪
bundle
对象
实现
我认可有点儿标题党了,呵呵。其实就只是一个Activity跳转页面而后接收值作处理的例子而已。
废话很少说,直接进入正题。
首先咱们利用网友用JAVA编写的基于android的可视化GUI布局拖拉工具程序 --DroidDraw。(
点击这里下载
)布局如下界面:
此页面位于res/layout/main.xml。
制做接收值页面,页面以下:
此页面位于res/layout/mainlayout.xml,到此为止准备工做所有作好了,接下来正式进入 咱们的代码实现功能。。
主页面代码位于src/cn.terry/BundleObject.java
代码以下:
import android.app.Activity;
import android.app.AlertDialog;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.*;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
public class BundleObject extends Activity {
private Button mButton;
private EditText mEditText;
private RadioGroup mRadioGroup;
private double height;
private String Sex;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mButton=(Button)findViewById(R.id.confirm);
mButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
mEditText=(EditText)findViewById(R.id.heigt);
if(mEditText.getText().toString().length()==0)
{
/* AlertDialog builder = new AlertDialog.Builder(BundleObject.this).create();
builder.setTitle("提示");
builder.setMessage("請輸入您的身高!!");
builder.show();*/
new AlertDialog.Builder(BundleObject.this)
.setMessage("请輸入您的身高")
.setTitle("提示")
.setNeutralButton("肯定",new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
mEditText.setHighlightColor(Color.RED);
}
}).create()
.show();
return;
}
mRadioGroup=(RadioGroup)findViewById(R.id.sex);
height=Double.parseDouble(mEditText.getText().toString());
if(mRadioGroup.getCheckedRadioButtonId()==R.id.M)
{
Sex="M";
}
else
{
Sex="F";
}
Intent intent=new Intent();
intent.setClass(BundleObject.this, Next.class);
Bundle bun=new Bundle();
bun.putDouble("Height", height);
bun.putString("Sex", Sex);
intent.putExtras(bun);
startActivity(intent);
BundleObject.this.finish();
}
});
}
}
复制代码
在此有一点想让你们注意的是:弹出对话框的时候AlertDialog.Builder()这个方法在1.5以上都要加上类名.this 好比 个人页面的名字叫BundleObject.java就必须以下写下
AlertDialog.Builder(BundleObject.this)
复制代码
1.5如下的版本直接this便可。。 到此为止己经完成了一半的功能了,那么在Acitivity2(Next.java)要如何接收来自Activity1(BundleObject.java)传递过来的数据呢?试想,在Activity1是以Bundle封装对象,天然在Activity2亦是以Bundle的方式来解开封装的数据咯;程序中以
Bundle bun=this.getIntent().getExtras();
复制代码
这样的方法来取得Bundle对象传递过来的性别与身高,通过计算以后,显示在屏幕上。废话到此为止,第二个页面的处理程序以下:
import java.text.DecimalFormat;
import java.text.NumberFormat;
import android.content.Intent;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.*;
public class Next extends Activity {
private TextView mTextView01;
private TextView mTextView02;
private Button mButton1;
private String Sex;
private double Height;
private String SextText;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mainlayout);
Bundle bun=this.getIntent().getExtras();
Sex=bun.getString("Sex");
Height=bun.getDouble("Height");
if(Sex.equals("M"))
{
SextText="男性";
}
else
{
SextText="女性";
}
//取得標準體重
String Weight=getWeight(Sex, Height);
mTextView01=(TextView)findViewById(R.id.TextView01);
mTextView02=(TextView)findViewById(R.id.TextView02);
String result = "您是一位" + SextText +
"您的身高为:" + Height + "cm";
String result2= "您的标准体重为:" + Weight +"千克";
mTextView01.setText(result);
mTextView02.setText(result2);
mButton1=(Button)findViewById(R.id.Button03);
mButton1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent=new Intent();
intent.setClass(Next.this, BundleObject.class);
startActivity(intent);
Next.this.finish();
}
});
}
//四舍五入的方法
private String format(double num)
{
NumberFormat formatter=new DecimalFormat("0.00");
String s=formatter.format(num);
return s;
}
//取得體重
public 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;
}
}
复制代码
有一点须要你们注意的是,若是要在新建的项目多新建一个处理类那必需在配置文件为他显示声明,程序才会运行经过,这里配置文件java的命名方式以下:AndroidManifest.xml 至关于asp.net 的web.config为了代码的完整性,我顺便把配置文件也贴出来,高手能够直接跳过
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="cn.terry"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".BundleObject"
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=".Next"></activity>
</application>
</manifest>
复制代码