安卓 激活应用组件 intent
生活随笔
收集整理的這篇文章主要介紹了
安卓 激活应用组件 intent
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
在執(zhí)行應(yīng)用程序中需要調(diào)用另一個(gè)活動(dòng),也就是實(shí)現(xiàn)頁(yè)面跳轉(zhuǎn)
1. 顯式intent 跳轉(zhuǎn)頁(yè)面
1.1 帶參 一個(gè)或多個(gè)
<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>主活動(dòng)
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);// 接收數(shù)據(jù)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);}第二個(gè)頁(yè)面
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);// 接收數(shù)據(jù)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 無(wú)參數(shù) 跳轉(zhuǎn)頁(yè)面
// 添加點(diǎn)擊事件 btn1.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View arg0) {// TODO Auto-generated method stubIntent intent = new Intent(MainActivity.this, Two.class);// 啟動(dòng)另外一個(gè)活動(dòng)startActivity(intent);} });2 注冊(cè)活動(dòng) AndroidManifest.xml
<!-- 注冊(cè)活動(dòng) --> <activityandroid:name=".Two"><intent-filter><action android:name="two" /><category android:name="android.intent.category.DEFAULT" /></intent-filter> </activity>3 案例:隱式 intent撥打電話
| ACTION_CALL | 活動(dòng) | 撥通電話的界面 |
| ACTION_EDIT | 活動(dòng) | 填號(hào)碼的界面 |
| ACTION_MAIN | 活動(dòng) | |
| 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="輸入手機(jī)號(hào)" /><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>權(quán)限添加 AndroidMainifest.xml
<uses-permission android:name="android.permission.CALL_PHONE"/>總結(jié)
以上是生活随笔為你收集整理的安卓 激活应用组件 intent的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: Ubuntu伪分布式hadoop安装
- 下一篇: 安卓 简单的登录案例