700字范文,内容丰富有趣,生活中的好帮手!
700字范文 > Android学习笔记(四):在Activity中跳转--Intent的使用

Android学习笔记(四):在Activity中跳转--Intent的使用

时间:2019-02-26 00:15:07

相关推荐

Android学习笔记(四):在Activity中跳转--Intent的使用

Android学习笔记(四):在Activity中跳转--Intent的使用

上篇,我们实战了一个很小的项目BMI,通过BMI这个项目,可以很好的理解Activity的程序结构,以方便后面高级API的学习。本篇笔记是Android入门的最后一篇笔记。

我们来修改一下BMI项目,使修改后的程序增加一个界面,我们将BMI的计算结果和建议放在新的界面上来显示。

一.编程步骤

1.新建一个Activity类Report.java

package com.demo.android.bmi;import java.text.DecimalFormat;import android.app.Activity;import android.os.Bundle;import android.view.View;import android.widget.Button;import android.widget.TextView;public class Report extends Activity{private TextView result;private TextView suggest;private Button btn_back;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.report);findViews();showResults();setListeners();}private void findViews(){btn_back = (Button) findViewById(R.id.btn_back);suggest = (TextView) findViewById(R.id.suggest);result = (TextView) findViewById(R.id.result);}private void showResults(){Bundle bundle = this.getIntent().getExtras();DecimalFormat nf = new DecimalFormat("0.00");double height = Double.parseDouble(bundle.getString("KEY_HEIGHT"))/100;double weight = Double.parseDouble(bundle.getString("KEY_WEIGHT"));double BMI = weight/(height*height);result.setText("你的 BMI值 是:"+nf.format(BMI));if(BMI>25){suggest.setText(R.string.advice_heavy);}else if(BMI<20){suggest.setText(R.string.advice_light);}else{suggest.setText(R.string.advice_average);}}private void setListeners(){btn_back.setOnClickListener(backMain);}private Button.OnClickListener backMain = new Button.OnClickListener(){@Overridepublic void onClick(View arg0) {Report.this.finish();}};}

2.在res/layout中增加一个布局配置文件report.xml

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="/apk/res/android"android:orientation="vertical" android:layout_width="fill_parent"android:layout_height="fill_parent"><TextView android:id="@+id/result"android:layout_width="fill_parent"android:layout_height="wrap_content"android:text=""/><TextView android:id="@+id/suggest"android:layout_width="fill_parent"android:layout_height="wrap_content"android:text=""/><Button android:id="@+id/btn_back"android:layout_width="fill_parent"android:layout_height="wrap_content"android:text="@string/back"/></LinearLayout>

3.修改AndroidManifest.xml,将Report这个Activity添加进去。(可以手动修改,也可以通过可视化工具自动增加)

<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="/apk/res/android"package="com.demo.android.bmi"android:versionCode="1"android:versionName="1.0"><application android:icon="@drawable/icon" android:label="@string/app_name"><activity android:name=".BMI"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="Report"android:label="@string/report_title"></activity></application><uses-sdk android:minSdkVersion="3" /></manifest>

4.在res/values中增加一个report.xml,用来存放report页面用到的字串。

<?xml version="1.0" encoding="utf-8"?><resources><string name="report_title">BMI 报告</string><string name="back">返回</string></resources>

5.修改BMI.java(主要是onclick方法中的业务流程的处理及页面间传值的方式)

package com.demo.android.bmi;import android.app.Activity;import android.app.AlertDialog;import android.content.DialogInterface;import android.content.Intent;import .Uri;import android.os.Bundle;import android.view.Menu;import android.view.MenuItem;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.EditText;public class BMI extends Activity {private Button btn ;private EditText field_height;private EditText field_weight;protected static final int MENU_ABOUT = Menu.FIRST;protected static final int MENU_QUIT = Menu.FIRST+1;@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);findViews();setListener();}@Overridepublic boolean onCreateOptionsMenu(Menu menu) {super.onCreateOptionsMenu(menu);menu.add(0, MENU_ABOUT, 0, "关于...");menu.add(0,MENU_QUIT,0,"结束");return true;}@Overridepublic boolean onMenuItemSelected(int featureId, MenuItem item) {super.onMenuItemSelected(featureId, item);switch (item.getItemId()){caseMENU_ABOUT:openOptionsDialog();break;case MENU_QUIT:finish();break;}return true;}/*** 获取组件*/private void findViews(){btn = (Button) findViewById(R.id.submit);field_height = (EditText) findViewById(R.id.height);field_weight = (EditText) findViewById(R.id.weight);}/*** 按钮加入监听*/private void setListener(){btn.setOnClickListener(calcBMI);}/** 监听对象,监听方法*/private OnClickListener calcBMI = new OnClickListener() {@Overridepublic void onClick(View v) {//业务流程Intent intent = new Intent();intent.setClass(BMI.this, Report.class);Bundle bundle = new Bundle();bundle.putString("KEY_HEIGHT", field_height.getText().toString());bundle.putString("KEY_WEIGHT", field_weight.getText().toString());intent.putExtras(bundle);startActivity(intent);}};/*** 增加对话框*/private void openOptionsDialog(){//可监听的对话框new AlertDialog.Builder(this).setTitle(R.string.bmi_about).setMessage(R.string.bmi_msg).setPositiveButton(R.string.ok_label, new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which) {//do something...}}).setNegativeButton(R.string.homepage_label, new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which) {Uri uri = Uri.parse(getString(R.string.homepage_uri));Intent intent = new Intent(Intent.ACTION_VIEW,uri);startActivity(intent);}}).show();//短暂显示的对话框//Toast.makeText(this, R.string.bmi_msg, Toast.LENGTH_LONG).show();}}

修改完成,效果如下图,两个页面。

下篇我们来学习Android中的DEBUG记录日志与错误信息的方法。

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。