如何在对话框listView中添加editText和图标?

时间:2016-09-18 17:01:28

标签: android listview dialog

我尝试在下方显示dialog图片,listView包含图标和editText

enter image description here

但是,我只能与listView进行对话,并与editText单独进行对话。如何在一个对话框中合并

对话框listView的代码

 @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.action_add_task:
                String names[] ={"A","B","C","D"};
                AlertDialog.Builder alertDialog = new AlertDialog.Builder(this);
                LayoutInflater inflater = getLayoutInflater();
                View convertView = (View) inflater.inflate(R.layout.custom_dialog, null);
                alertDialog.setView(convertView);
                ListView lv = (ListView) convertView.findViewById(R.id.listView1);
                ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,names);
                lv.setAdapter(adapter);
                alertDialog.show();

                return true;

            default:
                return super.onOptionsItemSelected(item);
        }
    }

CUSTOM_DIALOG

<?xml version="1.0" encoding="utf-8"?>
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/listView1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

</ListView>

快照

enter image description here

代码中包含EditText的代码

 @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.action_add_task:
                AlertDialog.Builder builder = new AlertDialog.Builder(this);
                LayoutInflater inflater = getLayoutInflater();
                builder.setView(inflater.inflate(R.layout.dialog_with_edittext,null))
                        // Add action buttons
                        .setPositiveButton(R.string.cancel, new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int id) {
                                dialog.cancel();
                            }
                        })
                        .setNegativeButton(R.string.save, new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int id) {

                            }
                        });
                builder.show();
                return true;

            default:
                return super.onOptionsItemSelected(item);
        }
    }

dialog_with_editText

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

    <EditText
        android:id="@+id/username"
        android:inputType="textEmailAddress"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="16dp"
        android:layout_marginLeft="4dp"
        android:layout_marginRight="4dp"
        android:layout_marginBottom="4dp"
        android:hint="Add new task" />

    <EditText
        android:id="@+id/password"
        android:inputType="textPassword"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="4dp"
        android:layout_marginLeft="4dp"
        android:layout_marginRight="4dp"
        android:layout_marginBottom="16dp"
        android:fontFamily="sans-serif"
        android:hint="Summary"/>
</LinearLayout>

快照 enter image description here

2 个答案:

答案 0 :(得分:2)

首先为每个listView项创建一个布局(editText和icon - imageView)

<强> custom_item_layout.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
<ImageView

    android:id="@+id/img"
    android:layout_alignParentStart="true"
    android:layout_width="50dp"
    android:layout_height="50dp"
    android:layout_alignParentLeft="true" />
    <EditText
        android:layout_marginStart="60dp"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:id="@+id/edtText"
        android:layout_alignEnd="@+id/img"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_alignRight="@+id/img"
        android:layout_marginLeft="60dp" />
</RelativeLayout>
        <EdidText
            android:id="@+id/edtText"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
    </LinearLayout>

然后创建一个扩展BaseAdapter的自定义适配器类,并在getView()方法中为自定义布局充气:

<强> YourAdapterClass

 ...
 private static class Holder {
        ImageView img;
        EditText edt;
    }

  @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        Holder holder = new Holder();

        if (convertView == null) {
            LayoutInflater inflater = LayoutInflater.from(context);
            convertView = inflater.inflate(R.layout.custom_item_layout, parent, false);


            holder.img = (ImageView) convertView.findViewById(R.id.img);
            holder.edt= (EditText) convertView.findViewById(R.id.edtText);

            convertView.setTag(holder);
        } else {
            holder = (Holder) convertView.getTag();
        }

        //do what you want with holder.img and holder.edt


        return convertView;
    }

然后在对话框listView

的代码中
 @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.action_add_task:
                String names[] ={"A","B","C","D"};
                AlertDialog.Builder alertDialog = new AlertDialog.Builder(this);
                LayoutInflater inflater = getLayoutInflater();
                View convertView = (View) inflater.inflate(R.layout.custom_dialog, null);
                alertDialog.setView(convertView);
                ListView lv = (ListView) convertView.findViewById(R.id.listView1);
                YourAdapterClass adapter = new YourAdapterClass(this,images,names); //whatever you want to put in
                lv.setAdapter(adapter);
                alertDialog.show();

                return true;

            default:
                return super.onOptionsItemSelected(item);
        }
    }

答案 1 :(得分:1)

如果您想在列表视图中添加图标,可以查看this