Android 使用按钮和对话框复选框的多选项目,并在列表视图中显示所选项目而不是按钮

Posted

技术标签:

【中文标题】Android 使用按钮和对话框复选框的多选项目,并在列表视图中显示所选项目而不是按钮【英文标题】:Android Multiple select item using button and dialog checkbox and display the selected item in listview instead of button 【发布时间】:2016-02-07 17:17:46 【问题描述】:

我正在尝试使用按钮和对话框复选框进行 android 多选项目,并在列表视图中显示所选项目(如图所示)而不是按钮(当前显示在按钮中:请参考图片)

我可以在下面的按钮中显示接收者,但我希望在列表视图中显示相同的内容:

从对话框复选框中选择后当前显示:

"Receiver1, Receiver2"

我想要喜欢:

image of Receiver1 with Receiver1 Name
image of Receiver2 with Receiver2 Name

SendServiceActivity.java

import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.text.Layout;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;

import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Environment;
import android.provider.MediaStore;
import android.util.Log;
import android.widget.ImageView;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;

public class SendServiceActivity extends AppCompatActivity implements View.OnClickListener
protected Button selectReceiversBtn;
    protected CharSequence[] receivers = 
            "Receiver1", "Receiver2", "Receivers3";
    protected ArrayList<CharSequence> selectedReceivers = new ArrayList<>();

    protected void onCreate(Bundle savedInstanceState) 
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main); 

        selectReceiversBtn = (Button) findViewById(R.id.select_receivers);
        selectReceiversBtn.setOnClickListener((View.OnClickListener) this);
    

    @Override
    public void onClick(View view) 
        switch(view.getId()) 
            case R.id.select_receivers:
                showSelectReceiversDialog();
                break;
            default:
                break;
        
       

    protected void onChangeSelectedReceivers() 
        StringBuilder stringBuilder = new StringBuilder();

        for(CharSequence receivers : selectedReceivers)
            stringBuilder.append(receivers + ",");
        selectReceiversBtn.setText(stringBuilder.toString());
    

    protected void showSelectReceiversDialog() 
        boolean[] checkedReceivers = new boolean[receivers.length];
        int count = receivers.length;

        for(int i = 0; i < count; i++)
            checkedReceivers[i] = selectedReceivers.contains(receivers[i]);

        DialogInterface.OnMultiChoiceClickListener receiversDialogListener = new DialogInterface.OnMultiChoiceClickListener() 
            @Override
            public void onClick(DialogInterface dialog, int which, boolean isChecked) 
                if(isChecked)
                    selectedReceivers.add(receivers[which]);
                else
                    selectedReceivers.remove(receivers[which]);

                onChangeSelectedReceivers();
            
        ;

        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder
                .setTitle("Select Receivers")
                .setMultiChoiceItems(receivers, checkedReceivers, receiversDialogListener)
                .setCancelable(false)
                .setPositiveButton("OK", new DialogInterface.OnClickListener() 
                    public void onClick(DialogInterface dialog, int id) 
                        dialog.dismiss();
                    
                );
        AlertDialog dialog = builder.create();
        dialog.show();
    

activity_main.xml

            <LinearLayout
            android:layout_
            android:layout_
            android:orientation="vertical">

            <TextView
                android:id="@+id/textView1"
                android:layout_
                android:layout_
                android:padding="5dip"
                android:text="Select Receivers" />

            <Button
                android:id="@+id/select_receivers"
                android:layout_
                android:layout_
                android:padding="5dip"
                android:text="- None Selected -" />


        </LinearLayout>

【问题讨论】:

【参考方案1】:

您的要求的完整源代码如下:

mainactivity.class

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.text.Layout;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Environment;
import android.provider.MediaStore;
import android.util.Log;
import android.widget.ImageView;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;

public class MainActivity extends Activity implements View.OnClickListener
    ArrayAdapter<CharSequence> adapter;
    ListView listView1;
    protected Button selectReceiversBtn;
    protected CharSequence[] receivers = 
            "Receiver1", "Receiver2", "Receivers3";
    protected ArrayList<CharSequence> selectedReceivers = new ArrayList<>();

    protected void onCreate(Bundle savedInstanceState) 
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main); 

        listView1=(ListView) findViewById(R.id.listView1);
        selectReceiversBtn = (Button) findViewById(R.id.select_receivers);
        selectReceiversBtn.setOnClickListener((View.OnClickListener) this);


       // String[] your_array = "a","b","c"
        adapter = new ArrayAdapter<CharSequence>(this,
                android.R.layout.simple_list_item_multiple_choice, selectedReceivers);
        listView1.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
        listView1.setAdapter(adapter);
    

    @Override
    public void onClick(View view) 
        switch(view.getId()) 
            case R.id.select_receivers:
                showSelectReceiversDialog();
                break;
            default:
                break;
        
       

    protected void onChangeSelectedReceivers() 
        StringBuilder stringBuilder = new StringBuilder();

        for(CharSequence receivers : selectedReceivers)
            stringBuilder.append(receivers + ",");
        adapter.notifyDataSetChanged();
        //selectReceiversBtn.setText(stringBuilder.toString());
    

    protected void showSelectReceiversDialog() 
        boolean[] checkedReceivers = new boolean[receivers.length];
        int count = receivers.length;

        for(int i = 0; i < count; i++)
            checkedReceivers[i] = selectedReceivers.contains(receivers[i]);

        DialogInterface.OnMultiChoiceClickListener receiversDialogListener = new DialogInterface.OnMultiChoiceClickListener() 
            @Override
            public void onClick(DialogInterface dialog, int which, boolean isChecked) 
                if(isChecked)
                    selectedReceivers.add(receivers[which]);
                else
                    selectedReceivers.remove(receivers[which]);

                onChangeSelectedReceivers();
            
        ;

        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder
                .setTitle("Select Receivers")
                .setMultiChoiceItems(receivers, checkedReceivers, receiversDialogListener)
                .setCancelable(false)
                .setPositiveButton("OK", new DialogInterface.OnClickListener() 
                    public void onClick(DialogInterface dialog, int id) 
                        dialog.dismiss();

                    
                );
        AlertDialog dialog = builder.create();
        dialog.show();
    

activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_
    android:layout_
    android:orientation="vertical" >

    <TextView
        android:id="@+id/textView1"
        android:layout_
        android:layout_
        android:padding="5dip"
        android:text="Select Receivers" />

    <Button
        android:id="@+id/select_receivers"
        android:layout_
        android:layout_
        android:padding="5dip"
        android:text="- None Selected -" />

    <ListView
        android:id="@+id/listView1"
        android:layout_
        android:layout_ >
    </ListView>

</LinearLayout>

复制并粘贴上面的代码..当您签入对话框时,它会自动添加到列表视图中

【讨论】:

【参考方案2】:

以下代码适合您:

String[] your_array = "a","b","c"
        adapter = new ArrayAdapter<String>(this,
                android.R.layout.simple_list_item_multiple_choice, your_array);
        listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
        listView.setAdapter(adapter);

使用上面的代码,您可以在列表视图中选中或取消选中。

【讨论】:

@Ketan 我如何在我的代码中实现你的代码,因为 Java 中的新手所以问你。 adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_multiple_choice, 接收器); listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE); listView.setAdapter(适配器); 以上 3 行使用 instend of open dialog 在xml文件中使用listview 您可以使用以下链接中的示例。adanware.blogspot.in/2012/04/…

以上是关于Android 使用按钮和对话框复选框的多选项目,并在列表视图中显示所选项目而不是按钮的主要内容,如果未能解决你的问题,请参考以下文章

delphi中如何实现checkbox的多选

没有AlertDialog的android中的多选微调器

vue修改弹框的多选框会影响表格的内容

vue添加第一行为空的多选框

创建过滤数据表的多选下拉列表

js实现元素的多选,或单选功能