欢迎访问 生活随笔!

生活随笔

当前位置: 首页 > 编程资源 > 编程问答 >内容正文

编程问答

安卓 激活应用组件 intent

发布时间:2025/5/22 编程问答 71 豆豆
生活随笔 收集整理的这篇文章主要介绍了 安卓 激活应用组件 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的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。