首页 > 代码库 > (Android review)打开Activity返回结果
(Android review)打开Activity返回结果
一、基本知识点
其实要完成这个功能很简单:
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
四、效果图
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。