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.Toast;
public class Main4Activity extends AppCompatActivity {
private Button dialog;
AlertDialog.OnClickListener clickListener=new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
switch (which){
case DialogInterface.BUTTON_NEGATIVE:
Toast.makeText(Main4Activity.this, "不开心", Toast.LENGTH_SHORT).show();
break;
case DialogInterface.BUTTON_POSITIVE:
Toast.makeText(Main4Activity.this, "开心", Toast.LENGTH_SHORT).show();
break;
case DialogInterface.BUTTON_NEUTRAL:
Toast.makeText(Main4Activity.this, "一般", Toast.LENGTH_SHORT).show();
break;
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main4);
dialog= (Button) findViewById(R.id.alert);
dialog.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
AlertDialog.Builder builder=new AlertDialog.Builder(Main4Activity.this);
builder.setTitle("心情")
.setMessage("你今天开心么?")
.setNegativeButton("不开心",clickListener)
.setPositiveButton("开心",clickListener)
.setNeutralButton("一般",clickListener);
builder.show();
}
});
}
}
方法一:编写样式文件
//2.通过java代码去修改样式
//保证dialog已经可以成功显示
AlertDialog ad = builder.create();
ad.show();
Window w = ad.getWindow();//先去获得dialog所在的窗体
WindowManager wm = w.getWindowManager();//取得窗体的管理权限
Display d = wm.getDefaultDisplay();//得到手机屏幕窗体的宽和高
WindowManager.LayoutParams lp = w.getAttributes();//获得dialog的宽和高
lp.height = (int) (d.getHeight()*0.6);
lp.gravity = Gravity.BOTTOM;
w.setAttributes(lp);
方法三:自己编写页面布局来弹出
builder.setView(R.layout.a);
builder.show();
java代码如下:
private Button select;
private String[] positives={"好看","好看","真好看"};
select= (Button) findViewById(R.id.select);
select.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
AlertDialog.Builder builder=new AlertDialog.Builder(Main4Activity.this);
builder.setTitle("优点呢?")
.setItems(positives, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(Main4Activity.this, positives[which], Toast.LENGTH_SHORT).show();
}
});
builder.show();
}
});
java代码如下:
private Button radio;
radio= (Button) findViewById(R.id.radio);
radio.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
AlertDialog.Builder builder=new AlertDialog.Builder(Main4Activity.this);
builder.setTitle("只能选一个哟~")
.setSingleChoiceItems(positives, 1, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(Main4Activity.this, positives[which], Toast.LENGTH_SHORT).show();
}
});
builder.show();
}
});
java代码如下:
private Button ck;
private String[] positives={"好看","好看","真好看"};
ck= (Button) findViewById(R.id.ck);
ck.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
AlertDialog.Builder builder=new AlertDialog.Builder(Main4Activity.this);
builder.setTitle("可以选很多个哟~~")
.setMultiChoiceItems(positives, new boolean[]{true, false, true}, new DialogInterface.OnMultiChoiceClickListener() {
@Override
public void onClick(DialogInterface dialog, int which, boolean isChecked) {
Toast.makeText(Main4Activity.this, positives[which], Toast.LENGTH_SHORT).show();
}
});
builder.show();
}
});