深入浅出Android:加入新的Activity(BMI)

1、如图

2、

Report.java
 1 package example.bmi;
 2 import android.app.Activity;
 3 import android.os.Bundle;
 4 
 5 
 6 public class Report extends Activity {
 7     @Override
 8     protected void onCreate(Bundle savedInstanceState) {
 9         super.onCreate(savedInstanceState);
10         setContentView(R.layout.report);
11     }
12 }

3、

MainActivity
  1 package example.bmi;
  2 
  3 import java.text.DecimalFormat;
  4 
  5 import android.net.Uri;
  6 import android.os.Bundle;
  7 import android.app.Activity;
  8 import android.app.AlertDialog;
  9 import android.content.ClipData.Item;
 10 import android.content.DialogInterface;
 11 import android.content.Intent;
 12 import android.view.Menu;
 13 import android.view.MenuItem;
 14 import android.view.View;
 15 import android.view.View.OnClickListener;
 16 import android.widget.Button;
 17 import android.widget.EditText;
 18 import android.widget.TextView;
 19 
 20 public class MainActivity extends Activity {
 21 
 22     @Override
 23     protected void onCreate(Bundle savedInstanceState) {
 24         super.onCreate(savedInstanceState);
 25         setContentView(R.layout.activity_main);
 26         
 27         findViews();
 28         setListensers();
 29     }
 30     private Button button_calc;
 31     private EditText field_height;
 32     private EditText field_weight;
 33     private TextView view_result;
 34     private TextView view_suggest;
 35     private void findViews()
 36     {
 37         button_calc=(Button)findViewById(R.id.submit);
 38         field_height=(EditText)findViewById(R.id.height);
 39         field_weight=(EditText)findViewById(R.id.weight);
 40         view_result=(TextView)findViewById(R.id.result);
 41         view_suggest=(TextView)findViewById(R.id.suggest);
 42     }
 43     private void setListensers()
 44     {
 45         button_calc.setOnClickListener(calcBMI);
 46     }
 47     private Button.OnClickListener calcBMI=new Button.OnClickListener()
 48     {
 49         public void onClick(View v)
 50         {
 51             //switch to report page
 52             Intent intent = new Intent();
 53             intent.setClass(MainActivity.this, Report.class);
 54             startActivity(intent);
 55     /*        DecimalFormat nf=new DecimalFormat("0.00");
 56 
 57             double height=Double.parseDouble(field_height.getText().toString())/100;
 58             double weight=Double.parseDouble(field_weight.getText().toString());
 59             double BMI=weight/(height*height);
 60             view_result.setText(getText(R.string.bmi_result)+nf.format(BMI));
 61             //give health advice
 62             if(BMI>25)
 63             {
 64                 view_suggest.setText(R.string.advice_heavy);
 65             }
 66             else if(BMI<20)
 67             {
 68                 view_suggest.setText(R.string.advice_light);
 69                }
 70             else
 71             {
 72                 view_suggest.setText(R.string.advice_average);
 73             }*/
 74         //    openOptionsDialog();
 75         }    
 76     };
 77     protected static final int MENU_ABOUT = Menu.FIRST;
 78     protected static final int MENU_Quit = Menu.FIRST+1;
 79     
 80     @Override
 81     public boolean onCreateOptionsMenu(Menu menu)
 82     {
 83         super.onCreateOptionsMenu(menu);
 84         menu.add(0, MENU_ABOUT, 0, "关于");
 85         menu.add(0, MENU_Quit, 0, "结束");
 86         return true;
 87     }
 88     public boolean onOptionsItemSelected(MenuItem item)
 89     {
 90         super.onOptionsItemSelected(item);
 91         switch(item.getItemId())
 92         {
 93         case MENU_ABOUT:
 94             openOptionsDialog();
 95             break;
 96         case MENU_Quit:
 97             finish();
 98             break;
 99         }
100         return true;
101     }
102     private void openOptionsDialog()
103     {
104         new AlertDialog.Builder(MainActivity.this)
105             .setTitle(R.string.about_title)
106             .setMessage(R.string.about_msg)
107             .setPositiveButton(R.string.ok_label,
108                     new DialogInterface.OnClickListener() {
109                 public void onClick(
110                         DialogInterface dialoginterface,int i){
111                    }
112                 
113                 })
114             .setNegativeButton(R.string.homepage_label,
115                     new DialogInterface.OnClickListener() {
116                         
117                         @Override
118                         public void onClick(DialogInterface dialog, int which) {
119                             // TODO Auto-generated method stub
120                             Uri uri=Uri.parse(getString(R.string.homepage_uri));
121                             Intent intent=new Intent(Intent.ACTION_VIEW,uri);
122                             startActivity(intent);
123                         }
124                     })
125             .show();
126     }
127 
128   /*  @Override
129     public boolean onCreateOptionsMenu(Menu menu) {
130         // Inflate the menu; this adds items to the action bar if it is present.
131         getMenuInflater().inflate(R.menu.activity_main, menu);
132         return true;
133     }
134     */
135 }

4、layout/report.xml

report.xml
 1 <?xml version="1.0" encoding="utf-8"?>
 2 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     xmlns:tools="http://schemas.android.com/tools"
 4     android:layout_width="match_parent"
 5     android:layout_height="match_parent"
 6     tools:context=".MainActivity" >
 7 
 8     <TextView
 9         android:layout_width="fill_parent"
10         android:layout_height="wrap_content"
11         android:text="Hello World,Bmi!" 
12         />
13 </RelativeLayout>

5、values/report.xml

1 <?xml version="1.0" encoding="utf-8"?>
2 <resources>
3 
4     <string name="report_title">BMI 报告</string>
5 
6 </resources>

6、

AndroidManifest.xml
 1 <?xml version="1.0" encoding="utf-8"?>
 2 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
 3     package="example.bmi"
 4     android:versionCode="1"
 5     android:versionName="1.0" >
 6 
 7     <uses-sdk
 8         android:minSdkVersion="8"
 9         android:targetSdkVersion="14" />
10 
11     <application
12         android:allowBackup="true"
13         android:icon="@drawable/ic_launcher"
14         android:label="@string/app_name"
15         android:theme="@style/AppTheme" >
16         <activity
17             android:name="example.bmi.MainActivity"
18             android:label="@string/app_name" >
19             <intent-filter>
20                 <action android:name="android.intent.action.MAIN" />
21 
22                 <category android:name="android.intent.category.LAUNCHER" />
23             </intent-filter>
24         </activity>
25         <activity android:name="Report"
26                   android:label="@string/report_title">
27         </activity>
28     </application>
29 
30 </manifest>

 

 

 

转载于:https://www.cnblogs.com/huanghuang/archive/2012/11/30/2796836.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值