android - 如何在对话框listView中添加editText和icon?

标签 android listview dialog

我试图使对话框看起来像下图,其中listView包含图标和editText

enter image description here

但是,我只能分别使用 listVieweditText 创建一个对话框。我怎样才能将它们组合在一个对话框中?

对话框 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);
        }
    }

自定义对话框

<?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

最佳答案

首先为每个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() 方法中扩展自定义布局:

你的适配器类

 ...
 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;
    }

然后在您的dialog 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);
        }
    }

关于android - 如何在对话框listView中添加editText和icon?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39560332/

相关文章:

android - 获取包括操作栏的窗口 View ?

java - 如何将 OnLongClickListener 设置为 ListView 中的按钮?

java - 使用 UIBinder 创建一个进入对话框的 Widget

Android - 将参数从 AlertDialog 传递到 AlertDialog'host

Android 6.0 Marshmallow (API 23) 中弃用了 AndroidHttpClient 和 HttpGet API

android - 如何在Android平台打开 "front camera"?

android - 为什么MediaPlayer失败?

android - 可扩展 ListView 项目高度,其中包含另一个 ListView

android - 如何工作,当 ListView 在 ScrollView 中时?

jquerydialog() - 只捕获紧挨着开场白的div