編輯:關於Android編程
在開發中,如果感覺系統自帶的提示框不好看,開發者可以自己定義提示框的樣式,主要是繼承Dialog
程序目錄結構<喎?/kf/ware/vc/" target="_blank" class="keylink">vcD4KPHA+PGltZyBzcmM9"/uploadfile/Collfiles/20140816/2014081608450642.png" alt="\">
關鍵代碼
package com.dzt.custom.dialog; import android.app.Dialog; import android.content.Context; import android.content.res.Resources; import android.util.DisplayMetrics; import android.view.Gravity; import android.view.View; import android.view.Window; import android.view.WindowManager; import android.widget.TextView; /** * 自定義對話框 * * */ public class CustomDialog extends Dialog implements android.view.View.OnClickListener { private static int default_width = 400; // 默認寬度 private static int default_height = 200;// 默認高度 private static String mShowText = null; private TextView mText = null; public CustomDialog(Context context) { super(context); } public CustomDialog(Context context, int layout, int style, String msg) { this(context, default_width, default_height, layout, style, msg); } public CustomDialog(Context context, int width, int height, int layout, int style, String msg) { super(context, style); // 設置內容 setContentView(layout); mShowText = msg; initWidgets(); // 設置窗口屬性 Window window = getWindow(); WindowManager.LayoutParams params = window.getAttributes(); // 設置寬度、高度、密度、對齊方式 float density = getDensity(context); params.width = (int) (width * density); params.height = (int) (height * density); params.gravity = Gravity.CENTER; window.setAttributes(params); } @Override protected void onStop() { // TODO Auto-generated method stub super.onStop(); System.out.println("stop"); } /** * 初始化提示框中的控件 */ private void initWidgets() { CustomImageButton btn = (CustomImageButton) findViewById(R.id.btn_ok); btn.setOnClickListener(this); btn = (CustomImageButton) findViewById(R.id.btn_cancel); btn.setOnClickListener(this); mText = (TextView) findViewById(R.id.dlg_tv_text); mText.setText(mShowText); } /** * 獲取顯示密度 * * @param context * @return */ public float getDensity(Context context) { Resources res = context.getResources(); DisplayMetrics dm = res.getDisplayMetrics(); return dm.density; } @Override public void onClick(View v) { // TODO Auto-generated method stub switch (v.getId()) { case R.id.btn_ok: if (listener != null) listener.onClickOk(); System.out.println("you click ok button---------"); break; case R.id.btn_cancel: if (listener != null) listener.onClickCancel(); System.out.println("you click cancel button-------"); break; default: break; } } public void setOnClickBtnListener(OnClickBtnListener listener) { this.listener = listener; } private OnClickBtnListener listener = null; public interface OnClickBtnListener { public void onClickOk(); public void onClickCancel(); } }在提示框中對兩個按鈕設置了監聽器,按鈕也是自定義的
package com.dzt.custom.dialog; import android.content.Context; import android.content.res.TypedArray; import android.graphics.Canvas; import android.graphics.Paint; import android.graphics.Paint.Align; import android.util.AttributeSet; import android.util.Log; import android.widget.ImageButton; /** * 自定義ImageButton 可以在ImageButton上面設置文字 */ public class CustomImageButton extends ImageButton { private static final String TAG = "CustomImageButton_dzt"; private String mtext = ""; private int mcolor = 0; private float mtextsize = 0f; private Paint mpatin; public CustomImageButton(Context context, AttributeSet attrs) { super(context, attrs); initAttrs(attrs); } private void initAttrs(AttributeSet attrs) { TypedArray array = getContext().obtainStyledAttributes(attrs, R.styleable.CustomButtonAttrs); mtext = array.getString(R.styleable.CustomButtonAttrs_textValue); mcolor = array.getColor(R.styleable.CustomButtonAttrs_textColor, 230); mtextsize = array.getDimension(R.styleable.CustomButtonAttrs_textSize, 25.0f); array.recycle(); // 回收資源 mpatin = new Paint(); mpatin.setTextAlign(Align.CENTER); Log.d(TAG, "mtextsize = " + mtextsize); } public void setText(String text) { this.mtext = text; } public void setColor(int color) { this.mcolor = color; } public void setTextSize(float textsize) { this.mtextsize = textsize; } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); mpatin.setColor(mcolor); mpatin.setTextSize(mtextsize); canvas.drawText(mtext, canvas.getWidth() / 2, (canvas.getHeight() / 2)+10, mpatin); } }在調用時也非常簡單
@Override public void onClick(View v) { // TODO Auto-generated method stub switch (v.getId()) { case R.id.btn_show_dialog: customDialog = new CustomDialog(this, R.layout.dialog_layout, R.style.DialogTheme, getResources().getString( R.string.text_show)); customDialog.show(); customDialog.setOnClickBtnListener(new OnClickBtnListener() { @Override public void onClickOk() { // TODO Auto-generated method stub System.out.println("you click ok button"); // 處理點擊OK時的操作 // ............... customDialog.cancel(); } @Override public void onClickCancel() { // TODO Auto-generated method stub System.out.println("you click cancel button"); // 處理點擊Cancel時的操作 // ................... customDialog.cancel(); } }); break; default: break; } }需要源碼的可以到:http://download.csdn.net/detail/deng0zhaotai/7760615
微信指紋支付是目前微信為了進一步提升交易安全所推出的支付驗證手段,但也有很多用戶在使用中會發現很多小問題,比如手上出汗時,就不能順利驗證支付,小編就這個問題
B. Little Pony and Harmony Chest time limit per test 4 seconds memory limi
iBeacon的工作原理是基於Bluetooth Low Energy(BLE)低功耗藍牙傳輸技術,iBeacon基站不斷向四周發送藍牙信號,當智
MediaRecorder類詳解 手機一般都有麥克風和攝像頭,而Android系統就可以利用這些硬件來錄制音視頻了。 為了增加對錄制音視頻的支持,Android系統提