标签:风飞雪未扬 从零开始学android intent意图的使用
Intent意图是android中非常重要的部分,他在Activity,service中有较为广泛的应用。
1
|
public void startActivity(Intent intent)
|
普通
|
启动一个Activity,并通过Intent传送数据
|
2
|
public void startActivityForResult(Intent intent, int requestCode)
|
普通
|
启动并接收另一个Activity程序回传数据,当requestCode大于0才可以触发onActivityResult()
|
3
|
public Intent getIntent()
|
普通
|
返回启动当前Activity程序的Intent
|
4
|
protected void onActivityResult(int requestCode, int resultCode, Intent data)
|
普通
|
当需要接收Intent回传数据的时候覆写此方法对回传操作进行处理
|
5
|
public void finish()
|
普通
|
调用此方法会返回之前的Activity程序,并自动调用onActivityResult()方法
|
6
|
public final Cursor managedQuery (Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder)
|
普通
|
处理返回的Cursor结果集
|
接下里用例子逐步讲解
最简单的Intent
主界面xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/hello_world" /> <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_centerHorizontal="true" android:layout_marginBottom="141dp" android:text="转跳" /> </RelativeLayout>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="我是第二屏界面" /> </RelativeLayout>
package com.example.intent1; import android.os.Bundle; import android.app.Activity; import android.content.Intent; import android.view.Menu; import android.view.View; import android.widget.Button; public class MainActivity extends Activity { private Button button; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); button = (Button) this.findViewById(R.id.button1); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub Intent intent = new Intent(MainActivity.this, Other.class);//创建Intent 对象 startActivity(intent);//开启intent MainActivity.this.finish();//使当前Activity结束 } }); } }
package com.example.intent1; import android.app.Activity; import android.os.Bundle; import android.text.style.SuperscriptSpan; public class Other extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.other); } }
接下来看一看可以传递数据的Intent
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity" > <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_centerHorizontal="true" android:text="传递" /> <EditText android:id="@+id/edit" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" android:ems="10" android:text=" /> </RelativeLayout> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity" > <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_centerHorizontal="true" android:text="传递" /> <EditText android:id="@+id/edit" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" android:ems="10" android:text="" /> </RelativeLayout>
Other.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <TextView android:id="@+id/textView1" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="TextView" /> <Button android:id="@+id/button2" android:gravity="center_horizontal" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="获取数据" /> </LinearLayout>
java文件
package com.example.intent2; import android.os.Bundle; import android.R.integer; import android.app.Activity; import android.content.Intent; import android.view.Menu; import android.view.View; import android.widget.Button; import android.widget.EditText; public class MainActivity extends Activity { private Button button; private EditText info; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); button=(Button)this.findViewById(R.id.button1); info=(EditText)this.findViewById(R.id.edit); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub Intent intent=new Intent(MainActivity.this,Other.class);//设置意图 intent.putExtra("info", info.getText().toString());//为意图增加附加信息 startActivity(intent);//开始意图 MainActivity.this.finish();//终止当前Ativity } }); } }
Other.java
package com.example.intent2; 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 Other extends Activity { Intent intent; private Button button; private TextView text; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); super.setContentView(R.layout.other); button=(Button)this.findViewById(R.id.button2); text=(TextView)this.findViewById(R.id.textView1); intent=super.getIntent();//获取意图对象 button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub String info=intent.getStringExtra("info");//获取传递的数据 text.setText(info);//显示数据 } }); } }
使用Intent传递并返回数据
借助Intent实现成人身高体重测试程序采用BMI 法 体重指数 = 体重(公斤) 除 身高(米)的平方 kg/m2 正常体重 : 体重指数 = 18 - 25 超重 : 体重指数 = 25 - 30 轻度肥胖 : 体重指数 > 30 中度肥胖 : 体重指数 > 35 重度肥胖 : 体重指数 > 40
xml
package com.example.inten3; import android.os.Bundle; import android.app.Activity; import android.content.Intent; import android.view.Menu; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; public class MainActivity extends Activity { private Button resetInfo,getInfo; private EditText height,weight; private TextView detail; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); super.setContentView(R.layout.activity_main); resetInfo=(Button)this.findViewById(R.id.button1);//重置信息按钮 getInfo=(Button)this.findViewById(R.id.button2);//获取信息按钮 height=(EditText)this.findViewById(R.id.editText1);//身高输入框 weight=(EditText)this.findViewById(R.id.editText2);//体重输入框 detail=(TextView)this.findViewById(R.id.textView3);//信息显示 // 重置按钮的监听 resetInfo.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub //将他们都设置为空 weight.setText(""); height.setText(""); detail.setText(""); } }); // 获取信息按钮的监听 getInfo.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub Intent intent =new Intent(MainActivity.this, Other.class);//创建意图对象 // 增加附加信息 intent.putExtra("weight", Double.valueOf(weight.getText().toString())); intent.putExtra("height", Double.valueOf(height.getText().toString())); //设置启动并传递请求码 startActivityForResult(intent, 1); } }); } // 处理返回的信息 @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { // TODO Auto-generated method stub switch (resultCode) { case RESULT_OK: //设置信息 MainActivity.this.detail.setText(data.getStringExtra("endMsg")); break; default: break; } } }
package com.example.inten3; 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 Other extends Activity { private Button returnMsg, resetbut; private TextView weightMsg, heightMsg; private Double weight, height; private String endMsg; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); super.setContentView(R.layout.other); returnMsg = (Button) this.findViewById(R.id.Otherbutton1); resetbut = (Button) this.findViewById(R.id.Otherbutton2); weightMsg = (TextView) this.findViewById(R.id.OthertextView1); heightMsg = (TextView) this.findViewById(R.id.OthertextView2); // 获取意图对象 Intent intent = super.getIntent(); // 获得参数 weight = intent.getDoubleExtra("weight", 0); height = intent.getDoubleExtra("height", 0); weightMsg.setText("您的体重:" + weight + "KG"); heightMsg.setText("您的身高:" + height + "CM"); returnMsg.setOnClickListener(new View.OnClickListener() { // 对传入的数据进项判断和处理 @Override public void onClick(View v) { // TODO Auto-generated method stub Double heightNum = height / 100; double endNum = weight / (heightNum * heightNum); if (endNum >= 18 && endNum <= 25) { endMsg = "您的身体很正常哦,继续保持亲。"; } else if (endNum < 18) { endMsg = "亲啊,不能总是减肥啊,您现在可是偏瘦哦,当心身体"; } else if (endNum > 25 && endNum <= 30) { endMsg = "您现在有一点胖哦,不过不要紧,要注意饮食哦"; } else if (endNum > 30 && endNum <= 35) { endMsg = "亲,您已经进入肥胖人群 了哦,注意锻炼哦"; } else if (endNum > 35) { endMsg = "额,你,你。。你怎么可以这样,还不赶快减肥,当心找不到对象哦"; } // 将参数设置到intnet上 Other.this.getIntent().putExtra("endMsg", endMsg); // 设置返回集,并设置返回接受码 Other.this.setResult(RESULT_OK, Other.this.getIntent()); // 将dangqianActivity结束掉 Other.this.finish(); } }); // 重新输入监听 resetbut.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub // 设置返回集合取消状态码 Other.this.setResult(RESULT_CANCELED, Other.this.getIntent()); // 结束当前Activity Other.this.finish(); } }); } }
学习了intent的基本用途,下节我们来学习Intent调用系统指令来完成一些其他操作
下节预报:Intent进阶
标签:风飞雪未扬 从零开始学android intent意图的使用
原文地址:http://blog.csdn.net/u013616976/article/details/39136781