androidstartActivityForResultの例



Android Startactivityforresult Example



ActivityForResult.java

public class ActivityForResult extends Activity { Button bn EditText city @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState) setContentView(R.layout.main) // get the components on the interface bn = (Button) findViewById(R.id.bn) city = (EditText) findViewById(R.id.city) // Bind event listener for the button bn.setOnClickListener(new OnClickListener() { @Override public void onClick(View source) { // Create the required corresponding to the target Activity of Intent Intent intent = new Intent(ActivityForResult.this, SelectCityActivity.class) // start specified Activity and wait for the results returned, where 0 is a request code for identifying the request startActivityForResult(intent, 0) } }) } // override this method in a callback way to get the results returned by the specified Activity @Override public void onActivityResult(int requestCode , int resultCode, Intent intent) { // When requestCode, resultCode 0 simultaneously, that is, to deal with specific results if (requestCode == 0 && resultCode == 0) { // Remove Intent data in the Extras Bundle data = intent.getExtras() // retrieve data in Bundle String resultCity = data.getString('city') // modify the contents of the text box city city.setText(resultCity) } } }

SelectCityActivity.java



public class SelectCityActivity extends ExpandableListActivity { // define an array provinces private String[] provinces = new String[] { 'Guangdong', 'Guangxi', 'Hunan'} private String[][] cities = new String[][] { { 'Guangzhou', 'Shenzhen', 'Zhuhai', 'Zhongshan'}, { 'Guilin', 'Liuzhou', 'Nanning', 'North'}, { 'Changsha', 'Yueyang', 'Hengyang', 'Zhuzhou'} } public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState) ExpandableListAdapter adapter = new BaseExpandableListAdapter() { // Get the location specified group, sub-sub-list item at the specified list item data @Override public Object getChild(int groupPosition, int childPosition) { return cities[groupPosition][childPosition] } @Override public long getChildId(int groupPosition, int childPosition) { return childPosition } @Override public int getChildrenCount(int groupPosition) { return cities[groupPosition].length } private TextView getTextView() Gravity.LEFT) textView.setPadding(36, 0, 0, 0) textView.setTextSize(20) return textView // This method determines the appearance of each sub-option @Override public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) { TextView textView = getTextView() textView.setText(getChild(groupPosition, childPosition) .toString()) return textView } // Get set of data at the specified set of locations @Override public Object getGroup(int groupPosition) { return provinces[groupPosition] } @Override public int getGroupCount() { return provinces.length } @Override public long getGroupId(int groupPosition) { return groupPosition } // This method determines the appearance of each set of options @Override public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) { LinearLayout ll = new LinearLayout(SelectCityActivity.this) ll.setOrientation(0) ImageView logo = new ImageView(SelectCityActivity.this) ll.addView(logo) TextView textView = getTextView() textView.setText(getGroup(groupPosition).toString()) ll.addView(textView) return ll } @Override public boolean isChildSelectable(int groupPosition, int childPosition) { return true } @Override public boolean hasStableIds() { return true } } // Set the window displays a list of setListAdapter(adapter) getExpandableListView().setOnChildClickListener( new OnChildClickListener() { @Override public boolean onChildClick(ExpandableListView parent, View source, int groupPosition, int childPosition, long id) { // Get the corresponding Activity before starting the Activity Intent Intent intent = getIntent() intent.putExtra('city', cities[groupPosition][childPosition]) // set the result code that SelectActivity and set to return after the end of Activity SelectCityActivity.this.setResult(0, intent) // end SelectCityActivity. SelectCityActivity.this.finish() return false } }) } }
比較的単純な例で、クリックします 統合ダウンロードソースなし