版權聲明:本文爲HaiyuKing原創文章,轉載請註明出處!java
回收刪除對話框,繼承AlertDialog。android
仿照釘釘的長按彈出的移除置頂對話框。git
繼承AlertDialog;github
實現標題文本變化的狀況下,對話框寬度保持不變。app
注意事項:ide
一、 導入類文件後須要change包名以及從新import R文件路徑佈局
二、 Values目錄下的文件(strings.xml、dimens.xml、colors.xml等),若是項目中存在,則複製裏面的內容,不要整個覆蓋ui
package com.why.project.articleremovedeldialogdemo.dialog; import android.app.Activity; import android.app.AlertDialog; import android.content.Context; import android.os.Bundle; import android.util.DisplayMetrics; import android.view.View; import android.view.ViewGroup; import android.widget.LinearLayout; import android.widget.TextView; import com.why.project.articleremovedeldialogdemo.R; /** * Created by HaiyuKing * Used 回收、刪除對話框 */ public class ArticleRemoveDelDialog extends AlertDialog { private static final String TAG = ArticleRemoveDelDialog.class.getSimpleName(); private Context context;//上下文 private String buildTitle;//標題 private TextView title_tv; private TextView remove_tv; private TextView del_tv; public ArticleRemoveDelDialog(Context context, String buildTitle) { super(context, R.style.style_alert_confirm_dialog); //設置爲false,按對話框之外的地方不起做用 setCanceledOnTouchOutside(true); //設置爲false,按返回鍵不能退出 setCancelable(true); this.context = context; this.buildTitle = buildTitle; } protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.alert_dialog_remove_del); initViews(); initDatas(); initEvents(); } /**初始化view*/ private void initViews() { title_tv = findViewById(R.id.title_tv); remove_tv = findViewById(R.id.remove_tv); del_tv = findViewById(R.id.del_tv); } /**初始化數據*/ private void initDatas() { title_tv.setText(buildTitle);//設置標題 //設置標題區域的寬度 final DisplayMetrics display = new DisplayMetrics(); ((Activity)this.context).getWindowManager().getDefaultDisplay().getMetrics(display); title_tv.setLayoutParams(new LinearLayout.LayoutParams(display.widthPixels - context.getResources().getDimensionPixelOffset(R.dimen.alert_dialog_confirm_margin) * 2, ViewGroup.LayoutParams.WRAP_CONTENT)); } /**初始化監聽事件*/ private void initEvents() { //回收 remove_tv.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if(mOnRemoveDelButtonClickListener != null){ mOnRemoveDelButtonClickListener.onRemoveButtonClick(); } dismiss(); } }); //刪除 del_tv.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if(mOnRemoveDelButtonClickListener != null){ mOnRemoveDelButtonClickListener.onDelButtonClick(); } dismiss(); } }); } public static abstract interface OnRemoveDelButtonClickListener { //回收按鈕的點擊事件接口 public abstract void onRemoveButtonClick(); //刪除按鈕的點擊事件接口 public abstract void onDelButtonClick(); } private OnRemoveDelButtonClickListener mOnRemoveDelButtonClickListener; public void setOnRemoveDelButtonClickListener(OnRemoveDelButtonClickListener mOnRemoveDelButtonClickListener) { this.mOnRemoveDelButtonClickListener = mOnRemoveDelButtonClickListener; } }
<?xml version="1.0" encoding="utf-8"?> <resources> <color name="colorPrimary">#3F51B5</color> <color name="colorPrimaryDark">#303F9F</color> <color name="colorAccent">#FF4081</color> <!-- ==================確認取消對話框(alertdialog)================== --> <!-- 對話框的填充顏色 --> <color name="alert_dialog_confirm_solid_color">#ffffffff</color> </resources>
<?xml version="1.0" encoding="utf-8"?> <resources> <!-- ==================確認取消對話框(alertdialog)================== --> <!-- 對話框的圓角角度 --> <dimen name="alert_dialog_confirm_corners">5dp</dimen> <!-- 對話框的總體外邊距 --> <dimen name="alert_dialog_confirm_margin">30dp</dimen> </resources>
<resources> <!-- Base application theme. --> <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar"> <!-- Customize your theme here. --> <item name="colorPrimary">@color/colorPrimary</item> <item name="colorPrimaryDark">@color/colorPrimaryDark</item> <item name="colorAccent">@color/colorAccent</item> </style> <!-- ==================確認取消對話框(alertdialog)================== --> <style name="style_alert_confirm_dialog"> <!-- Dialog的windowFrame框爲無 --> <item name="android:windowFrame">@null</item> <!-- 是否顯示title --> <item name="android:windowNoTitle">false</item> <!-- 是否浮如今activity之上 --> <item name="android:windowIsFloating">true</item> <!-- 半透明 --> <item name="android:windowIsTranslucent">true</item> <!-- 設置dialog的背景:#00000000透明色 --> <item name="android:windowBackground">@android:color/transparent</item> <!-- 背景變灰:整個屏幕變灰,配合setCanceledOnTouchOutside(false) --> <item name="android:backgroundDimEnabled">true</item> </style> </resources>
package com.why.project.articleremovedeldialogdemo; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.widget.Button; import android.widget.Toast; import com.why.project.articleremovedeldialogdemo.dialog.ArticleRemoveDelDialog; public class MainActivity extends AppCompatActivity { private Button btn_open; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initViews(); initEvents(); } private void initViews() { btn_open = findViewById(R.id.btn_open); } private void initEvents() { btn_open.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { ArticleRemoveDelDialog articleRemoveDelDialog = new ArticleRemoveDelDialog(MainActivity.this,"這是一個標題,可能會很長文本,若是超過寬度的話,會省略號"); articleRemoveDelDialog.setOnRemoveDelButtonClickListener(new ArticleRemoveDelDialog.OnRemoveDelButtonClickListener() { @Override public void onRemoveButtonClick() { Toast.makeText(MainActivity.this,"回收",Toast.LENGTH_SHORT).show(); } @Override public void onDelButtonClick() { Toast.makeText(MainActivity.this,"刪除",Toast.LENGTH_SHORT).show(); } }); articleRemoveDelDialog.show(); } }); } }
無this
暫時空缺spa