15/8/27/預設和自定義Toast/簡單、多選項、單選、多選AlterDialog
阿新 • • 發佈:2019-02-04
Toast
Toast分為預設的和自定義佈局的Toast
1.Toast是獨立, 不依賴於Activity,但是不能對他進行操作,因此通常用於提示資訊,
2.預設的Toast比較簡單其程式碼如下:
Toast toast=Toast.makeText(getApplicationContext(),"恭喜你中獎了",Toast.LENGTH_LONG);
Spanned spanned=Html.fromHtml("我是<img src=''><font color='#ff0000'>暴擊</font>", new Html.ImageGetter () {
@Override
public Drawable getDrawable(String source) {
Drawable drawable = getResources().getDrawable(R.mipmap.ic_launcher);
drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
return drawable;
}
}, null);
toast.setText(spanned);
toast.setDuration(Toast.LENGTH_SHORT);
toast.setGravity(Gravity.CENTER,0,0);
toast.show();
3.上面是Spanner是富文字
4.自定義佈局的Toast需要自定義一個佈局,並且需要通過呼叫inflate方法來找到該佈局設定成為View
5.自定義Toast的程式碼如下:
oast toast1=new Toast(getApplicationContext());
LayoutInflater inflater=getLayoutInflater();
View toastView=inflater.inflate(R.layout.toast_zidingyi,null);
TextView textView= (TextView) toastView.findViewById(R.id.textview_toast1);
textView.setText("彈出自定義Toast標題");
ImageView imageView= (ImageView) toastView.findViewById(R.id.imageview_toast);
imageView.setImageResource(R.mipmap.ic_launcher);
TextView textView1= (TextView) toastView.findViewById(R.id.textview_toast2);
textView1.setText("彈出自定義Toast結尾");
toast1.setView(toastView);
toast1.setDuration(Toast.LENGTH_SHORT);
toast1.show();
6.最重要的一點是最後toast一定要呼叫show方法,這樣才能進行顯示
AlterDialog有多種方式
最重要的一點最後一定要呼叫creat方法和show方法
1.最簡單的AlterDialog實現的程式碼如下:
AlertDialog.Builder builder=new AlertDialog.Builder(MainActivity.this);
builder.setIcon(R.mipmap.ic_launcher).setTitle("恭喜").setMessage("恭喜你獲得安卓玩偶一個,點選確定獲取").setNegativeButton("NegativeButton",new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(getApplicationContext(), "點選了NegativeButton", Toast.LENGTH_SHORT).show();
}
}).setNeutralButton("NeutralButton",new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(getApplicationContext(), "點選了NeutralButton", Toast.LENGTH_SHORT).show();
}
}).setPositiveButton("PositiveButton",new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(getApplicationContext(), "點選了PositiveButton", Toast.LENGTH_SHORT).show();
}
});
AlertDialog dialog=builder.create();
dialog.show();
2.多選項的AlterDialog
private String[] mData={"Json","Kasi","Luxi","Major"};
AlertDialog.Builder builder1=new AlertDialog.Builder(MainActivity.this);
builder1.setIcon(R.mipmap.ic_launcher);
builder1.setTitle("標題");
builder1.setItems(mData, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(getApplicationContext(), "支援了第" + which + "位", Toast.LENGTH_SHORT).show();
}
});
AlertDialog dialog1=builder1.create();
dialog1.show();
3.只能單選的AlterDialog
private String[] mSections={"Top","Mid","ADC","Sub","Ye"};
private String mSection;
AlertDialog.Builder builder=new AlertDialog.Builder(MainActivity.this);
builder.setIcon(R.mipmap.ic_launcher);
builder.setTitle("標題");
builder.setSingleChoiceItems(mSections,0, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
mSection=mSections[which];
Toast.makeText(getApplicationContext(), "選擇的位置是:" + mSections[which], Toast.LENGTH_SHORT).show();
}
});
builder.setNeutralButton("確定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
mButton3.setText(mSection);
}
});
builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
AlertDialog dialog=builder.create();
dialog.show();
4.多選AlterDialog
private String[] mFavoer={"薇恩","伊澤瑞爾","凱特琳","希維爾","格雷福斯","庫奇","圖奇"};
private boolean[] mMangerFavoer=new boolean[mFavoer.length];
private StringBuffer favoer;
AlertDialog.Builder builder=new AlertDialog.Builder(MainActivity.this);
builder.setIcon(R.mipmap.ic_launcher);
builder.setTitle("選擇你最喜歡的英雄");
builder.setMultiChoiceItems(mFavoer, mMangerFavoer, new DialogInterface.OnMultiChoiceClickListener() {
@Override
public void onClick(DialogInterface dialog, int which, boolean isChecked) {
mMangerFavoer[which]=isChecked;
}
});
builder.setNeutralButton("確定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
favoer=new StringBuffer();
for(int i=0;i<mMangerFavoer.length;i++){
if(mMangerFavoer[i]){
favoer.append(mFavoer[i]);
}
}
mButton4.setText("ADC:"+favoer);
}
});
builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
AlertDialog dialog=builder.create();
dialog.show();