- 论坛徽章:
- 0
|
Android平台
Eclipse开发工具
可能不是最好的,希望大家多指正。
MainActivity部分的代码- package com.example.mydialog;
-
- import android.os.Bundle;
- import android.annotation.SuppressLint;
- import android.app.Activity;
- import android.app.AlertDialog;
- import android.content.Intent;
- import android.graphics.Color;
- import android.view.Menu;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.view.Window;
- import android.widget.Button;
- import android.widget.ImageButton;
- import android.widget.TextView;
-
- public class MainActivity extends Activity {
-
- Button mBtn;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
-
- mBtn=(Button) findViewById(R.id.btn);
- mBtn.setOnClickListener(new OnClickListener() {
-
- @Override
- public void onClick(View arg0) {
- // TODO Auto-generated method stub
- showAlertDialog();
- }
- });
-
- }
-
-
- // 对话框
- private void showAlertDialog() {
-
- final AlertDialog dlg = new AlertDialog.Builder(this).create();
- dlg.show();
- Window window = dlg.getWindow();
- // *** 主要就是在这里实现这种效果的.
- // 设置窗口的内容页面,alertdialog.xml文件中定义view内容
- window.setContentView(R.layout.alertdialog);
- // 为确认按钮添加事件,执行退出应用操作
- Button tv_queding = (Button) window.findViewById(R.id.tv_content1);
- tv_queding.setText(" 确 定 ");
- tv_queding.setOnClickListener(new View.OnClickListener() {
- @SuppressLint("SdCardPath")
- public void onClick(View v) {
-
- Intent intent=new Intent();
- intent.setClass(MainActivity.this, NextActivity.class);
- startActivity(intent);
- dlg.cancel();
- }
- });
- //取消按钮不做任何操作 可以不用添加动作
- Button tv_quxiao = (Button) window.findViewById(R.id.tv_content2);
- tv_quxiao.setText(" 取 消 ");
- tv_quxiao.setOnClickListener(new View.OnClickListener() {
- public void onClick(View v) {
-
-
-
- dlg.cancel();
- }
- });
-
-
- dlg.setCancelable(false);
- }
-
- }
复制代码 alertdialog.xml部分的代码 |
|