【问题标题】:Android: display multiple selections from a dialog box in a listAndroid:从列表中的对话框中显示多个选择
【发布时间】:2018-02-26 01:45:09
【问题描述】:

我想要实现的是从警报对话框中选择多个项目(我已经设法做到了),然后在列表视图中显示这些选择。

我尝试了一些不同的想法,但由于我的经验有限,我正在苦苦挣扎。

如果有人可以提供帮助,将不胜感激,在此先感谢。

这是我的 .xml

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

    <Button
    android:id="@+id/btn"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:text="Tone"
    android:layout_centerHorizontal="true"/>

    <TextView
    android:id="@+id/tv"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@id/btn"
    android:textSize="20dp"/>

</RelativeLayout>

这是我的.java

import android.content.DialogInterface;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import java.util.Arrays;
import java.util.List;

public class MainActivity extends AppCompatActivity {

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

        Button btn = findViewById(R.id.btn);
        final TextView tv = findViewById(R.id.tv);

        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                // Build an AlertDialog
                AlertDialog.Builder builder = new alertDialog.Builder(MainActivity.this);

                // String array for alert dialog multi choice items
                final String[] tone = new String[]{
                        "Red",
                        "Yellow",
                        "Green",
                        "Purple",
                        "Pink"
                };

                final boolean [] checkedTones = new boolean[]{
                        false,  // Red
                        false,  // Yellow
                        false,  // Green
                        false,  // Purple
                        false   // Pink
                };

                final List<String> toneList = Arrays.asList(tone);

                builder.setMultiChoiceItems(tone, checkedTones, new DialogInterface.OnMultiChoiceClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int which, boolean isChecked) {

                        // Update the current focused item's checked status
                        checkedTones[which] = isChecked;

                        // Get the current focused item
                        String currentItem = toneList.get(which);

                        // Notify the current action
                        Toast.makeText(getApplicationContext(), tone[which], Toast.LENGTH_SHORT).show();
                    }
                });

                // Specify the dialog is not cancelable
                builder.setCancelable(false);

                // Set a title for alert dialog
                builder.setTitle("Your Tone");

                // Set the positive/yes button click listener
                builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        // Do something when click positive button
                        tv.setText("Your Tone\n");
                        for (int i = 0; i<checkedTones.length; i++){
                            boolean checked = checkedTones[i];
                            if (checked) {
                                tv.setText(tv.getText() + toneList.get(i) + "\n");
                            }
                        }
                    }
                });

                // Set the negative/no button click listener
                builder.setNegativeButton("No", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        // Do something when click the negative button
                    }
                });

                // Set the neutral/cancel button click listener
                builder.setNeutralButton("Cancel", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        // Do something when click the neutral button
                    }
                });

                AlertDialog dialog = builder.create();
                // Display the alert dialog on interface
                dialog.show();
            }
        });
    }
}

【问题讨论】:

  • 谢谢,这就是我想要实现的目标。当单击肯定按钮时,我尝试为列表视图复制该代码,但是我没有任何运气。我到底需要改变什么才能让它工作。

标签: android listview android-studio android-alertdialog


【解决方案1】:

您可以使用自定义对话框与您自己的 ui 界面和逻辑示例在这里https://www.mkyong.com/android/android-custom-dialog-example/。或者您可以使用重量轻且可在任何地方重复使用的 Dialog 片段。例子在这里https://medium.com/@xabaras/creating-a-custom-dialog-with-dialogfragment-f0198dab656d

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-10
    • 2011-08-05
    • 1970-01-01
    相关资源
    最近更新 更多