//MainActivity切换进入chooseActivity
public class MainActivity extends Activity {
private Button cityBtn,registerBtn;
private EditText city,psd,psd2,name;
private Intent intent;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
cityBtn=(Button)findViewById(R.id.cityBtn);
registerBtn=(Button)findViewById(R.id.registerBtn);
name=(EditText)findViewById(R.id.name);
city=(EditText)findViewById(R.id.city);
psd=(EditText)findViewById(R.id.psd);
psd2=(EditText)findViewById(R.id.psd2);
cityBtn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent intent=new Intent(MainActivity.this,chooseCityActivity.class); //此处打开chooseAcitivity悬着界面
//startActivity(intent);
startActivityForResult(intent, 0); //从mainAcitivity中进入chooseActivity界面
}
});
registerBtn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
}
});
//onActivityResult(requestCode, resultCode, data)
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent intent) { //从chooseActivity中传回的数据参数intent中获取
// TODO Auto-generated method stub
if(requestCode==0 && resultCode==1){
Bundle data=intent.getExtras();
String cityStr=data.getString("city");
String provinStr=data.getString("province");
city.setText(provinStr+"."+cityStr);
}
super.onActivityResult(requestCode, resultCode, intent);
}
@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;
}
}
//chooseActivity回传数据 数据列表类
public class chooseCityActivity extends ExpandableListActivity {
private String[] provinces=new String[] {"江西","江苏","浙江"};
private String[][] cities=new String[][] {{"南昌","九江","赣州","吉安"},{"南京","苏州","无锡","扬州"},{"杭州","温州","台州","金华"}};
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
ExpandableListAdapter adapter=new BaseExpandableListAdapter() {
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
// TODO Auto-generated method stub
return true;
}
private TextView getTextView(){
AbsListView.LayoutParams lp=new AbsListView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,64);
TextView textView=new TextView(chooseCityActivity.this);
textView.setLayoutParams(lp);
textView.setGravity(Gravity.CENTER_VERTICAL|Gravity.LEFT);
textView.setPadding(36, 0, 0, 0);
textView.setTextSize(20);
return textView;
}
@Override
public boolean hasStableIds() {
// TODO Auto-generated method stub
return false;
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
LinearLayout ll=new LinearLayout(chooseCityActivity.this);
ll.setOrientation(LinearLayout.VERTICAL);
ImageView logo=new ImageView(chooseCityActivity.this);
ll.addView(logo);
TextView textView=getTextView();
textView.setText(getGroup(groupPosition).toString());
ll.addView(textView);
return ll;
}
@Override
public long getGroupId(int groupPosition) {
// TODO Auto-generated method stub
return groupPosition;
}
@Override
public int getGroupCount() {
// TODO Auto-generated method stub
return provinces.length;
}
@Override
public Object getGroup(int groupPosition) {
// TODO Auto-generated method stub
return provinces[groupPosition];
}
@Override
public int getChildrenCount(int groupPosition) {
// TODO Auto-generated method stub
return cities[groupPosition].length;
}
@Override
public View getChildView(int groupPosition, int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
TextView textView=getTextView();
textView.setText(getChild(groupPosition,childPosition).toString());
//System.out.println(textView.toString());
// TODO Auto-generated method stub
return textView;
}
@Override
public long getChildId(int groupPosition, int childPosition) {
// TODO Auto-generated method stub
return childPosition;
}
@Override
public Object getChild(int groupPosition, int childPosition) {
// TODO Auto-generated method stub
return cities[groupPosition][childPosition];
}
};
setListAdapter(adapter);
getExpandableListView().setOnChildClickListener(new OnChildClickListener() {
public boolean onChildClick(ExpandableListView parent, View v,
int groupPosition, int childPosition, long id) {
//Intent intent=getIntent();
Intent intent=new Intent();
Bundle data=new Bundle();
data.putString("province", provinces[groupPosition]); //回传的数据1 key-value
data.putString("city", cities[groupPosition][childPosition]); //回传的数据2
intent.putExtras(data);
chooseCityActivity.this.setResult(1,intent);
//startActivity(intent);
chooseCityActivity.this.finish(); //关闭当前的窗口
return false;
}
});
}
}