AlertDialog(自定义对话框)示例

一、定义一个与系统对话框不同的布局,然后调用setView()将布局inflater到alertDialog上:

二、效果图:

AlertDialog(自定义对话框)示例_第1张图片

三、帖代码:

MainActivity

package com.Evan.demo_alertdialog;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends Activity {

	private Button button;
	private AlertDialog alert=null;
	private AlertDialog.Builder builder=null;
	private View view;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		button=(Button) findViewById(R.id.button1);
		builder=new AlertDialog.Builder(MainActivity.this);
		
		//加载自定义的View
		final LayoutInflater inflater=MainActivity.this.getLayoutInflater();
		view=inflater.inflate(R.layout.view_dialog_custom, null,false);
	    builder.setView(view);
		builder.setCancelable(false);
	    alert=builder.create();
	    view.findViewById(R.id.btn_close).setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				alert.dismiss();
			}
		});
	    view.findViewById(R.id.btn_blog).setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Intent intent=new Intent(MainActivity.this,MyActivity.class);
				startActivity(intent);
				alert.dismiss();
			}
		});
	    view.findViewById(R.id.btn_cancle).setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				alert.dismiss();
			}
		});
	    button.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				alert.show();
			}
		});
	}
}
Myactivity:

package com.Evan.demo_alertdialog;

import android.app.Activity;
import android.os.Bundle;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;

public class MyActivity extends Activity {
	private static long ExitTime = 0;
	private WebView webView;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		webView=new WebView(this);
		webView.setWebViewClient(new WebViewClient(){
			@Override
			public boolean shouldOverrideUrlLoading(WebView view, String url) {
				view.loadUrl(url);
				return true;
			}
		});
		webView.getSettings().setJavaScriptEnabled(true);//设置webView属性,运行JS脚本
		webView.loadUrl("http://blog.csdn.net/xiangyong_1521");//连接
		setContentView(webView);//显示
	}

	/*
	 * 我们需要重写回退按钮的事件,当用户点击回退按钮判断
	 * 1、webView.canGoBack()判断网页是否能后退,可以则goback()
	 * 2、如果不可以,点击两次退出APP,弹出提示Toast
	 */
	@Override
	public void onBackPressed() {
		if(webView.canGoBack()){
			webView.goBack();
		}else{
			if(System.currentTimeMillis()-ExitTime>2000){
				Toast.makeText(getApplicationContext(), "再按一次退出我的微博", 0).show();
				ExitTime=System.currentTimeMillis();
			}else{
				super.onBackPressed();
			}
		}

	}
}
view_dialog_custom.xml:




    
        
         

        

        


btn_selctor_choose.xml:




    
    

btn_selctor_exit.xml:




    
    

AlertDialog(自定义对话框)示例_第2张图片


你可能感兴趣的:(View)