Android--activity擷取傳回值

來源:互聯網
上載者:User

標籤:

寫了一個DEMO:簡訊選擇連絡人的資料傳遞

主activity:

import android.os.Bundle;import android.app.Activity;import android.content.Intent;import android.view.Menu;import android.view.View;import android.widget.EditText;public class MainActivity extends Activity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);}public void click(View c){//跳轉至選擇連絡人ActivityIntent intent = new Intent(this, ContactActivity.class);//startActivity(intent);//用這個api啟動的Activity,在銷毀時,系統會回調onActivityResultstartActivityForResult(intent, 10);}public void click2(View v){//跳轉至選擇快捷回複的ActivityIntent intent = new Intent(this, CallbackActivity.class);startActivityForResult(intent, 20);}//如果有Activity在銷毀時返回了資料,那麼就會調用此方法來接收資料//requestCode:用來區分資料來自於哪一個Activity//resultCode:用來區分返回的資料是什麼類型的@Overrideprotected void onActivityResult(int requestCode, int resultCode, Intent data) {// TODO Auto-generated method stubsuper.onActivityResult(requestCode, resultCode, data);String name = data.getStringExtra("name");if(requestCode == 10){EditText et = (EditText)findViewById(R.id.et);et.setText(name);}else if(requestCode == 20){EditText et_content = (EditText)findViewById(R.id.et_content);et_content.setText(name);}}}
連絡人activity:

import android.app.Activity;import android.content.Intent;import android.os.Bundle;import android.view.View;import android.widget.AdapterView;import android.widget.AdapterView.OnItemClickListener;import android.widget.ArrayAdapter;import android.widget.ListView;public class ContactActivity extends Activity {@Overrideprotected void onCreate(Bundle savedInstanceState) {// TODO Auto-generated method stubsuper.onCreate(savedInstanceState);setContentView(R.layout.activity_contact);ListView lv = (ListView) findViewById(R.id.lv);final String[] objects = new String[]{"小志","逼哥","世界級XXX","國服第一"};lv.setAdapter(new ArrayAdapter<String>(this, R.layout.item_listview, R.id.tv, objects));//給listview設定條目的點擊偵聽lv.setOnItemClickListener(new OnItemClickListener() {//當某個條目被點擊時,此方法調用@Overridepublic void onItemClick(AdapterView<?> parent, View view,int position, long id) {//Activity返回時傳遞資料,也是通過意圖對象Intent data = new Intent();//把要傳遞的資料封裝至意圖對象中data.putExtra("name", objects[position]);//當前Activity銷毀時,data這個意圖就會傳遞給啟動當前Activity的那個ActivitysetResult(1, data);//銷毀當前Activityfinish();}});}@Overridepublic void onBackPressed() {// TODO Auto-generated method stubsuper.onBackPressed();}}

快捷回複activity:

import android.app.Activity;import android.content.Intent;import android.os.Bundle;import android.view.View;import android.widget.AdapterView;import android.widget.AdapterView.OnItemClickListener;import android.widget.ArrayAdapter;import android.widget.ListView;public class CallbackActivity extends Activity {@Overrideprotected void onCreate(Bundle savedInstanceState) {// TODO Auto-generated method stubsuper.onCreate(savedInstanceState);setContentView(R.layout.activity_contact);ListView lv = (ListView) findViewById(R.id.lv);final String[] objects = new String[]{"免談,沒戲,滾犢子","媳婦我錯了,求原諒","老子才是一家之主"};lv.setAdapter(new ArrayAdapter<String>(this, R.layout.item_listview, R.id.tv, objects));lv.setOnItemClickListener(new OnItemClickListener() {@Overridepublic void onItemClick(AdapterView<?> parent, View view,int position, long id) {Intent data = new Intent();data.putExtra("name", objects[position]);setResult(2, data);finish();}});}}

<LinearLayout 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"     android:orientation="vertical"    >    <LinearLayout         android:layout_width="match_parent"        android:layout_height="wrap_content"        android:orientation="horizontal"        >    <EditText        android:id="@+id/et"        android:layout_weight="1"        android:layout_width="0dp"        android:layout_height="wrap_content"        android:hint="請輸入連絡人"        />    <Button         android:layout_weight="0"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="+"        android:onClick="click"        />    </LinearLayout>    <EditText        android:id="@+id/et_content"        android:layout_weight="1"        android:layout_width="match_parent"        android:layout_height="0dp"        android:hint="請輸入簡訊內容"        android:gravity="top"        /><Button     android:layout_width="wrap_content"    android:layout_height="wrap_content"    android:text="快捷回複"    android:onClick="click2"    /><Button     android:layout_width="wrap_content"    android:layout_height="wrap_content"    android:text="傳送簡訊"    /></LinearLayout>

<?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" >        <ListView         android:id="@+id/lv"        android:layout_width="match_parent"        android:layout_height="match_parent"        ></ListView></LinearLayout>

<?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/tv"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:textSize="20sp"        /></LinearLayout>



Android--activity擷取傳回值

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.