【问题标题】:How to add a Single choice list to a custom dialog in android?如何将单选列表添加到android中的自定义对话框?
【发布时间】:2012-03-13 15:21:12
【问题描述】:

我想在自定义对话框中添加一个选择列表我已经尝试过不同的类型,但我发现应用程序仅因空指针错误而崩溃。我发现它只能在 AlertDialog.Builder 格式中完成很多例子。请举个例子帮助我。谢谢。

【问题讨论】:

  • 简单的 android 对话框会自行关闭。为避免这种情况,请尝试覆盖对话框方法或使用 PreferenceActivity 和 PreferenceList 以及您的活动。

标签: android listview radio-button radiobuttonlist


【解决方案1】:

试试这个

String x_id;

final String[] arrayOfStrings = first.toArray(new String[first.size()]);

Dialog dialog = new Dialog(Conf_game.this);
dialog.setContentView(R.layout.dialogs);
dialog.setTitle("SELECT Item ");        

final ListView lst = (ListView) dialog.findViewById(R.id.dialog_list);

lst.setAdapter(new ArrayAdapter<String>(this,
        android.R.layout.simple_list_item_checked, android.R.id.text1,
        arrayOfStrings));

lst.setOnItemClickListener(new OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> arg0, View arg1,
            int item, long arg3) {

        coursetext.setText(arrayOfStrings[item]);

        x_id = arrayOfStrings [item];

        System.out.println(" Value of ID OF ABC OF"
                + arrayOfStrings[item] + "    IS    " + C_id);
        dialog.dismiss();
    }
});

dialog.show();

【讨论】:

  • 我没有得到选中或未选中单选按钮的状态,它仍然未选中。如何解决这个 Manku。
  • 整数计数器; if (radiobutton1.isChecked()) { counter= counter++; Log.v("的值", "计数器是"+counter); } }
  • 但是对于列表,我们没有按照上面的代码单独使用单选按钮。
  • 我只使用单选列表,我采用了与上面相同的方法,但我不确定为什么会这样
【解决方案2】:

APIDemos 应用程序为此提供了一个示例。您可以从中获得帮助。

【讨论】:

    【解决方案3】:

    创建自定义对话框的步骤:

    创建对话框布局文件,如:

    <RelativeLayout xmlns:android = "http://schemas.android.com/apk/res/android" 
          android:layout_width = "wrap_content" 
          android:layout_height = "wrap_content"> 
          <!-- The Title Bar -->
          <LinearLayout 
              android:id = "@+id/ dlg_priority_titlebar" 
              android:orientation = "horizontal" 
              android:layout_width = "fill_parent" 
              android:layout_height = "wrap_content" 
              android:layout_alignParentTop = "true"> 
              <ImageView 
                android:src = "@drawable/image" 
                android:layout_width = "wrap_content" 
                android:layout_height = "wrap_content" 
                android:layout_margin the "5dip" /> 
            <TextView 
                android:layout_width = "wrap_content 
                android:layout_height = "wrap_content" 
                android:text = "Select Task Priority" 
                android:layout_gravity = "center_vertical" /> 
          </LinearLayout> 
          <ListView 
              android:id = "@+id/dlg_priority_lvw " 
              android:layout_width = "wrap_content" 
              android:layout_height = "wrap_content" 
              android:layout_below = "@+id/dlg_priority_titlebar" 
              the android:background = "@drawable/layout_home_bg"> 
          </ListView>      
    </RelativeLayout>
    

    由于ListView中的布局是自定义的,所以要为ListView创建一个布局文件:

    <LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android" 
        android:orientation = "horizontal" 
        android:layout_width = "fill_parent" 
        android:layout_height = "fill_parent"> 
    
    <ImageView 
          android:id = "@+id/list_priority_img" 
          android:layout_width = "wrap_content" 
          android:layout_height = "wrap_content" 
          android:layout_gravity = "center_vertical" 
          android:layout_margin = "5dip" /> 
    <TextView 
         android:id = "@+id/list_priority_value" 
         android:layout_width = "wrap_content" 
         android:layout_height = "wrap_content" 
         android:layout_gravity = "center_vertical" 
         android:textsize = "28dip" 
         android:textColor = "@drawable/ black" /> 
    </LinearLayout>
    

    创建一个继承自Dialog的自定义Dialog类PriorityDlg

    public class PriorityDlg extends Dialog {
    
    private Context context;
    private ListView dlg_priority_lvw = null;
    public PriorityDlg(Context context) {
        super(context);
        this.context = context;
        // TODO Auto-generated constructor stub
    }
    public PriorityDlg(Context context, int theme) {
        super(context, theme);
        this.context = context;
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        this.setContentView(R.layout.dlg_priority);
        dlg_priority_lvw = (ListView) findViewById(R.id.dlg_priority_lvw);
        // ListView
        SimpleAdapter adapter = new SimpleAdapter(context, getPriorityList(),
                R.layout.lvw_priority, new String[] { "list_priority_img",
                        "list_priority_value" }, new int[] {
                        R.id.list_priority_img, R.id.list_priority_value });
        dlg_priority_lvw.setAdapter(adapter);
        //ListView
        dlg_priority_lvw
                .setOnItemClickListener(new AdapterView.OnItemClickListener() {
    
                    @Override
                    public void onItemClick(AdapterView<?> arg0, View arg1,
                            int arg2, long arg3) {
    
                    }
                });
    }
    private List<HashMap<String, Object>> getPriorityList() {
        List<HashMap<String, Object>> priorityList = new ArrayList<HashMap<String, Object>>();
        HashMap<String, Object> map1 = new HashMap<String, Object>();
        map1.put("list_priority_img", R.drawable.priority_not_important);
        map1.put("list_priority_value", context.getResources().getString(
                R.string.dlg_priority_not_important));
        priorityList.add(map1);
        HashMap<String, Object> map2 = new HashMap<String, Object>();
        map2.put("list_priority_img", R.drawable.priority_general);
        map2.put("list_priority_value", context.getResources().getString(
                R.string.dlg_priority_general));
        priorityList.add(map2);
        HashMap<String, Object> map3 = new HashMap<String, Object>();
        map3.put("list_priority_img", R.drawable.priority_important);
        map3.put("list_priority_value", context.getResources().getString(
                R.string.dlg_priority_important));
        priorityList.add(map3);
        HashMap<String, Object> map4 = new HashMap<String, Object>();
        map4.put("list_priority_img", R.drawable.priority_very_important);
        map4.put("list_priority_value", context.getResources().getString(
                R.string.dlg_priority_very_important));
        priorityList.add(map4);
    
        return priorityList;
    }
    }
    

    创建自定义对话框

    PriorityDlg dlg = new PriorityDlg (SimpleTaskActivity.this, R.style.dlg_priority); 
    dlg.show();
    

    其中R.style.dlg_priority设置对话框使用的样式文件,只是让对话框去掉标题栏,当然你也可以通过代码来完成这个效果:

    <? Xml version = "1.0" encoding = "utf-8"?> 
    <resources> 
        <style name="dlg_priority" parent="@android:Theme.Dialog"> 
            <item name = "android: windowNoTitle"> true </ item> 
        </ style> 
    </ resources>
    

    【讨论】:

      猜你喜欢
      • 2012-11-08
      • 2011-07-12
      • 1970-01-01
      • 2018-09-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多