zoukankan      html  css  js  c++  java
  • Dialog的使用

    看了SDK,写了这几个例子。

    代码如下:

    package com.chaowen;

    import android.app.Activity;
    import android.app.AlertDialog;
    import android.app.Dialog;
    import android.app.ProgressDialog;
    import android.app.AlertDialog.Builder;
    import android.content.Context;
    import android.content.DialogInterface;
    import android.os.Bundle;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.view.View.OnClickListener;
    import android.widget.Button;
    import android.widget.ImageView;
    import android.widget.TextView;
    import android.widget.Toast;

    publicclass DialogActivity extends Activity {
    private Button dialog1,dialog2,dialog3,dialog4,dialog5,dialog6,dialog7;
    @Override
    publicvoid onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    dialog1
    = (Button)findViewById(R.id.button01);
    dialog2
    = (Button)findViewById(R.id.button02);
    dialog3
    = (Button)findViewById(R.id.button03);
    dialog4
    = (Button)findViewById(R.id.button04);
    dialog5
    = (Button)findViewById(R.id.button05);
    dialog6
    = (Button)findViewById(R.id.button06);
    dialog7
    = (Button)findViewById(R.id.button07);


    dialog1.setOnClickListener(
    new OnClickListener() {

    @Override
    publicvoid onClick(View v) {
    AlertDialog.Builder builder
    =new AlertDialog.Builder(DialogActivity.this);
    builder.setMessage(
    "这是一个简单的对话框")
    .setCancelable(
    false)
    .setPositiveButton(
    "确定", new DialogInterface.OnClickListener() {

    @Override
    publicvoid onClick(DialogInterface dialog, int which) {
    Toast.makeText(DialogActivity.
    this, "你按了确定", Toast.LENGTH_LONG).show();

    }
    })
    .setNegativeButton(
    "取消", new DialogInterface.OnClickListener() {

    @Override
    publicvoid onClick(DialogInterface dialog, int which) {
    Toast.makeText(DialogActivity.
    this, "你按了取消", Toast.LENGTH_LONG).show();

    }
    });
    AlertDialog alert
    =builder.create();
    alert.show();
    }
    });


    dialog2.setOnClickListener(
    new OnClickListener() {

    @Override
    publicvoid onClick(View v) {
    final CharSequence[] items = {"Red","Green","Blue"};
    AlertDialog.Builder builder
    =new AlertDialog.Builder(DialogActivity.this);
    builder.setTitle(
    "选择颜色");
    builder.setItems(items,
    new DialogInterface.OnClickListener() {

    @Override
    publicvoid onClick(DialogInterface dialog, int item) {
    Toast.makeText(getApplicationContext(), items[item], Toast.LENGTH_LONG).show();

    }
    });
    AlertDialog alert
    = builder.create();
    alert.show();
    }
    });


    dialog3.setOnClickListener(
    new OnClickListener() {

    @Override
    publicvoid onClick(View v) {
    final CharSequence[] items = {"Red","Green","Blue"};
    AlertDialog.Builder builder
    =new AlertDialog.Builder(DialogActivity.this);
    builder.setTitle(
    "选择颜色");
    builder.setSingleChoiceItems(items,
    -1, new DialogInterface.OnClickListener() {

    @Override
    publicvoid onClick(DialogInterface dialog, int item) {
    Toast.makeText(getApplicationContext(), items[item],Toast.LENGTH_LONG).show();

    }
    });

    AlertDialog alert
    = builder.create();
    alert.show();
    }
    });

    dialog4.setOnClickListener(
    new OnClickListener() {

    @Override
    publicvoid onClick(View v) {
    ProgressDialog dialog
    = ProgressDialog.show(DialogActivity.this, "这是一个圆形处理条",
    "Loading. Please wait...", true);

    }
    });


    dialog5.setOnClickListener(
    new OnClickListener() {

    @Override
    publicvoid onClick(View v) {
    ProgressDialog pd
    =new ProgressDialog(DialogActivity.this);
    pd.setTitle(
    "这是一个水平进度条");
    pd.setMessage(
    "Load.....");
    pd.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
    pd.setCancelable(
    true);
    pd.show();
    }
    });


    //自定义处理条一
    dialog6.setOnClickListener(new OnClickListener() {

    @Override
    publicvoid onClick(View v) {
    AlertDialog.Builder builder;
    AlertDialog alertDialog;
    Context mContext
    = DialogActivity.this;
    LayoutInflater inflater
    = (LayoutInflater) mContext.getSystemService(LAYOUT_INFLATER_SERVICE);
    View layout
    = inflater.inflate(R.layout.custom_dialog, (ViewGroup)findViewById(R.id.layout_root));


    TextView text
    = (TextView) layout.findViewById(R.id.text);
    text.setText(
    "Hello, this is a custom dialog!");
    ImageView image
    = (ImageView) layout.findViewById(R.id.image);
    image.setImageResource(R.drawable.icon);
    builder
    =new AlertDialog.Builder(mContext);
    builder.setView(layout);
    alertDialog
    = builder.create();
    alertDialog.show();
    }
    });


    //自定义处理条二
    dialog7.setOnClickListener(new OnClickListener() {

    @Override
    publicvoid onClick(View v) {
    Context mContext
    = DialogActivity.this;
    Dialog dialog
    =new Dialog(mContext);

    dialog.setContentView(R.layout.custom_dialog);
    dialog.setTitle(
    "Custom Dialog");

    TextView text
    = (TextView) dialog.findViewById(R.id.text);
    text.setText(
    "Hello, this is a custom dialog!");
    ImageView image
    = (ImageView) dialog.findViewById(R.id.image);
    image.setImageResource(R.drawable.icon);
    dialog.show();
    }
    });
    }


    }

    源码下载:

     

    http://u.115.com/file/aq25dgey

  • 相关阅读:
    C# TransactionScope 使用
    .Net 4.5 的async 和await 的简单理解使用
    图片的等比缩放
    IIS 8 下使用 WCF
    SQL Server 中字符串中包含字符串变量的表示方法
    jsTree 的简单用法--异步加载和刷新数据
    webService 部署以后参数输入框不能显示
    js 节点属性
    js 数组排序
    js 时间格式化 -- 时间加减实现
  • 原文地址:https://www.cnblogs.com/andgoo/p/2094986.html
Copyright © 2011-2022 走看看