package com.example.uiconrrol; import android.app.ListActivity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.widget.ArrayAdapter; import android.widget.ListView; public class MainActivity extends ListActivity{ private ArrayAdapter<ListCellData> adapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); adapter = new ArrayAdapter<ListCellData>(this, android.R.layout.simple_list_item_1); setListAdapter(adapter); adapter.add(new ListCellData(this, "RadioGroup", new Intent(this, AtyUsingRadioGroup.class))); } @Override protected void onListItemClick(ListView l, View v, int position, long id) { ListCellData data = adapter.getItem(position); data.startActivity(); super.onListItemClick(l, v, position, id); } }
package com.example.uiconrrol; import android.content.Context; import android.content.Intent; public class ListCellData { public ListCellData(Context context, String controlName, Intent relatedIntent){ this.controlName = controlName; this.context = context; this.relatedIntent = relatedIntent; } private String controlName = ""; public String getControlName() { return controlName; } private Context context = null; public Context getContext() { return context; } private Intent relatedIntent = null; public Intent getRelatedIntent() { return relatedIntent; } public void startActivity(){ getContext().startActivity(getRelatedIntent()); } //重写toString方法,只显示Name @Override public String toString() { return getControlName(); } }
package com.example.uiconrrol; import android.app.Activity; import android.app.AlertDialog; import android.os.Bundle; import android.view.View; import android.widget.RadioButton; public class AtyUsingRadioGroup extends Activity { private RadioButton radioRight; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.aty_using_radiogroup); radioRight = (RadioButton) findViewById(R.id.radioRight); findViewById(R.id.btnSubmit).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if(radioRight.isChecked()){ new AlertDialog.Builder(AtyUsingRadioGroup.this).setTitle("判断").setMessage("回答正确").setPositiveButton("Cancel", null).show(); }else{ new AlertDialog.Builder(AtyUsingRadioGroup.this).setTitle("判断").setMessage("回答错误").setPositiveButton("Cancel", null).show(); } } }); } }
<?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:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="老师是个帅哥吗?" /> <RadioGroup android:id="@+id/radioGroup1" android:layout_width="wrap_content" android:layout_height="wrap_content" > <RadioButton android:id="@+id/radioRight" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="是" /> <RadioButton android:id="@+id/radioNo" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="不是" /> <RadioButton android:id="@+id/radio2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="不知道" /> </RadioGroup> <Button android:id="@+id/btnSubmit" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="提交" /> </LinearLayout>
时间: 2024-10-11 09:21:49