Mike 发表于 2021-8-4 15:34:13

Android alertDialog

package com.example.dialogue;


import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.Menu;
import android.widget.Toast;


public class MainActivity extends Activity {


@Override
public void onCreate(Bundle savedInstanceState) {


super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);


final String[] arrayFruit = new String[] { "苹果", "橘子", "草莓", "香蕉" };
final boolean[] arrayFruitSelected = new boolean[] { true, true, false,
false };


Dialog alertDialog = new AlertDialog.Builder(this)
.setTitle("你喜欢吃哪种水果?")
.setIcon(R.drawable.ic_launcher)
.setMultiChoiceItems(arrayFruit, arrayFruitSelected,
new DialogInterface.OnMultiChoiceClickListener() {


@Override
public void onClick(DialogInterface dialog,
int which, boolean isChecked) {
arrayFruitSelected = isChecked;
}
})
.setPositiveButton("确认", new DialogInterface.OnClickListener() {


@Override
public void onClick(DialogInterface dialog, int which) {
StringBuilder stringBuilder = new StringBuilder();
for (int i = 0; i < arrayFruitSelected.length; i++) {
if (arrayFruitSelected == true) {
stringBuilder.append(arrayFruit + "、");
}
}
Toast.makeText(MainActivity.this,
stringBuilder.toString(), Toast.LENGTH_SHORT)
.show();
}
})
.setNegativeButton("取消", new DialogInterface.OnClickListener() {


@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}
}).create();
alertDialog.show();
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}


}


文档来源:51CTO技术博客https://blog.51cto.com/u_15322177/3266604
页: [1]
查看完整版本: Android alertDialog