自定义Dialog用于电话咨询,具有从底部弹出效果,点击按钮改变颜色

最终效果如下:


1.主activity有个按钮点击弹出对话框,初始化Dialog,具体代码如下:

package com.example.customdialog;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends Activity {
	private Context context;
	private Button button;
	private Dialog dialog;

	private TextView phone_number;
	private Button cancel_btn,call_btn;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        context = this;
        
        //初始化对话框
        initDialog();
        
        button = (Button) findViewById(R.id.button);
        button.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				dialog.show();
			}
		});
    }
    
    private void initDialog(){
    	dialog = new Dialog(context, R.style.dialog);
    	dialog.setContentView(R.layout.dialog_view);
    	
    	Window window = dialog.getWindow();
    	//将Dialog显示在底部
    	window.setGravity(Gravity.BOTTOM);
    	//设置Dialog弹出方式
    	window.setWindowAnimations(R.style.dialogBottomStyle);
    	
    	android.view.WindowManager.LayoutParams params = window.getAttributes();
    	DisplayMetrics dm = new DisplayMetrics();
    	getWindowManager().getDefaultDisplay().getMetrics(dm);
    	params.width = dm.widthPixels;
    	
    	phone_number = (TextView) dialog.findViewById(R.id.phone_num);
    	//取消按钮
    	cancel_btn = (Button) dialog.findViewById(R.id.cancel_btn);
    	cancel_btn.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				if(dialog != null)
					dialog.cancel();
			}
		});
    	
    	//拨打电话按钮
    	call_btn = (Button) dialog.findViewById(R.id.call_btn);
    	call_btn.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent intent = new Intent(Intent.ACTION_CALL,Uri.parse("tel:"+phone_number.getText().toString()));
				startActivity(intent);
			}
		});
    }
    
}


2.activity_main.xml文件



    
3.自定义Dialog视图,dialog_view.xml





    

    

    

        


Demo下载
http://pan.baidu.com/s/1dD5Z22d

你可能感兴趣的:(Android_UI)