传送数据到新意图(Intent)
Intent可以分为两种类型,“默认的”与“自定义的”。
我们可以使用IntentFilter过滤和寻找对应的Intent.
Activity分为两种类型:独立的,相依赖的。
相依赖的Activity是需要与其它Activity交换数据的一种Activity.
相依赖的Activity又分为单向和双向。
传送方:
package com.demo.android.bmi;
import java.net.URISyntaxException;
import java.text.DecimalFormat;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View.OnClickListener;
import android.view.View;
public class bmi extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
findViews();
setListeners();
}
private Button button_calc;
private EditText field_height;
private EditText field_weight;
private void findViews()
{
button_calc=(Button)findViewById(R.id.submit);
field_height=(EditText)findViewById(R.id.height);
field_weight=(EditText)findViewById(R.id.weight);
}
private void setListeners()
{
button_calc.setOnClickListener(calcBMI);
}
private OnClickListener calcBMI=new OnClickListener()
{
public 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);
}
};
}
接收方:
package com.demo.android.bmi;
import java.text.DecimalFormat;
import com.demo.android.bmi.R;
import android.view.View;
import android.view.View.OnClickListener;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class Report extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.report);
findViews();
showResults();
setListeners();
}
private TextView view_result;
private TextView view_suggest;
private Button button_back;
private void findViews()
{
button_back=(Button)findViewById(R.id.back);
view_result =(TextView)findViewById(R.id.result);
view_suggest =(TextView)findViewById(R.id.suggest);
}
private void setListeners()
{
button_back.setOnClickListener(button_back_click);
}
private OnClickListener button_back_click=new OnClickListener()
{
public void onClick(View v)
{
Report.this.finish();
}
};
private void showResults()
{
DecimalFormat nf=new DecimalFormat("0.00");
try{
Bundle bundle=this.getIntent().getExtras();//接收信息
double height=Double.parseDouble(bundle.getString("KEY_HEIGHT"))/100;
double weight=Double.parseDouble(bundle.getString("KEY_WEIGHT"));
double bmi=weight/(height*height);
view_result.setText("Your bmi is "+nf.format(bmi));
if(bmi>25)
view_suggest.setText(R.string.advice_heavy);
else if(bmi<20)
view_suggest.setText(R.string.advice_light);
else view_suggest.setText(R.string.advice_average);
//openOptionsDialog();
}
catch(Exception obj)
{
Toast.makeText(Report.this, R.string.input_error, Toast.LENGTH_SHORT).show();
}
}
}