我希望能够实现的是从警报对话框中选择多个项目(我已设法做到),然后在列表视图中显示这些选择。
我尝试了一些不同的想法,但由于我的经验有限,我正在努力。
如果有人能提供帮助,我们将非常感谢,提前谢谢。
这是我的.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();
}
});
}
}
答案 0 :(得分:0)
您可以使用自己的ui界面使用自定义对话框,逻辑示例在https://www.mkyong.com/android/android-custom-dialog-example/。或者您可以使用重量轻且可重复使用的Dialog片段。示例在这里https://medium.com/@xabaras/creating-a-custom-dialog-with-dialogfragment-f0198dab656d