Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> (Android review)打開Activity返回結果

(Android review)打開Activity返回結果

編輯:關於Android編程

一、基本知識點

其實要完成這個功能很簡單:

1、MainActivity

startActivityForResult(intent, 100);//第二個是請求碼


@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);

if(data != null){
if(resultCode == 200){//可以根據請求嗎來判斷,也可以根據結果碼來判斷

String username = data.getStringExtra("username");
et_username.setText(username);
}
}
}


2、MainActivity2


setResult(200,intent);//第一個參數是返回碼



二、代碼

1、MainActivity

package com.example.setresult;

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 {

	public EditText et_username;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		
		et_username = (EditText) findViewById(R.id.et_name);
		
		
	}

	public void get(View view){
		
		Intent intent = new Intent(this, MainActivity2.class);
		startActivityForResult(intent, 100);//第二個是請求碼
		
	}
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		
		if(data != null){
			if(resultCode == 200){//可以根據請求嗎來判斷,也可以根據結果碼來判斷
				
				String username = data.getStringExtra("username");
				et_username.setText(username);
			}
		}
	}
	
	@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;
	}

}


2、MainActivity2

package com.example.setresult;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.TextureView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

public class MainActivity2 extends Activity {

	public ListView listView;
	public MyAdapter adapter;
	
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.main2);
		
		listView = (ListView) findViewById(R.id.listview);
		
		adapter = new MyAdapter();
		listView.setAdapter(adapter);
		listView.setOnItemClickListener(new MyOnItemClickListener());
		
	}
	
	public class MyOnItemClickListener implements OnItemClickListener{

		@Override
		public void onItemClick(AdapterView arg0, View arg1, int position,
				long arg3) {
			Intent intent = new Intent(getApplicationContext(), MainActivity.class);
			
			String username = (String) adapter.getItem(position);
			intent.putExtra("username", username);
			
			setResult(200,intent);//第一個參數是返回碼
			
			finish();//講當前的activity結束掉
			
		}
		
	}
	
	public class MyAdapter extends BaseAdapter{

		String names[] =  new String[]{
			"hjd",
			"hjd1",
			"hjd2",
			"hjd3",
			"hjd4"
		};
		
		@Override
		public int getCount() {
			return names.length;
		}

		@Override
		public Object getItem(int position) {
			return names[position];
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View arg1, ViewGroup arg2) {
			TextView tv = new TextView(getApplicationContext());
			
			tv.setText(names[position]);
			tv.setTextSize(25);
			tv.setTextColor(Color.GREEN);
			
			
			return tv;
		}
		
	}
}


三、源碼下載

http://download.csdn.net/detail/caihongshijie6/7793215


四、效果圖





  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved