如何每个项目之前警告对话框中添加图标?图标、项目、对话框中

2023-09-04 02:55:13 作者:側臉眉依淺

我使用警告对话框,code以下,现在我希望把图像的每个文本之前

例如电子邮件图标,然后文本电子邮件, Facebook的图标,然后点击文本的Facebook等。在警告对话框

使用下面的code请指导如何给每个文本值前添加图标?

 最后的CharSequence []项目= {电子邮件,
                脸谱,推特,LinkedIn};
        AlertDialog.Builder建设者=新AlertDialog.Builder(More.this);
        builder.setTitle(「股份机应用);
        builder.setItems(项目,新DialogInterface.OnClickListener(){
            公共无效的onClick(DialogInterface对话,诠释项){



                如果(项目== 0){


                }否则,如果(项目== 1)
                {





                }否则,如果(项目== 2)
                {


                }
                否则,如果(项目== 3)
                {




                }


            }
        });
        AlertDialog警报= builder.create();
        alert.show();
 

解决方案 将选中的阿里图标添加至项目中

您必须使用自定义ListAdapter添加你的形象。在方式是继承一个ArrayAdapter(默认使用的AlertDialog)。下面是一个例子:

 最后一个项目[]项目= {
    新的项目(电子邮件,android.R.drawable.ic_menu_add)
    新的项目(脸谱,android.R.drawable.ic_menu_delete)
    新的项目(...,0)//没有这一个图标
};

ListAdapter适配器=新的ArrayAdapter<项目>(
    本,
    android.R.layout.select_dialog_item,
    android.R.id.text1,
    项目){
        公共查看getView(INT位置,查看convertView,ViewGroup中父){
            //使用超类来创建视图
            视图V = super.getView(位置,convertView,父母);
            TextView的电视=(TextView中)v.findViewById(android.R.id.text1);

            //把图像上的TextView
            tv.setCompoundDrawablesWithIntrinsicBounds(项目[位置] .icon,0,0,0);

            //添加保证金图像与文字之间(支持各种屏幕密度)
            INT DP5 =(INT)(5 * getResources()getDisplayMetrics()密度+ 0.5F);
            tv.setCompoundDrawablePadding(DP5);

            返回伏;
        }
    };


新AlertDialog.Builder(本)
    .setTitle(「股份机应用)
    .setAdapter(适配器,新DialogInterface.OnClickListener(){
        公共无效的onClick(DialogInterface对话,诠释项){
            // ...
        }
    })。显示();
 

下面是Item类

 公共静态类项目{
    公众最终文本字符串;
    公众最终诠释图标;
    公共项目(字符串文本,整数图标){
        this.text =文本;
        this.icon =图标;
    }
    @覆盖
    公共字符串的toString(){
        返回文本;
    }
}
 

i am using alert dialog with code below now i want to put image before each text

for example email icon then text Email, facebook icon then text Facebook etc.. in alert dialog

using following code please guide how to add icon before each text value?

final CharSequence[] items = { "Email",
                "Facebook","Twitter","LinkedIn"};
        AlertDialog.Builder builder = new AlertDialog.Builder(More.this);
        builder.setTitle("Share Appliction");
        builder.setItems(items, new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int item) {



                if (item == 0) {


                } else if (item == 1) 
                {





                }else if(item == 2)
                {


                } 
                else if(item == 3)
                {




                }


            }
        });
        AlertDialog alert = builder.create();
        alert.show();

解决方案

You must use a custom ListAdapter to add your image. On way is to subclass the ArrayAdapter (used by default by the AlertDialog). Here is an example:

final Item[] items = {
    new Item("Email", android.R.drawable.ic_menu_add),
    new Item("Facebook", android.R.drawable.ic_menu_delete),
    new Item("...", 0),//no icon for this one
};

ListAdapter adapter = new ArrayAdapter<Item>(
    this,
    android.R.layout.select_dialog_item,
    android.R.id.text1,
    items){
        public View getView(int position, View convertView, ViewGroup parent) {
            //Use super class to create the View
            View v = super.getView(position, convertView, parent);
            TextView tv = (TextView)v.findViewById(android.R.id.text1);

            //Put the image on the TextView
            tv.setCompoundDrawablesWithIntrinsicBounds(items[position].icon, 0, 0, 0);

            //Add margin between image and text (support various screen densities)
            int dp5 = (int) (5 * getResources().getDisplayMetrics().density + 0.5f);
            tv.setCompoundDrawablePadding(dp5);

            return v;
        }
    };


new AlertDialog.Builder(this)
    .setTitle("Share Appliction")
    .setAdapter(adapter, new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int item) {
            //...
        }
    }).show();

Here is the Item class

public static class Item{
    public final String text;
    public final int icon;
    public Item(String text, Integer icon) {
        this.text = text;
        this.icon = icon;
    }
    @Override
    public String toString() {
        return text;
    }
}

 
精彩推荐
图片推荐