Toast與自定義Toast
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private Button mButton;
private Button mButton2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mButton = (Button) findViewById(R.id.button);
mButton2 = (Button) findViewById(R.id.button2);
mButton.setOnClickListener(this);
mButton2.setOnClickListener(this);
}
public void onClick(View v) {
switch (v.getId()) {
case R.id.button:
Toast toast = Toast.makeText(getApplicationContext(), "Clicked Button", Toast.LENGTH_LONG);
//富文本
Spanned spanned = Html.fromHtml("我是一個<img src=''/><font color='#55ff'>Toast</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.show();
toast.setGravity(Gravity.CENTER, 0, 0);//偏移距離
break;
case R.id.button2:
Toast toast2=new Toast(getApplicationContext());
LayoutInflater inflater=getLayoutInflater();
View toastView=inflater.inflate(R.layout.mytoast_layout,null);
toast2.setView(toastView);
toast2.setDuration(Toast.LENGTH_LONG);
toast2.show();
break;
default:
break;
}
}
}
自定義Toast
Dialog
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private Button mButton;
private Button mButton2;
private Button mButton3;
private Button mButton4;
private Button mButton5;
private Button mButton6;
private Button mButton7;
private Button mButton8;
private Button mButton9;
private Calendar mCalendar;
private Dialog mDialog;
private String[] mSex={"男","女","其他"};
private String sex;
private String[] array={"第一條數據","第二條數據","第三條數據"};
private String[] mHobby={"讀書","學習","寫字","看書"};
private boolean[] mIsCheckedHobby=new boolean[mHobby.length];
private StringBuffer hobby;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mButton= (Button) findViewById(R.id.button);
mButton2= (Button) findViewById(R.id.button2);
mButton3= (Button) findViewById(R.id.button3);
mButton4= (Button) findViewById(R.id.button4);
mButton5= (Button) findViewById(R.id.button5);
mButton6= (Button) findViewById(R.id.button6);
mButton7= (Button) findViewById(R.id.button7);
mButton8= (Button) findViewById(R.id.button8);
mButton9= (Button) findViewById(R.id.button9);
mButton.setOnClickListener(this);
mButton2.setOnClickListener(this);
mButton3.setOnClickListener(this);
mButton4.setOnClickListener(this);
mButton5.setOnClickListener(this);
mButton6.setOnClickListener(this);
mButton7.setOnClickListener(this);
mButton8.setOnClickListener(this);
mButton9.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.button:
showAlertDialog();
break;
case R.id.button2:
showSelectDialog();
break;
case R.id.button3:
showProgressDialog();
break;
case R.id.button4:
showSingleChoiceDialog();
break;
case R.id.button5:
showMultiChoiceDialog();
break;
case R.id.button6:
showOwnDialog();
break;
case R.id.button7:
showDateDialog();
break;
case R.id.button8:
showTimeDialog();
break;
case R.id.button9:
showPopupWindow();
break;
default:
break;
}
}
/**
*
* @param keyCode
* @param event
* @return 通過返回鍵返回
*/
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if(keyCode==KeyEvent.KEYCODE_BACK){
if(mPopupWindow!=null&&mPopupWindow.isShowing()){
mPopupWindow.dismiss();
return true;
}
}
return super.onKeyDown(keyCode, event);
}
/**
*AlertDialog
*/
public void showAlertDialog(){
AlertDialog.Builder builder=new AlertDialog.Builder(MainActivity.this);
builder.setIcon(R.mipmap.ic_launcher).setTitle("我是Dialog").setMessage("這是內容").setNegativeButton("Negative", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(getApplicationContext(),"點擊的是Negative",Toast.LENGTH_LONG).show();
}
}).setNeutralButton("Neutral", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(getApplicationContext(),"點擊的是Neutral",Toast.LENGTH_LONG).show();
}
}).setPositiveButton("Positive", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(getApplicationContext(),"點擊的是Positive",Toast.LENGTH_LONG).show();
}
});
AlertDialog dialog=builder.create();
dialog.show();
}
/**
*多選一Dialog
*/
public void showSelectDialog(){
AlertDialog.Builder builder=new AlertDialog.Builder(MainActivity.this);
builder.setTitle("多條選擇");
builder.setItems(array, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(getApplicationContext(),"選擇了第"+which+"個",Toast.LENGTH_LONG).show();
}
});
AlertDialog dialog=builder.create();
dialog.show();
}
/**
*ProgressDialog
*/
public void showProgressDialog(){
ProgressDialog progressDialog=new ProgressDialog(MainActivity.this);
progressDialog.setTitle("Progress Dialog");
progressDialog.setMessage("Loding...");
progressDialog.show();
}
/**
*單選Dialog
*/
public void showSingleChoiceDialog(){
AlertDialog.Builder builder=new AlertDialog.Builder(MainActivity.this);
builder.setTitle("選擇性別");
builder.setSingleChoiceItems(mSex, 0, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
sex=mSex[which];
Toast.makeText(getApplicationContext(),"選擇的性別是"+mSex[which],Toast.LENGTH_LONG).show();
}
});
builder.setNeutralButton("確定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
mButton4.setText("您的性別是"+sex);
}
});
builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
AlertDialog dialog=builder.create();
dialog.show();
}
/**
*多選Dialog
*/
public void showMultiChoiceDialog(){
AlertDialog.Builder builder=new AlertDialog.Builder(MainActivity.this);
builder.setTitle("選擇愛好");
builder.setMultiChoiceItems(mHobby, mIsCheckedHobby, new DialogInterface.OnMultiChoiceClickListener() {
@Override
public void onClick(DialogInterface dialog, int which, boolean isChecked) {
mIsCheckedHobby[which]=isChecked;
}
});
builder.setNeutralButton("確定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
hobby=new StringBuffer();
for(int i=0;i<mHobby.length;i++){
if(mIsCheckedHobby[i]){
hobby.append(mHobby[i]);
}
}
mButton5.setText("您的愛好是"+hobby);
}
});
builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
AlertDialog dialog=builder.create();
dialog.show();
}
/**
* 自定義Dialog
*/
public void showOwnDialog(){
mDialog=new Dialog(MainActivity.this);
LayoutInflater inflater=getLayoutInflater();
View dialogView=inflater.inflate(R.layout.dialog_layout,null);
TextView textView_title= (TextView) dialogView.findViewById(R.id.textview_title);
TextView textView_message= (TextView) dialogView.findViewById(R.id.textview_message);
Button button_cancel= (Button) dialogView.findViewById(R.id.button_cancel);
Button button_ok= (Button) dialogView.findViewById(R.id.button_ok);
textView_title.setText("這是標題");
textView_message.setText("這是內容");
button_cancel.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mDialog.dismiss();
}
});
button_ok.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this,"點擊了確定",Toast.LENGTH_LONG).show();
mDialog.dismiss();
}
});
mDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
mDialog.setContentView(dialogView);
mDialog.show();
}
/**
* 日期Dialog
*/
public void showDateDialog(){
mCalendar=Calendar.getInstance();
DatePickerDialog dialog=new DatePickerDialog(MainActivity.this, new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
mCalendar.set(year,monthOfYear,dayOfMonth);
SimpleDateFormat format=new SimpleDateFormat("yyyy年MM月dd日");
Toast.makeText(getApplicationContext(),"得到造型後的日期"+format.format(mCalendar.getTime()),Toast.LENGTH_LONG).show();
}
},mCalendar.get(Calendar.YEAR),mCalendar.get(Calendar.MONTH),mCalendar.get(Calendar.DAY_OF_MONTH));
dialog.show();
}
/**
* 時間Dialog
*/
public void showTimeDialog(){
mCalendar=Calendar.getInstance();
TimePickerDialog dialog=new TimePickerDialog(MainActivity.this, new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
mCalendar.set(Calendar.HOUR,hourOfDay);
mCalendar.set(Calendar.MINUTE,minute);
SimpleDateFormat format=new SimpleDateFormat("yyyy年MM月dd日HH:mm");
Toast.makeText(getApplicationContext(),"得到造型後的日期"+format.format(mCalendar.getTime()),Toast.LENGTH_LONG).show();
}
},mCalendar.get(Calendar.HOUR),mCalendar.get(Calendar.MINUTE),true);
dialog.show();
}
/**
*PopupWindowDialog 彈出框
*/
public void showPopupWindow(){
mPopupWindow=new PopupWindow(MainActivity.this);
View popView=getLayoutInflater().inflate(R.layout.pop_layout,null); mPopupWindow.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT); mPopupWindow.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
mPopupWindow.setContentView(popView);
mPopupWindow.setFocusable(false);
mPopupWindow.setOutsideTouchable(true);//點擊邊框以外的部分返回
mPopupWindow.showAsDropDown(mButton6);//在Button6的下方彈出(顯示)
}
}
自定義dialog
加工後:
<style parent="@android:Theme.Dialog" name="NoDialogTitle">
<item name="android:windowFrame">@null</item>
<item name="android:windowNoTitle">true</item>
<item name="android:windowBackground">@android:color/transparent</item>
<item name="android:windowIsFloating">true</item>
<item name="android:windowContentOverlay">@null</item>
</style>
public void showOwnDialog(){
mDialog=new Dialog(MainActivity.this,R.style.NoDialogTitle);//去掉標題
...
...
//mDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);//這一句可省略
mDialog.setContentView(dialogView);
mDialog.show();
}
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<corners android:bottomLeftRadius="@dimen/corners"/>//設置左下角爲圓角,距離爲"@dimen/corners",這是在dimens中設置的
//(<corners android:radius="@dimen/corners"/>):設置四個角都爲圓角
<solid android:color="#85edFF"/>//設置背景色
<stroke android:color="#ffffff" android:width="1dp"/>//設置邊框線的顏色及寬度
</shape>
設置圓角距離(大小):
<resources>
<dimen name="corners">5dp</dimen>
</resources>
日期Dialog:
時間Dialog:
PopupWindow: