原理图:
1)main.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" android:orientation="vertical" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="用户名" /> <EditText android:id="@+id/et_username" android:layout_width="fill_parent" android:layout_height="wrap_content" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="get" android:text="获取" /> </LinearLayout>
2)main2.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" android:orientation="vertical" > <ListView android:id="@+id/listview" android:layout_width="fill_parent" android:layout_height="fill_parent" /> </LinearLayout>
3)MainActivity
package com.njupt.openactivityforresult; 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 { private EditText et_username; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); et_username = (EditText) findViewById(R.id.et_username); } public void get(View v){ Intent intent = new Intent(this,Main2Activity.class); startActivityForResult(intent,100); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if(requestCode == 100){ if(data != null){ 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; } }
package com.njupt.openactivityforresult; import android.app.Activity; import android.content.Intent; import android.graphics.Color; import android.os.Bundle; import android.view.View; import android.view.ViewGroup; import android.widget.AdapterView.OnItemClickListener; import android.widget.AdapterView; import android.widget.BaseAdapter; import android.widget.ListView; import android.widget.TextView; public class Main2Activity extends Activity { private ListView listview; private MyBaseAdapter adapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main2); listview = (ListView) findViewById(R.id.listview); adapter = new MyBaseAdapter(); listview.setAdapter(adapter); listview.setOnItemClickListener(new MyOnItemClickListener()); } private class MyOnItemClickListener implements OnItemClickListener{ @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { //在listview中获取某一item的方法1: // Intent intent = new Intent(); // String username = (String) adapter.getItem(position); // intent.putExtra("username", username); // setResult(200,intent); // finish(); //在listview中获取某一item的方法2: String username = (String) parent.getItemAtPosition(position); Intent intent = new Intent(); intent.putExtra("username",username); setResult(200, intent); finish(); } } private class MyBaseAdapter extends BaseAdapter{ String[] names = new String[]{"章泽天","章泽天1","章泽天2","章泽天3","章泽天4"}; 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 convertView, ViewGroup parent) { TextView tv = new TextView(getApplicationContext()); tv.setText(names[position]); tv.setTextSize(25); tv.setTextColor(Color.GREEN); return tv; } } }
5)AndroidManifest.xml
还是那一句话,最后别忘了在清单文件中吧Main2Activity注册上
<activity android:name="com.njupt.openactivityforresult.Main2Activity" android:label="选择联系人"/>