安卓 激活应用组件 intent
生活随笔
收集整理的这篇文章主要介绍了
安卓 激活应用组件 intent
小编觉得挺不错的,现在分享给大家,帮大家做个参考.
在执行应用程序中需要调用另一个活动,也就是实现页面跳转
1. 显式intent 跳转页面
1.1 带参 一个或多个
<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"tools:context="${relativePackage}.${activityClass}" ><EditTextandroid:id="@+id/editText1"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_alignParentLeft="true"android:layout_below="@+id/textView1"android:layout_marginLeft="44dp"android:layout_marginTop="32dp"android:ems="10"android:text="输入用户名"><requestFocus /></EditText><EditTextandroid:id="@+id/editText2"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_below="@+id/editText1"android:layout_centerHorizontal="true"android:layout_marginTop="61dp"android:ems="10"android:text="密码"/><Buttonandroid:id="@+id/button1"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_alignLeft="@+id/editText2"android:layout_centerVertical="true"android:layout_marginLeft="22dp"android:text="Button" /></RelativeLayout>主活动
TextView textView1;@Override protected void onCreate(Bundle savedInstanceState) {// TODO Auto-generated method stubsuper.onCreate(savedInstanceState);setContentView(R.layout.one);textView1 = (TextView) findViewById(R.id.textView1);// 接收数据Intent intent = getIntent();// 方法一String username = intent.getStringExtra("username");// 方法2//Bundle bundle = intent.getExtras();//String username = bundle.getString("username");textView1.setText(username);textView1.setTextColor(Color.RED);textView1.setTextSize(20);}第二个页面
TextView textView1;@Override protected void onCreate(Bundle savedInstanceState) {// TODO Auto-generated method stubsuper.onCreate(savedInstanceState);setContentView(R.layout.one);textView1 = (TextView) findViewById(R.id.textView1);// 接收数据Intent intent = getIntent();// 方法一String username = intent.getStringExtra("username");// 方法2//Bundle bundle = intent.getExtras();//String username = bundle.getString("username");textView1.setText(username);textView1.setTextColor(Color.RED);textView1.setTextSize(20);}1.2 无参数 跳转页面
// 添加点击事件 btn1.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View arg0) {// TODO Auto-generated method stubIntent intent = new Intent(MainActivity.this, Two.class);// 启动另外一个活动startActivity(intent);} });2 注册活动 AndroidManifest.xml
<!-- 注册活动 --> <activityandroid:name=".Two"><intent-filter><action android:name="two" /><category android:name="android.intent.category.DEFAULT" /></intent-filter> </activity>3 案例:隐式 intent拨打电话
| ACTION_CALL | 活动 | 拨通电话的界面 |
| ACTION_EDIT | 活动 | 填号码的界面 |
| ACTION_MAIN | 活动 | |
| ACTION_BATTERY_LOW | ||
| ACTION_HEADSERT_LPLUG |
MainActivity.java
public class MainActivity extends Activity {EditText editText1;Button btn1;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);editText1 = (EditText) findViewById(R.id.editText1);btn1 = (Button) findViewById(R.id.button1);btn1.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View arg0) {// TODO Auto-generated method stubString num = editText1.getText().toString();Intent intent = new Intent(Intent.ACTION_CALL,Uri.parse("tel:"+ num));startActivity(intent);}}); }activity_main.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"tools:context="${relativePackage}.${activityClass}" ><TextViewandroid:id="@+id/textView1"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="输入手机号" /><EditTextandroid:id="@+id/editText1"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_alignLeft="@+id/button1"android:layout_below="@+id/textView1"android:layout_marginTop="24dp"android:ems="10" /><Buttonandroid:id="@+id/button1"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_alignParentLeft="true"android:layout_below="@+id/editText1"android:layout_marginLeft="26dp"android:layout_marginTop="118dp"android:text="拨打" /></RelativeLayout>权限添加 AndroidMainifest.xml
<uses-permission android:name="android.permission.CALL_PHONE"/>总结
以上是生活随笔为你收集整理的安卓 激活应用组件 intent的全部内容,希望文章能够帮你解决所遇到的问题。
- 上一篇: Ubuntu伪分布式hadoop安装
- 下一篇: 安卓 简单的登录案例