(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
四、效果图
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。