首页 > 代码库 > Android带返回值的窗口跳转
Android带返回值的窗口跳转
1、AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.fish.helloworld" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="17" android:targetSdkVersion="17" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name=".Receive" android:label="@string/title_activity_receive" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name=".Send" android:label="@string/title_activity_send" > </activity> </application></manifest>
2、Receive
package com.fish.helloworld;import com.fish.helloworld.Forwarding.backButton_OnClickListener;import android.app.Activity;import android.content.Intent;import android.os.Bundle;import android.text.Editable;import android.view.Menu;import android.view.MenuItem;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.TextView;public class Receive extends Activity { private backButton_OnClickListener backButtonListener = new backButton_OnClickListener(); private TextView m_TextView; static final private int GET_CODE = 0; class backButton_OnClickListener implements OnClickListener{ public void onClick(View v){ Intent intent = new Intent(Receive.this, Send.class); startActivityForResult(intent, GET_CODE); } } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.receive_result); final Button backButton = (Button)findViewById(R.id.button1); backButton.setOnClickListener(backButtonListener); m_TextView = (TextView)findViewById(R.id.textView1); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data){ if(requestCode == GET_CODE){ String text = ""; if(resultCode == RESULT_CANCELED){ text = "Corkey"; }else{ text = "Violet"; } m_TextView.setText(text); } } }
3、Send
package com.fish.helloworld;import com.fish.helloworld.Receive.backButton_OnClickListener;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.view.View.OnClickListener;import android.widget.Button;public class Send extends Activity { private backButton_OnClickListener backButtonListener = new backButton_OnClickListener(); private backButton_OnClickListener2 backButtonListener2 = new backButton_OnClickListener2(); class backButton_OnClickListener implements OnClickListener{ public void onClick(View v){ setResult(RESULT_OK, (new Intent().setAction("Corkey!"))); finish(); } } class backButton_OnClickListener2 implements OnClickListener{ public void onClick(View v){ setResult(RESULT_CANCELED, (new Intent().setAction("Violet!"))); finish(); } } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.send_result); final Button backButton = (Button)findViewById(R.id.button1); backButton.setOnClickListener(backButtonListener); final Button backButton2 = (Button)findViewById(R.id.button2); backButton2.setOnClickListener(backButtonListener2); }}
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。