Android自訂ProgressDialog

來源:互聯網
上載者:User

package cn.testprogressdialog;import android.app.Activity;import android.app.Dialog;import android.content.Context;import android.content.DialogInterface;import android.content.DialogInterface.OnCancelListener;import android.os.Bundle;import android.view.Window;/** * Demo描述: * 自訂ProgressDialog *  * 備忘說明: * 在此處progressDialog.setOnCancelListener(){} * 並沒有起到按下back鍵盤使風火輪消失的作用. * 因為在initDialog(Context context)中設定了 * dialog.setCancelable(false); * 若需要此功能,注釋掉該句代碼即可. */public class MainActivity extends Activity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);Dialog progressDialog=initDialog(MainActivity.this);progressDialog.setOnCancelListener(new OnCancelListener() {@Overridepublic void onCancel(DialogInterface dialog) {dialog.dismiss();//其餘商務邏輯}});progressDialog.show();}public Dialog initDialog(Context context) {Dialog dialog = new DialogSubClass(MainActivity.this, R.style.dialog);dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);dialog.setCanceledOnTouchOutside(false);dialog.setCancelable(false);return dialog;}}
package cn.testprogressdialog;import android.app.Dialog;import android.content.Context;import android.os.Bundle;import android.view.LayoutInflater;import android.view.View;public class DialogSubClass extends Dialog {public DialogSubClass(Context context, int theme) {super(context, theme);}protected DialogSubClass(Context context, boolean cancelable,OnCancelListener cancelListener) {super(context, cancelable, cancelListener);}public DialogSubClass(Context context) {super(context);}@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);View view = LayoutInflater.from(getContext()).inflate(R.layout.dialoglayout, null);setContentView(view);}@Overridepublic void show() {super.show();}@Overridepublic void dismiss() {super.dismiss();}public void setMsg(String txt) {}}
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="wrap_content"    android:layout_height="wrap_content" >    <ProgressBar        android:id="@+id/progressBar"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_centerHorizontal="true"        />    <TextView        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="loading"        android:textSize="20sp"        android:layout_below="@id/progressBar"        android:layout_marginTop="-5dip"        android:layout_centerHorizontal="true"        android:textColor="@android:color/black"         /></RelativeLayout>
<resources>    <style name="AppBaseTheme" parent="android:Theme.Light"></style>    <style name="AppTheme" parent="AppBaseTheme"></style>        <style name="dialog" parent="@android:style/Theme.Dialog">        <item name="android:windowFrame">@null</item>        <item name="android:windowIsFloating">true</item>        <item name="android:windowIsTranslucent">true</item>        <item name="android:windowNoTitle">true</item>        <item name="android:windowBackground">@android:color/transparent</item>        <item name="android:backgroundDimEnabled">true</item>        <item name="android:windowContentOverlay">@null</item>    </style></resources>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    >    <TextView        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="測試自訂ProgressDialog"        android:layout_centerHorizontal="true"        android:textSize="24sp"    /></RelativeLayout>

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.