android customDialog自定义对话框

阅读更多
MainActivity.java
package com.customDialog;

import android.os.Bundle;
import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;

public class MainActivity extends Activity {

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		Button btn = (Button) findViewById(R.id.button1);

		btn.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Dialog dialog = new MyDialog(MainActivity.this);
				dialog.setTitle("对话框提示");
				// dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
				dialog.show();
			}
		});
	}

	class MyDialog extends Dialog {

		public MyDialog(Context context) {
			super(context);
			// TODO Auto-generated constructor stub
		}

		@Override
		protected void onCreate(Bundle savedInstanceState) {
			// TODO Auto-generated method stub
			super.onCreate(savedInstanceState);
			this.setContentView(R.layout.dialog);
			Button btn1 = (Button) findViewById(R.id.button1);
			Button btn2 = (Button) findViewById(R.id.button2);

			btn1.setOnTouchListener(new OnTouchListener() {

				@Override
				public boolean onTouch(View v, MotionEvent event) {
					// TODO Auto-generated method stub
					if (v.getId() == R.id.button1) {
						MyDialog.this.dismiss();
					}
					return false;
				}
			});

			btn2.setOnTouchListener(new OnTouchListener() {

				@Override
				public boolean onTouch(View v, MotionEvent event) {
					// TODO Auto-generated method stub
					if (v.getId() == R.id.button2) {
						MyDialog.this.dismiss();
					}
					return true;
				}
			});
		}

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.activity_main, menu);
		return true;
	}
}





activity_main.xml


    



dialog.xml:




    

    

    



  • com.customDialog.rar (688.7 KB)
  • 下载次数: 1

你可能感兴趣的:(自定义对话框,对话框)