Android學(xué)習(xí)筆記--Activity中使用Intent傳值示例代碼
更新時(shí)間:2013年06月19日 16:57:45 作者:
Intent負(fù)責(zé)對(duì)應(yīng)用中一次操作的動(dòng)作、動(dòng)作涉及數(shù)據(jù)、附加數(shù)據(jù)進(jìn)行描述,Android則根據(jù)此Intent的描述,負(fù)責(zé)找到對(duì)應(yīng)的組件,將Intent傳遞給調(diào)用的組件,并完成組件的調(diào)用
Intent,又稱為意圖,是一種運(yùn)行時(shí)綁定機(jī)制,它能在程序運(yùn)行的過程中鏈接兩個(gè)不同的組件(Activity、Service、BroadcastReceiver)。通過Intent,程序可以向Android表達(dá)某種請(qǐng)求或意愿,Android會(huì)根據(jù)意愿的內(nèi)容選擇適當(dāng)?shù)慕M件來請(qǐng)求。
在這些組件之間的通訊中,主要是由Intent協(xié)助完成的。Intent負(fù)責(zé)對(duì)應(yīng)用中一次操作的動(dòng)作、動(dòng)作涉及數(shù)據(jù)、附加數(shù)據(jù)進(jìn)行描述,Android則根據(jù)此Intent的描述,負(fù)責(zé)找到對(duì)應(yīng)的組件,將Intent傳遞給調(diào)用的組件,并完成組件的調(diào)用。因此,Intent在這里起著一個(gè)媒體中介的作用,專門提供組件互相調(diào)用的相關(guān)信息,實(shí)現(xiàn)調(diào)用者與被調(diào)用者之間的解耦。
通過Intent請(qǐng)求Activity,必須在AndroidManifest.xml文件中對(duì)被請(qǐng)求的Activity新增標(biāo)簽配置,否則會(huì)導(dǎo)致錯(cuò)誤。
Intent一般包含兩個(gè)主要信息,action、data。
action:表示這個(gè)Intent此次操作的動(dòng)作。
data:表示這次動(dòng)作涉及的數(shù)據(jù)。
通過一個(gè)例子來展示Activity中使用Intent導(dǎo)向新Activity并傳遞數(shù)據(jù)。此程序僅在兩個(gè)頁面之間相互跳轉(zhuǎn),但是每次跳轉(zhuǎn)會(huì)創(chuàng)建新的Activity,所以在startActivity()之后需要調(diào)用finish()銷毀當(dāng)前Activity,如果不銷毀,多次跳轉(zhuǎn)后,程序的Activity棧中會(huì)存放多個(gè)Activity,點(diǎn)擊設(shè)備的返回按鈕,會(huì)發(fā)現(xiàn)會(huì)一直向后退。
主要步驟:
新建Android項(xiàng)目,增加新布局文件other.xml,新增Activity類otherActivity.class,用于接受Intent并展示other.xml。
在MainActivity類中,聲明一個(gè)Intent類,通過Intent的構(gòu)造函數(shù)指明源和目標(biāo)。
獲得Intent后,使用Intent.putExtra()方法對(duì)其傳入數(shù)據(jù)。
調(diào)用Activity.startActivity啟動(dòng)這個(gè)Intent。
在otherActivity類中,使用Activity.getIntent()獲得當(dāng)前Activity的Intent。
獲得Intent后,使用Intent.getXxxExtra()方法獲得其中保存的數(shù)據(jù)。
在AndroidManifest.xml配置otherActivity節(jié)點(diǎn)。
示例代碼
步驟2--3:
public class MainActivity extends Activity {
private TextView textView;
private Button btn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView=(TextView)findViewById(R.id.textView1);
btn=(Button)findViewById(R.id.button1);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Intent構(gòu)造函數(shù):Intent來源;Intent目的。
Intent intent =new Intent(MainActivity.this,otherActivity.class);
intent.putExtra("data", "當(dāng)前是頁面2,信息來自頁面1");
startActivity(intent);//啟動(dòng)Activity
finish();
}
});
}
}
步驟4--5:
public class otherActivity extends Activity {
private Button btn;
private TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.other);
textView=(TextView)findViewById(R.id.textView2);
btn=(Button)findViewById(R.id.button2);
//通過Activity.getIntent()獲取當(dāng)前頁面接收到的Intent。
Intent intent =getIntent();
//getXxxExtra方法獲取Intent傳遞過來的數(shù)據(jù)
String msg=intent.getStringExtra("data");
textView.setText(msg);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent=new Intent(otherActivity.this,MainActivity.class);
startActivity(intent);
finish();
}
});
}
}
步驟7:
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="cn.bgxt.IntentForAc.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".otherActivity"/>
</application>
從Activity中返回?cái)?shù)據(jù)
上面例子中只是介紹了Activity通過Intent傳遞數(shù)據(jù),然而在實(shí)際應(yīng)用中,不僅僅需要向Activity傳遞數(shù)據(jù),而且要從Activity中返回?cái)?shù)據(jù),雖然返回?cái)?shù)據(jù)和傳遞數(shù)據(jù)類似,但是還是有部分區(qū)別。
主要區(qū)別如下:
傳遞數(shù)據(jù)需要使用Activity.startActivityForResult()方法啟動(dòng)Activity,需要傳遞請(qǐng)求碼,而不是Activity.startActivity()。
返回?cái)?shù)據(jù)的時(shí)候,調(diào)用Activity.setResult()方法設(shè)置返回Intent以及返回碼。
需要重寫源Activity的onActivityResult()方法以便于接受返回的Intent,在onActivityResult()中會(huì)判斷請(qǐng)求碼和響應(yīng)碼。
通過一個(gè)例子說明從Activity返回?cái)?shù)據(jù)。此程序有兩個(gè)Activity,在MainActivity中輸入加法運(yùn)算的計(jì)算數(shù),跳轉(zhuǎn)到otherActivity中輸入計(jì)算結(jié)果,并在點(diǎn)擊返回后,把計(jì)算結(jié)果輸出到MainActivity中。
示例代碼
MainActivity:
public class MainActivity extends Activity {
private EditText one,two,result;
private Button btn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
one=(EditText)findViewById(R.id.one);
two=(EditText)findViewById(R.id.two);
result=(EditText)findViewById(R.id.result);
btn=(Button)findViewById(R.id.btnGo);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
int ione=Integer.parseInt(one.getText().toString());
int itwo=Integer.parseInt(two.getText().toString());
Intent intent=new Intent(MainActivity.this, otherActivity.class);
intent.putExtra("one", ione);
intent.putExtra("two", itwo);
//啟動(dòng)需要監(jiān)聽返回值的Activity,并設(shè)置請(qǐng)求碼:requestCode
startActivityForResult(intent, 1);
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
//當(dāng)otherActivity中返回?cái)?shù)據(jù)的時(shí)候,會(huì)響應(yīng)此方法
//requestCode和resultCode必須與請(qǐng)求startActivityForResult()和返回setResult()的時(shí)候傳入的值一致。
if(requestCode==1&&resultCode==2)
{
int three=data.getIntExtra("three", 0);
result.setText(String.valueOf(three));
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
otherActivity:
public class otherActivity extends Activity {
TextView tvShow;
EditText etResult;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.other);
tvShow=(TextView)findViewById(R.id.tvShow);
etResult=(EditText)findViewById(R.id.etResult);
Intent intent=getIntent();
int a=intent.getIntExtra("one", 0);
int b=intent.getIntExtra("two", 0);
tvShow.setText(a+" + "+b+" = "+" ? ");
Button btnResult=(Button)findViewById(R.id.btnReturn);
btnResult.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//新聲明一個(gè)Intent用于存放放回的數(shù)據(jù)
Intent i=new Intent();
int result=Integer.parseInt(etResult.getText().toString());
i.putExtra("three", result);
setResult(2, i);//設(shè)置resultCode,onActivityResult()中能獲取到
finish();//使用完成后結(jié)束當(dāng)前Activity的生命周期
}
});
}
}
在這些組件之間的通訊中,主要是由Intent協(xié)助完成的。Intent負(fù)責(zé)對(duì)應(yīng)用中一次操作的動(dòng)作、動(dòng)作涉及數(shù)據(jù)、附加數(shù)據(jù)進(jìn)行描述,Android則根據(jù)此Intent的描述,負(fù)責(zé)找到對(duì)應(yīng)的組件,將Intent傳遞給調(diào)用的組件,并完成組件的調(diào)用。因此,Intent在這里起著一個(gè)媒體中介的作用,專門提供組件互相調(diào)用的相關(guān)信息,實(shí)現(xiàn)調(diào)用者與被調(diào)用者之間的解耦。
通過Intent請(qǐng)求Activity,必須在AndroidManifest.xml文件中對(duì)被請(qǐng)求的Activity新增標(biāo)簽配置,否則會(huì)導(dǎo)致錯(cuò)誤。
Intent一般包含兩個(gè)主要信息,action、data。
action:表示這個(gè)Intent此次操作的動(dòng)作。
data:表示這次動(dòng)作涉及的數(shù)據(jù)。
通過一個(gè)例子來展示Activity中使用Intent導(dǎo)向新Activity并傳遞數(shù)據(jù)。此程序僅在兩個(gè)頁面之間相互跳轉(zhuǎn),但是每次跳轉(zhuǎn)會(huì)創(chuàng)建新的Activity,所以在startActivity()之后需要調(diào)用finish()銷毀當(dāng)前Activity,如果不銷毀,多次跳轉(zhuǎn)后,程序的Activity棧中會(huì)存放多個(gè)Activity,點(diǎn)擊設(shè)備的返回按鈕,會(huì)發(fā)現(xiàn)會(huì)一直向后退。
主要步驟:
新建Android項(xiàng)目,增加新布局文件other.xml,新增Activity類otherActivity.class,用于接受Intent并展示other.xml。
在MainActivity類中,聲明一個(gè)Intent類,通過Intent的構(gòu)造函數(shù)指明源和目標(biāo)。
獲得Intent后,使用Intent.putExtra()方法對(duì)其傳入數(shù)據(jù)。
調(diào)用Activity.startActivity啟動(dòng)這個(gè)Intent。
在otherActivity類中,使用Activity.getIntent()獲得當(dāng)前Activity的Intent。
獲得Intent后,使用Intent.getXxxExtra()方法獲得其中保存的數(shù)據(jù)。
在AndroidManifest.xml配置otherActivity節(jié)點(diǎn)。
示例代碼
步驟2--3:
復(fù)制代碼 代碼如下:
public class MainActivity extends Activity {
private TextView textView;
private Button btn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView=(TextView)findViewById(R.id.textView1);
btn=(Button)findViewById(R.id.button1);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Intent構(gòu)造函數(shù):Intent來源;Intent目的。
Intent intent =new Intent(MainActivity.this,otherActivity.class);
intent.putExtra("data", "當(dāng)前是頁面2,信息來自頁面1");
startActivity(intent);//啟動(dòng)Activity
finish();
}
});
}
}
步驟4--5:
復(fù)制代碼 代碼如下:
public class otherActivity extends Activity {
private Button btn;
private TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.other);
textView=(TextView)findViewById(R.id.textView2);
btn=(Button)findViewById(R.id.button2);
//通過Activity.getIntent()獲取當(dāng)前頁面接收到的Intent。
Intent intent =getIntent();
//getXxxExtra方法獲取Intent傳遞過來的數(shù)據(jù)
String msg=intent.getStringExtra("data");
textView.setText(msg);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent=new Intent(otherActivity.this,MainActivity.class);
startActivity(intent);
finish();
}
});
}
}
步驟7:
復(fù)制代碼 代碼如下:
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="cn.bgxt.IntentForAc.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".otherActivity"/>
</application>
從Activity中返回?cái)?shù)據(jù)
上面例子中只是介紹了Activity通過Intent傳遞數(shù)據(jù),然而在實(shí)際應(yīng)用中,不僅僅需要向Activity傳遞數(shù)據(jù),而且要從Activity中返回?cái)?shù)據(jù),雖然返回?cái)?shù)據(jù)和傳遞數(shù)據(jù)類似,但是還是有部分區(qū)別。
主要區(qū)別如下:
傳遞數(shù)據(jù)需要使用Activity.startActivityForResult()方法啟動(dòng)Activity,需要傳遞請(qǐng)求碼,而不是Activity.startActivity()。
返回?cái)?shù)據(jù)的時(shí)候,調(diào)用Activity.setResult()方法設(shè)置返回Intent以及返回碼。
需要重寫源Activity的onActivityResult()方法以便于接受返回的Intent,在onActivityResult()中會(huì)判斷請(qǐng)求碼和響應(yīng)碼。
通過一個(gè)例子說明從Activity返回?cái)?shù)據(jù)。此程序有兩個(gè)Activity,在MainActivity中輸入加法運(yùn)算的計(jì)算數(shù),跳轉(zhuǎn)到otherActivity中輸入計(jì)算結(jié)果,并在點(diǎn)擊返回后,把計(jì)算結(jié)果輸出到MainActivity中。
示例代碼
MainActivity:
復(fù)制代碼 代碼如下:
public class MainActivity extends Activity {
private EditText one,two,result;
private Button btn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
one=(EditText)findViewById(R.id.one);
two=(EditText)findViewById(R.id.two);
result=(EditText)findViewById(R.id.result);
btn=(Button)findViewById(R.id.btnGo);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
int ione=Integer.parseInt(one.getText().toString());
int itwo=Integer.parseInt(two.getText().toString());
Intent intent=new Intent(MainActivity.this, otherActivity.class);
intent.putExtra("one", ione);
intent.putExtra("two", itwo);
//啟動(dòng)需要監(jiān)聽返回值的Activity,并設(shè)置請(qǐng)求碼:requestCode
startActivityForResult(intent, 1);
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
//當(dāng)otherActivity中返回?cái)?shù)據(jù)的時(shí)候,會(huì)響應(yīng)此方法
//requestCode和resultCode必須與請(qǐng)求startActivityForResult()和返回setResult()的時(shí)候傳入的值一致。
if(requestCode==1&&resultCode==2)
{
int three=data.getIntExtra("three", 0);
result.setText(String.valueOf(three));
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
otherActivity:
復(fù)制代碼 代碼如下:
public class otherActivity extends Activity {
TextView tvShow;
EditText etResult;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.other);
tvShow=(TextView)findViewById(R.id.tvShow);
etResult=(EditText)findViewById(R.id.etResult);
Intent intent=getIntent();
int a=intent.getIntExtra("one", 0);
int b=intent.getIntExtra("two", 0);
tvShow.setText(a+" + "+b+" = "+" ? ");
Button btnResult=(Button)findViewById(R.id.btnReturn);
btnResult.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//新聲明一個(gè)Intent用于存放放回的數(shù)據(jù)
Intent i=new Intent();
int result=Integer.parseInt(etResult.getText().toString());
i.putExtra("three", result);
setResult(2, i);//設(shè)置resultCode,onActivityResult()中能獲取到
finish();//使用完成后結(jié)束當(dāng)前Activity的生命周期
}
});
}
}
相關(guān)文章
Android編程之SurfaceView實(shí)例詳解
這篇文章主要介紹了Android編程之SurfaceView用法,簡要分析了View和SurfaceView的區(qū)別,并結(jié)合實(shí)例形式分析了SurfaceView的具體使用步驟與相關(guān)技巧,需要的朋友可以參考下2016-02-02Android調(diào)試華為和魅族手機(jī)logcat不顯示的問題
今天小編就為大家分享一篇關(guān)于Android調(diào)試華為和魅族手機(jī)logcat不顯示的問題,小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧2018-10-10下載、編譯、運(yùn)行android 7.1系統(tǒng)詳解(ubuntu 16.0.4)
Android 7的系統(tǒng)版本新增的很多的新功能,本篇文章主要介紹了基于ubuntu 16.0.4環(huán)境的下載、編譯、運(yùn)行android 7.1系統(tǒng),有興趣的可以了解一下。2017-01-01Android調(diào)用google地圖生成路線圖實(shí)現(xiàn)代碼
Android程序調(diào)用本機(jī)google地圖并且傳遞起始和終點(diǎn)位置生成路線圖,有需要的朋有可以參考下,或許本文對(duì)你有所幫助,好了話不多說,看代碼2013-02-02Android自定義View實(shí)現(xiàn)多邊形統(tǒng)計(jì)圖示例代碼
這篇文章主要給大家介紹了關(guān)于Android自定義View如何實(shí)現(xiàn)多邊形統(tǒng)計(jì)圖的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。2018-01-01Android進(jìn)階事件分發(fā)機(jī)制解決事件沖突
這篇文章主要為大家介紹了Android進(jìn)階事件分發(fā)機(jī)制解決事件沖突過程詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-01-01