日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > Android >内容正文

Android

Android入门之AlertDialog

發布時間:2024/4/20 Android 42 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Android入门之AlertDialog 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

本文來自http://blog.csdn.net/hellogv/?,引用必須注明出處!

?????? 時隔一年,又要準備做Android的開發了,最近復習和整理一下Android的知識。這次要說的是AlertDialog,這種對話框會經常遇到。AlertDialog跟WIN32開發中的Dialog不一樣,AlertDialog是非阻塞的,而阻塞的對話框用的是PopupWindow。

?????? 先貼出程序運行的截圖:

main.xml的源碼:

[xhtml]?view plaincopyprint?
  • <?xml?version="1.0"?encoding="utf-8"?>??
  • <LinearLayout?xmlns:android="http://schemas.android.com/apk/res/android"??
  • ????android:orientation="vertical"??
  • ????android:layout_width="fill_parent"??
  • ????android:layout_height="fill_parent"??
  • ????>??
  • ??
  • <Button?android:id="@+id/Button01"?android:layout_height="wrap_content"?android:text="非Layout型對話框"?android:layout_width="fill_parent"></Button>??
  • <Button?android:id="@+id/Button02"?android:layout_height="wrap_content"?android:text="Layout型對話框"?android:layout_width="fill_parent"></Button><View?android:id="@+id/View01"?android:layout_width="wrap_content"?android:layout_height="wrap_content"></View>??
  • ??
  • </LinearLayout>??
  • ?

    下圖是非Layout型對話框,直接使用AlertDialog

    ?

    下圖是使用了Layout的對話框,可以自定義控件,實現更復雜的對話框

    dialoglayout.xml的源碼:

    [xhtml]?view plaincopyprint?
  • <?xml?version="1.0"?encoding="utf-8"?>??
  • ??
  • <LinearLayout?xmlns:android="http://schemas.android.com/apk/res/android"??
  • ????android:layout_width="fill_parent"?android:layout_height="wrap_content"??
  • ????android:orientation="vertical">??
  • ????<EditText?android:layout_height="wrap_content"??
  • ????????android:layout_width="fill_parent"?android:layout_marginLeft="20dip"??
  • ????????android:layout_marginRight="20dip"?android:textAppearance="?android:attr/textAppearanceMedium"?android:id="@+id/edtInput"/>??
  • </LinearLayout>??
  • ?

    程序源碼:

    [java]?view plaincopyprint?
  • package?com.testAlertDialog;??
  • ??
  • import?android.app.Activity;??
  • import?android.app.AlertDialog;??
  • import?android.content.Context;??
  • import?android.content.DialogInterface;??
  • import?android.os.Bundle;??
  • import?android.view.Gravity;??
  • import?android.view.LayoutInflater;??
  • import?android.view.View;??
  • import?android.view.View.OnClickListener;??
  • import?android.widget.Button;??
  • import?android.widget.EditText;??
  • import?android.widget.PopupWindow;??
  • ??
  • ??
  • public?class?testAlertDialog?extends?Activity?{??
  • ????Button?btnShowDialog;??
  • ????Button?btnShowDialog_Layout;??
  • ????/**?Called?when?the?activity?is?first?created.?*/??
  • ????@Override??
  • ????public?void?onCreate(Bundle?savedInstanceState)?{??
  • ????????super.onCreate(savedInstanceState);??
  • ????????setContentView(R.layout.main);??
  • ????????//定義按鈕??
  • ????????btnShowDialog=(Button)this.findViewById(R.id.Button01);??
  • ????????btnShowDialog.setOnClickListener(new?ClickEvent());??
  • ????????btnShowDialog_Layout=(Button)this.findViewById(R.id.Button02);??
  • ????????btnShowDialog_Layout.setOnClickListener(new?ClickEvent());??
  • ????}??
  • ??????
  • ??????
  • ????//統一處理按鍵事件??
  • ????class?ClickEvent?implements?OnClickListener{??
  • ??
  • ????????@Override??
  • ????????public?void?onClick(View?v)?{??
  • ????????????//?TODO?Auto-generated?method?stub??
  • ????????????if(v==btnShowDialog)??
  • ????????????????showDialog(testAlertDialog.this);??
  • ??????????????????
  • ????????????else?if(v==btnShowDialog_Layout)??
  • ????????????????showDialog_Layout(testAlertDialog.this);??
  • ??????????????
  • ????????}??
  • ??
  • ????}??
  • ??
  • ????//顯示基本的AlertDialog??
  • ????private?void?showDialog(Context?context)?{??
  • ????????AlertDialog.Builder?builder?=?new?AlertDialog.Builder(context);??
  • ????????builder.setIcon(R.drawable.icon);??
  • ????????builder.setTitle("Title");??
  • ????????builder.setMessage("Message");??
  • ????????builder.setPositiveButton("Button1",??
  • ????????????????new?DialogInterface.OnClickListener()?{??
  • ????????????????????public?void?onClick(DialogInterface?dialog,?int?whichButton)?{??
  • ????????????????????????setTitle("點擊了對話框上的Button1");??
  • ????????????????????}??
  • ????????????????});??
  • ????????builder.setNeutralButton("Button2",??
  • ????????????????new?DialogInterface.OnClickListener()?{??
  • ????????????????????public?void?onClick(DialogInterface?dialog,?int?whichButton)?{??
  • ????????????????????????setTitle("點擊了對話框上的Button2");??
  • ????????????????????}??
  • ????????????????});??
  • ????????builder.setNegativeButton("Button3",??
  • ????????????????new?DialogInterface.OnClickListener()?{??
  • ????????????????????public?void?onClick(DialogInterface?dialog,?int?whichButton)?{??
  • ????????????????????????setTitle("點擊了對話框上的Button3");??
  • ????????????????????}??
  • ????????????????});??
  • ????????builder.show();??
  • ????}??
  • ??
  • ??
  • ????//顯示基于Layout的AlertDialog??
  • ????private?void?showDialog_Layout(Context?context)?{??
  • ????????LayoutInflater?inflater?=?LayoutInflater.from(this);??
  • ????????final?View?textEntryView?=?inflater.inflate(??
  • ????????????????R.layout.dialoglayout,?null);??
  • ????????final?EditText?edtInput=(EditText)textEntryView.findViewById(R.id.edtInput);??
  • ????????final?AlertDialog.Builder?builder?=?new?AlertDialog.Builder(context);??
  • ????????builder.setCancelable(false);??
  • ????????builder.setIcon(R.drawable.icon);??
  • ????????builder.setTitle("Title");??
  • ????????builder.setView(textEntryView);??
  • ????????builder.setPositiveButton("確認",??
  • ????????????????new?DialogInterface.OnClickListener()?{??
  • ????????????????????public?void?onClick(DialogInterface?dialog,?int?whichButton)?{??
  • ????????????????????????setTitle(edtInput.getText());??
  • ????????????????????}??
  • ????????????????});??
  • ????????builder.setNegativeButton("取消",??
  • ????????????????new?DialogInterface.OnClickListener()?{??
  • ????????????????????public?void?onClick(DialogInterface?dialog,?int?whichButton)?{??
  • ????????????????????????setTitle("");??
  • ????????????????????}??
  • ????????????????});??
  • ????????builder.show();??
  • ????}??
  • }??

  • package com.ceac.deng;


    import android.support.v7.app.ActionBarActivity;
    import android.app.AlertDialog;
    import android.content.DialogInterface;
    import android.os.Bundle;
    import android.os.SystemClock;
    import android.view.Menu;
    import android.view.MenuItem;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;
    import android.widget.Chronometer;
    import android.widget.Toast;
    import android.widget.Chronometer.OnChronometerTickListener;




    public class MainActivity extends ActionBarActivity {


    private Chronometer chronometer;
    private Button stopButton;
    private Button startButton;

    private Button button;
    private Button button2;
    private Button button3;
    private Button button4;


    ? ? @Override
    ? ? protected void onCreate(Bundle savedInstanceState) {
    ? ? ? ? super.onCreate(savedInstanceState);
    ? ? ? ? setContentView(R.layout.activity_main);
    ? ? ? ??
    ? ? ? ? findView();
    ? ? ? ??
    ? ? ? ? chronometer.setBase(SystemClock.elapsedRealtime());
    // ? ? ? ?chronometer.setFormat("已經用了多少時間: ?%s");
    ? ? ? ? chronometer.start();
    ? ? ? ??
    ? ? ? ? chronometer.setOnChronometerTickListener(new OnChronometerTickListener() {

    @Override
    public void onChronometerTick(Chronometer chronometer) {
    // TODO Auto-generated method stub
    if (SystemClock.elapsedRealtime() - chronometer.getBase() >= 100000)
    {
    chronometer.stop();
    }
    }
    });
    ? ? ? ??
    ? ? ? ? setListenner();
    ? ? }


    ? ? /**
    ? ? ?* findView
    ? ? ?*/
    ? ? private void findView()
    ? ? {
    ? ? chronometer = (Chronometer) findViewById(R.id.chronometer1);
    ? ? stopButton = (Button) findViewById(R.id.stop);
    ? ? startButton = (Button) findViewById(R.id.start);
    ? ?
    ? ? button = (Button) findViewById(R.id.button1);
    ? ? button2 = (Button) findViewById(R.id.button2);
    ? ? button3 = (Button) findViewById(R.id.button3);
    ? ? button4 = (Button) findViewById(R.id.button4);
    ? ?
    ? ?
    ? ? }
    ? ??
    ? ? private void setListenner()
    ? ? {
    ? ? stopButton.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View v) {
    // TODO Auto-generated method stub
    chronometer.stop();
    System.out.println("stop chronometer....");
    }
    });
    ? ?
    ? ? startButton.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View v) {
    // TODO Auto-generated method stub
    chronometer.setBase(SystemClock.elapsedRealtime());
    chronometer.start();
    System.out.println("start Chronometer....");
    }
    });
    ? ?
    ? ? button.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View v) {
    // AlertDialog alertDialog = new AlertDialog.Builder(MainActivity.this).create();
    // alertDialog.setIcon(R.drawable.ic_launcher);
    // alertDialog.setTitle("系統提示");
    // alertDialog.setMessage("帶取消 、中立、確定按鈕的對話框");
    alertDialog.setButton(DialogInterface.BUTTON_NEGATIVE, "取消", listener);
    AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
    builder.setIcon(R.drawable.ic_launcher);
    builder.setTitle("系統提示");
    builder.setMessage("帶取消 、中立、確定按鈕的對話框");

    //確定
    builder.setPositiveButton("確定", new DialogInterface.OnClickListener() {

    @Override
    public void onClick(DialogInterface dialog, int which) {
    // TODO Auto-generated method stub
    showToast("點擊了確定的按鈕");
    }
    });

    //中立
    builder.setNeutralButton("中立", new DialogInterface.OnClickListener() {

    @Override
    public void onClick(DialogInterface dialog, int which) {
    // TODO Auto-generated method stub
    showToast("點擊了中立按鈕");
    }
    });

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

    @Override
    public void onClick(DialogInterface dialog, int which) {
    // TODO Auto-generated method stub
    showToast("點擊了取消按鈕");
    }
    });

    builder.show();
    }
    });
    ? ?
    ? ?
    ? ? }
    ? ??
    ? ? /**
    ? ? ?*?
    ? ? ?* @param str
    ? ? ?*/
    ? ? private void showToast(String str)
    ? ? {
    ? ? Toast.makeText(MainActivity.this, str, Toast.LENGTH_LONG).show();
    ? ? }
    ? ??
    ? ? @Override
    ? ? public boolean onCreateOptionsMenu(Menu menu) {
    ? ? ? ? // Inflate the menu; this adds items to the action bar if it is present.
    ? ? ? ? getMenuInflater().inflate(R.menu.main, menu);
    ? ? ? ? return true;
    ? ? }


    ? ? @Override
    ? ? public boolean onOptionsItemSelected(MenuItem item) {
    ? ? ? ? // Handle action bar item clicks here. The action bar will
    ? ? ? ? // automatically handle clicks on the Home/Up button, so long
    ? ? ? ? // as you specify a parent activity in AndroidManifest.xml.
    ? ? ? ? int id = item.getItemId();
    ? ? ? ? if (id == R.id.action_settings) {
    ? ? ? ? ? ? return true;
    ? ? ? ? }
    ? ? ? ? return super.onOptionsItemSelected(item);
    ? ? }
    }

    總結

    以上是生活随笔為你收集整理的Android入门之AlertDialog的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。