当前位置: 代码迷 >> 综合 >> android studio radioGroup radiobutton使用listactivity演示
  详细解决方案

android studio radioGroup radiobutton使用listactivity演示

热度:51   发布时间:2024-01-14 12:25:01.0

七月,太热!

第一步:

新建activity extends listactivity:

public class newact727 extends ListActivity{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,radioGroup.class)));}@Override
    protected void onListItemClick(ListView l, View v, int position, long id) {ListCellData listCellData=adapter.getItem(position);listCellData.startActivity();super.onListItemClick(l, v, position, id);}
}
listcelldata类的代码如下:

public class ListCellData {private String controlName="";private Intent i=null;Context context;public ListCellData(Context context,String controlName,Intent i) {this.controlName = controlName;this.context=context;this.i=i;}public String getControlName() {return controlName;}public ListCellData(Context context) {this.context = context;}public Intent getI() {return i;}@Override
    public String toString() {return getControlName();}public void startActivity(){context.startActivity(getI());}
}
再新建一个radiogroup activity,同时创建对应的layout:

public class radioGroup extends AppCompatActivity {@Override
    protected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_radio_group);final RadioButton r1=(RadioButton)findViewById(R.id.radio1);findViewById(R.id.radiobtn).setOnClickListener(new View.OnClickListener() {@Override
            public void onClick(View view) {if (r1.isChecked()){new AlertDialog.Builder(radioGroup.this).setMessage("right").show();}}});}
}
注意:这里引用的是
import android.app.AlertDialog;
这是一个简单的初学者例子。

  相关解决方案