android--Activity有傳回值的跳轉

來源:互聯網
上載者:User

標籤:

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"    package="com.hanqi.test4">    <application        android:allowBackup="true"        android:icon="@mipmap/ic_launcher"        android:label="@string/app_name"        android:supportsRtl="true"        android:theme="@style/AppTheme">        <activity android:name=".MainActivity"            >            <intent-filter>                <action android:name="android.intent.action.MAIN" />                <category android:name="android.intent.category.LAUNCHER" />            </intent-filter>        </activity>        <activity android:name=".Main2Activity"></activity>    </application></manifest>

MainActivity

package com.hanqi.test4;import android.content.Intent;import android.os.Bundle;import android.support.v7.app.AppCompatActivity;import android.util.Log;import android.view.View;import android.widget.EditText;import android.widget.Toast;/** * Created by Administrator on 2016/3/21. */public class MainActivity extends AppCompatActivity {    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);    setContentView(R.layout.main_layout);    }    //普通方式    public void ONCLICK(View v)    {        Log.e("T4TAG","按鈕的點擊監聽被觸發");        //靜態方法        //直接用類名就可以調用,不需要執行個體化        //構建了一個Toast執行個體        //方法連        Toast.makeText(this,"按鈕的點擊監聽被觸發",Toast.LENGTH_LONG).show();//        Toast toast= Toast.makeText(this,"按鈕的點擊監聽被觸發",Toast.LENGTH_LONG);//        toast.show();        //用intent        //取得要傳遞的資訊        //擷取View執行個體        EditText myet=(EditText)findViewById(R.id.myet);        String string= myet.getText().toString();        Intent intent= new Intent(this,Main2Activity.class);        //儲存內容        //getExtra Bundle 實際是一個HashMap  進行了限制        //intent.getExtras().putString("myet",string);        intent.putExtra("myet",string);        startActivity(intent);    }    //帶返回的方式    public void onCLICK(View v)    {        EditText myet=(EditText)findViewById(R.id.myet);        String string= myet.getText().toString();        Intent intent= new Intent(this,Main2Activity.class);        //儲存內容        //getExtra Bundle 實際是一個HashMap  進行了限制        //intent.getExtras().putString("myet",string);        intent.putExtra("myet",string);        //有返回資料的啟動方式        //第一個參數  intent        //第二個參數  requestCode  請求碼        startActivityForResult(intent, 1);    }    //重寫   處理返回資訊的監聽(回調方法)    //onActivityResult通用監聽  監聽所有返回資訊的    //必須要有requestCode區分有哪個請求返回的    @Override    protected void onActivityResult(int requestCode, int resultCode, Intent data) {        super.onActivityResult(requestCode, resultCode, data);        Log.e("TAG","requestCode="+requestCode+"resultCode"+resultCode);        if (requestCode ==1 )        {            if (resultCode == RESULT_OK)            {                //擷取返回資訊                String string = data.getExtras().getString("mytv");                EditText editText =(EditText)findViewById(R.id.myet);                editText.setText(string);                Toast.makeText(this, "返回資訊=" + string, Toast.LENGTH_LONG);            }            else {                Toast.makeText(this,"返回資訊有問題",Toast.LENGTH_SHORT);            }        }    }}

main_layout.xml

<?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">    <EditText        android:layout_width="100dp"        android:layout_height="wrap_content"        android:id="@+id/myet"        />    <Button        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="普通方式"        android:onClick="ONCLICK"        />    <Button        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="帶返回方式"        android:onClick="onCLICK"        /></LinearLayout>

Main2Activity

package com.hanqi.test4;import android.content.Intent;import android.os.Bundle;import android.support.v7.app.AppCompatActivity;import android.view.View;import android.widget.EditText;public class Main2Activity extends AppCompatActivity {    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main2);        //接受資訊        //擷取意圖        //傳遞過來的Intent        Intent intent=getIntent();        String s = intent.getExtras().getString("myet");        EditText mytv=(EditText)findViewById(R.id.mytv);        mytv.setText(s);    }    //普通返回    public void onclick(View V)    {        //關閉當前activity        finish();    }    public void ONclock(View v)    {        //儲存返回資料   也要用intent        EditText mytv=(EditText)findViewById(R.id.mytv);        Bundle bundle =new Bundle();        bundle.putString("mytv",mytv.getText().toString());        //設定返回資料        // 先設定ReaultCode,再設定儲存資料的意圖        setResult(RESULT_OK,new Intent().putExtra("mytv",mytv.getText().toString()));        //關閉當前activity        finish();    }}

activity_main2.xml

<?xml version="1.0" encoding="utf-8"?><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.hanqi.test4.Main2Activity">    <EditText        android:layout_width="100dp"        android:layout_height="wrap_content"        android:text="測試"        android:id="@+id/mytv"        />    <Button        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="普通返回"        android:onClick="onclick"        />    <Button        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="帶資料返回"        android:onClick="ONclock"        /></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.