android--Activity有返回值的跳转
生活随笔
收集整理的這篇文章主要介紹了
android--Activity有返回值的跳转
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android"package="com.hanqi.test4"><applicationandroid: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 {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main_layout);}//普通方式public void ONCLICK(View v){Log.e("T4TAG","按鈕的點擊監(jiān)聽被觸發(fā)");//靜態(tài)方法//直接用類名就可以調(diào)用,不需要實例化//構(gòu)建了一個Toast實例//方法連Toast.makeText(this,"按鈕的點擊監(jiān)聽被觸發(fā)",Toast.LENGTH_LONG).show();// Toast toast= Toast.makeText(this,"按鈕的點擊監(jiān)聽被觸發(fā)",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);//存儲內(nèi)容//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);//存儲內(nèi)容//getExtra Bundle 實際是一個HashMap 進行了限制//intent.getExtras().putString("myet",string);intent.putExtra("myet",string);//有返回數(shù)據(jù)的啟動方式//第一個參數(shù) intent//第二個參數(shù) requestCode 請求碼startActivityForResult(intent, 1);}//重寫 處理返回信息的監(jiān)聽(回調(diào)方法)//onActivityResult通用監(jiān)聽 監(jiān)聽所有返回信息的//必須要有requestCode區(qū)分有哪個請求返回的 @Overrideprotected 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"><EditTextandroid:layout_width="100dp"android:layout_height="wrap_content"android:id="@+id/myet"/><Buttonandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="普通方式"android:onClick="ONCLICK"/><Buttonandroid: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 {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main2);//接受信息//獲取意圖//傳遞過來的IntentIntent 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){//存儲返回數(shù)據(jù) 也要用intentEditText mytv=(EditText)findViewById(R.id.mytv);Bundle bundle =new Bundle();bundle.putString("mytv",mytv.getText().toString());//設置返回數(shù)據(jù)// 先設置ReaultCode,再設置存儲數(shù)據(jù)的意圖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"><EditTextandroid:layout_width="100dp"android:layout_height="wrap_content"android:text="測試"android:id="@+id/mytv"/><Buttonandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="普通返回"android:onClick="onclick"/><Buttonandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="帶數(shù)據(jù)返回"android:onClick="ONclock"/> </LinearLayout>?
轉(zhuǎn)載于:https://www.cnblogs.com/cuikang/p/5304351.html
總結(jié)
以上是生活随笔為你收集整理的android--Activity有返回值的跳转的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 数据结构顺序栈基本操作(C/C++实现)
- 下一篇: 栈——栈的定义及基本操作(初始化、判空、