Android百議程序:使用Intent回傳結果

來源:互聯網
上載者:User

標籤:android   blog   http   io   ar   os   使用   sp   java   

本章使用Intent,回傳需要的結果;

流程:

啟動首頁->按下按鍵啟動第二個activity->輸入使用者名稱,按下OK按鍵->回傳使用者名稱回首頁->首頁讀取使用者名稱並顯示出來。

1 啟動首頁圖:


2 啟動第二個activity


3 輸入使用者名稱:


4 回傳到首頁,讀取並顯示:


建立項目,然後輸入對應的代碼就OK了。

1 首頁介面代碼(activity_main.xml):

<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"    tools:context="com.example.intenttest.MainActivity" >    <Button        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="@string/activate_secondary"        android:onClick="onClick" /></LinearLayout>

建立一個Button,然後指定相應時間是onClick

2 首頁的回應時間代碼:

public class MainActivity extends Activity {/* * An integer value that identifies an activity you are calling. * When an Activity returns a value, you must have a way to identify it * If the request code is set to -1, then calling it using the  * startActivityForResult() method is equivalent to calling it using * the  startActivity() method. That is, no result will be returned. */int requestCode = 1;public static<T> boolean equ(T t1, T t2){return t1 == t2;}    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);    }        public void onClick(View view){    startActivityForResult(new Intent("su.bill.intenttest.SecondaryActivity"), requestCode);    }}

這裡和一般的startActivity不用的就是使用了startActivityForResult,使用這個函數就能回傳結果了。

3 進入第二個activity的介面代碼:

<?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="horizontal" >    <EditText        android:id="@+id/txt_username"        android:layout_weight="1"        android:layout_width="0dp"        android:layout_height="wrap_content"                android:hint="@string/your_name" />    <Button        android:id="@+id/btn_OK"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:onClick="onClick"        android:text="@string/ok_button" /></LinearLayout>

簡單的一個輸入文本,一個按鍵

4 第二個activity的邏輯代碼

package com.example.intenttest;import android.app.Activity;import android.content.Intent;import android.net.Uri;import android.os.Bundle;import android.view.View;import android.widget.EditText;public class SecondaryActivity extends Activity {@Overridepublic void onCreate(Bundle savedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.secondary_activity);}public void onClick(View v){Intent data = new Intent();EditText txt_username = (EditText) findViewById(R.id.txt_username);data.setData(Uri.parse(txt_username.getText().toString()));setResult(RESULT_OK, data);finish();}}

主要是onClick代碼,建立一個Intent data之後,設定好這個Intent的內容,然後利用setResult回傳這個Intent給首頁activity,其實基本原理還是利用Intent通訊。


5 首頁邏輯的讀取並顯示結果的代碼:

    @Override    public void onActivityResult(int reqCode, int resCode, Intent data) {    if (equ(reqCode, requestCode) && equ(resCode, RESULT_OK)){    Toast.makeText(this, data.getDataString(), Toast.LENGTH_SHORT).show();    }    }
從上一個對應的activity返回的時候就自動調用處理的函數,其中的表示符號就是requestCode和返回的標識符reqCode對應起來,以便確認這個返回的Intent是對應的activity返回的。

首頁邏輯的完整代碼如下:

package com.example.intenttest;import android.app.Activity;import android.content.Intent;import android.os.Bundle;import android.view.Menu;import android.view.MenuItem;import android.view.View;import android.widget.Toast;public class MainActivity extends Activity {/* * An integer value that identifies an activity you are calling. * When an Activity returns a value, you must have a way to identify it * If the request code is set to -1, then calling it using the  * startActivityForResult() method is equivalent to calling it using * the  startActivity() method. That is, no result will be returned. */int requestCode = 1;public static<T> boolean equ(T t1, T t2){return t1 == t2;}    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);    }        public void onClick(View view){    startActivityForResult(new Intent("su.bill.intenttest.SecondaryActivity"), requestCode);    }        @Override    public void onActivityResult(int reqCode, int resCode, Intent data) {    if (equ(reqCode, requestCode) && equ(resCode, RESULT_OK)){    Toast.makeText(this, data.getDataString(), Toast.LENGTH_SHORT).show();    }    }    @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;    }    @Override    public boolean onOptionsItemSelected(MenuItem item) {        // Handle action bar item clicks here. The action bar will        // automatically handle clicks on the Home/Up button, so long        // as you specify a parent activity in AndroidManifest.xml.        int id = item.getItemId();        if (id == R.id.action_settings) {            return true;        }        return super.onOptionsItemSelected(item);    }}

其他沒說到的代碼都是建立項目的時候自動產生的程式碼了,可以暫時忽略。


就是熟悉好Android的一些機制,理解起來倒是沒有什麼難度的。


說好是百議程序,看來不能百日之內完成了,因為還有很多其他事情做。

爭取做快點吧,以後出點精品教程,把學到的,讓更多人學會也是很有意義的事情的。


Android百議程序:使用Intent回傳結果

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.