自定义Dialog

本文介绍如何在Android应用中使用DialogUtil类创建加载对话框,并提供了展示、隐藏及检查对话框显示状态的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

工具类:
public class DialogUtil {

@SuppressLint("InlinedApi")
public static Dialog createLoadingDialog(Context context) {

    LayoutInflater inflater = LayoutInflater.from(context);
    View v = inflater.inflate(R.layout.progress_bar, null);
    LinearLayout layout = (LinearLayout) v.findViewById(R.id.layout);
    Dialog loadingDialog = new Dialog(context, R.style.loading_dialog_tran);
    loadingDialog.setCanceledOnTouchOutside(false);// true点击对话框外部取消对话框显示
    loadingDialog.setCancelable(false);// 不可以用“返回键”取消
    loadingDialog.setContentView(layout, new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
            LinearLayout.LayoutParams.MATCH_PARENT));
    return loadingDialog;

}

}

设置基类,添加调用方法:
package com.example.dialogdemo;

import android.app.Dialog;
import android.support.v7.app.ActionBarActivity;

public class baseActionBarActivity extends ActionBarActivity {
/**
* 加载图片提示框
*/
private Dialog progressDialog;

/**
 * 显示dialog 
 */
public void showProgressDialog() {
    try {

        if (progressDialog == null) {
            progressDialog = DialogUtil.createLoadingDialog(this);

        }
        progressDialog.show();
    } catch (Exception e) {
        e.printStackTrace();
    }

}

/**
 * 隐藏dialog
 */
public void dismissProgressDialog() {
    try {

        if (progressDialog != null && progressDialog.isShowing()) {
            progressDialog.dismiss();
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

/**
 * dialog是否显示
 */
public boolean isShow() {
    try {

        if (progressDialog != null && progressDialog.isShowing()) {
            return true;
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return false;
}

}

首页显示:
package com.example.dialogdemo;

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends baseActionBarActivity implements OnClickListener {

private Button show, dismiss;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    show = (Button) findViewById(R.id.show);
    show.setOnClickListener(this);
    dismiss = (Button) findViewById(R.id.dismiss);
    dismiss.setOnClickListener(this);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}

@Override
public void onClick(View v) {
    // TODO Auto-generated method stub
    switch (v.getId()) {
    case R.id.show:
        showProgressDialog();
        break;
    case R.id.dismiss:
        dismissProgressDialog();
        break;

    default:
        break;
    }

}

}

下载地址
https://github.com/youlongxifeng/Dialog

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值