android_dialog_3

现在在贴上代码。不过大家可以自己去看apidemo

那里有很多的好资源!!!

main.xml

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/screen"
    android:layout_width="match_parent" android:layout_height="match_parent"
    android:orientation="vertical">
    <LinearLayout
        android:layout_width="match_parent" android:layout_height="match_parent"
        android:orientation="vertical">
        <Button android:id="@+id/two_buttons"
            android:layout_width="match_parent" android:layout_height="wrap_content"
            android:text="@string/alert_dialog_two_buttons"/>
        <Button android:id="@+id/two_buttons2"
            android:layout_width="match_parent" android:layout_height="wrap_content"
            android:text="@string/alert_dialog_two_buttons2"/>
        <Button android:id="@+id/select_button"
            android:layout_width="match_parent" android:layout_height="wrap_content"
            android:text="@string/alert_dialog_select_button"/>
        <Button android:id="@+id/progress_button"
            android:layout_width="match_parent" android:layout_height="wrap_content"
            android:text="@string/alert_dialog_progress_button"/>
        <Button android:id="@+id/radio_button"
            android:layout_width="match_parent" android:layout_height="wrap_content"
            android:text="@string/alert_dialog_single_choice"/>
        <Button android:id="@+id/checkbox_button"
            android:layout_width="match_parent" android:layout_height="wrap_content"
            android:text="@string/alert_dialog_multi_choice"/>
        <Button android:id="@+id/checkbox_button2"
            android:layout_width="match_parent" android:layout_height="wrap_content"
            android:text="@string/alert_dialog_multi_choice_cursor"/>
        <Button android:id="@+id/text_entry_button"
            android:layout_width="match_parent" android:layout_height="wrap_content"
            android:text="@string/alert_dialog_text_entry"/>
    </LinearLayout>
</ScrollView>

下面是string.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="activity_alert_dialog">App/DialogApp/Dialog</string>
    <string name="app_name">Dialog大全</string>
    <string name="alert_dialog_two_buttons">OK Cancel dialog with  message</string>
    <string name="alert_dialog_two_buttons2">OK Cancel dialog with a long message</string>
    <string name="alert_dialog_select_button">List dialog</string>
    <string name="alert_dialog_single_choice">Single choice list</string>
    <string name="alert_dialog_multi_choice">Repeat alarm</string>
    <string name="alert_dialog_multi_choice_cursor">Send Call to VoiceMail</string>
    <string name="alert_dialog_progress_button">Progress dialog</string>
    <string name="alert_dialog_text_entry">Text Entry dialog</string>
         <string name="alert_dialog_two_buttons_msg">Header title</string>
    <string name="alert_dialog_two_buttons2_msg">
        Plloaso mako nuto siwuf cakso dodtos anr koop a
        cupy uf cak vux noaw yerw phuno. Whag schengos, uf efed, quiel
        ba mada su otrenzr.\n\nSwipontgwook proudgs hus yag su ba dagarmidad.
        Plasa maku noga wipont trenzsa schengos ent kaap zux comy.\n\nWipont trenz
        kipg naar mixent phona. Cak pwico siructiun
        ruous nust apoply tyu cak Uhex sisulutiun munityuw uw dseg
    </string>
    <string name="alert_dialog_ok">OK</string>
    <string name="alert_dialog_hide">Hide</string>
    <string name="alert_dialog_something">Something</string>
    <string name="alert_dialog_cancel">Cancel</string>
    <string name="title"> 
       Lorem ipsum dolor sit aie consectetur adipiscing\nPlloaso mako nuto
        siwuf cakso dodtos anr koop.</string>
     <string name="OK">ok</string>
      <string name="select_dialog">Header title</string>
</resources>

下面是value:array.xml

<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright (C) 2007 The Android Open Source Project

     Licensed under the Apache License, Version 2.0 (the "License");
     you may not use this file except in compliance with the License.
     You may obtain a copy of the License at
 
          http://www.apache.org/licenses/LICENSE-2.0
 
     Unless required by applicable law or agreed to in writing, software
     distributed under the License is distributed on an "AS IS" BASIS,
     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     See the License for the specific language governing permissions and
     limitations under the License.
-->
<resources>
  <!-- use dialog  -->
  <string-array name="select_dialog_items">
 <item>Command one</item>
 <item>Command two</item>
 <item>Command three</item>
 <item>Command four</item>
</string-array>
 <string-array name="select_dialog_items2">
  <item>Map</item>
  <item>Satellite</item>
  <item>Traffic</item>
  <item>Street view</item>
 </string-array>

 <string-array name="select_dialog_items3">
  <item>Every Monday</item>
  <item>Every Tuesday</item>
  <item>Every Wednesday</item>
  <item>Every Thursday</item>
  <item>Every Friday</item>
  <item>Every Saturday</item>
  <item>Every Sunday</item>
 </string-array>
</resources>

下面是layout:alert_dialog_text_entry.xml

<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright (C) 2008 The Android Open Source Project

     Licensed under the Apache License, Version 2.0 (the "License");
     you may not use this file except in compliance with the License.
     You may obtain a copy of the License at
 
          http://www.apache.org/licenses/LICENSE-2.0
 
     Unless required by applicable law or agreed to in writing, software
     distributed under the License is distributed on an "AS IS" BASIS,
     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     See the License for the specific language governing permissions and
     limitations under the License.
-->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">
   <TextView
   android:id="@+id/username_view"
   android:layout_height="wrap_content"
   android:layout_width="wrap_content"
   android:layout_marginLeft="20dip"
   android:layout_marginRight="20dip"
   android:text="username"
   android:gravity="left"
   android:textAppearance="?android:attr/textAppearanceMedium"/>
    <EditText
        android:id="@+id/username_edit"
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:layout_marginLeft="20dip"
        android:layout_marginRight="20dip"
        android:scrollHorizontally="true"
        android:autoText="false"
        android:capitalize="none"
        android:gravity="fill_horizontal"
        android:textColor="?android:attr/textAppearanceMedium"
         />
     <TextView
   android:id="@+id/username_view"
   android:layout_height="wrap_content"
   android:layout_width="wrap_content"
   android:layout_marginLeft="20dip"
   android:layout_marginRight="20dip"
   android:text="password"
   android:gravity="left"
   android:textAppearance="?android:attr/textAppearanceMedium"/>
   <EditText
        android:id="@+id/password_edit"
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:layout_marginLeft="20dip"
        android:layout_marginRight="20dip"
        android:scrollHorizontally="true"
        android:autoText="false"
        android:capitalize="none"
        android:gravity="fill_horizontal"
        android:password="true"
        android:textAppearance="?android:attr/textAppearanceMedium"/>
    </LinearLayout>


下面是主程序

package com.dialog;


import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.DialogInterface.OnMultiChoiceClickListener;
import android.database.Cursor;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.provider.Contacts;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends Activity {

//定义常量
 private static final int DIALOG__YES_NO_MESSAGE=1;
 private static final int DIALOG__YES_NO_LONG_MESSAGE=2;
 private static final int DIALOG_LIST=3;
 private static final int DIALOG_PROGRESS=4;
 private static final int DIALOG_SINGLE_CHOICE=5;
 private static final int DIALOG_MULTIPLE_CHOICE=6;
 private static final int DIALOG_TEXT_ENTRY=7;
 private static final int DIALOG_MULTIPLE_CHOICE_CURSOR=8;

//用于进度条
 private static final int MAX_PROGRESS=100;
 private ProgressDialog mprogressdialog;
 private int mprogress;
 private Handler mhandler;
 
 
    @Override
 protected Dialog onCreateDialog(int id) {
     switch(id)
     {
        case DIALOG__YES_NO_MESSAGE:

       
         return new AlertDialog.Builder(MainActivity.this)
                       .setIcon(R.drawable.alert_dialog_icon)
                      .setTitle(R.string.title)
                      .setPositiveButton(R.string.OK, new OnClickListener() {
        
        @Override
        public void onClick(DialogInterface dialog, int which) {
         
        }
       }).setNegativeButton("cancel", new OnClickListener() {
        
        @Override
        public void onClick(DialogInterface dialog, int which) {
         
        }
       })
       .create();
        case DIALOG__YES_NO_LONG_MESSAGE:
         return new AlertDialog.Builder(MainActivity.this)
                  .setIcon(R.drawable.alert_dialog_icon)
                  .setTitle(R.string.alert_dialog_two_buttons_msg)
                  .setMessage(R.string.alert_dialog_two_buttons2_msg)
                  .setPositiveButton(R.string.OK, new OnClickListener() {
       
       @Override
       public void onClick(DialogInterface dialog, int which) {
        
       }
      })
      .setNeutralButton(R.string.alert_dialog_something, new OnClickListener() {
       
       @Override
       public void onClick(DialogInterface dialog, int which) {
        // TODO Auto-generated method stub
        
       }
      })
      .setNegativeButton(R.string.alert_dialog_cancel, new OnClickListener() {
       
       @Override
       public void onClick(DialogInterface dialog, int which) {
        // TODO Auto-generated method stub
        
       }
      })
      .create();
        case DIALOG_LIST:
         return new AlertDialog.Builder(MainActivity.this)
                    .setTitle(R.string.select_dialog)
                    .setItems(R.array.select_dialog_items, new OnClickListener() {
       
       @Override
       public void onClick(DialogInterface dialog, int which) {
         /* User clicked so do some stuff */
        String [] items = getResources().getStringArray(R.array.select_dialog_items);
        new AlertDialog.Builder(MainActivity.this)
        .setMessage("你选择的是:"+which+","+items[which])
        .show();
       }
      })
      .create();
        case DIALOG_PROGRESS:
         mprogressdialog=new ProgressDialog(MainActivity.this);
         mprogressdialog.setIcon(R.drawable.alert_dialog_icon);
         mprogressdialog.setTitle(R.string.select_dialog);
         mprogressdialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
         mprogressdialog.setMax(MAX_PROGRESS);
         mprogressdialog.setButton(getText(R.string.alert_dialog_hide), new OnClickListener() {
    
    @Override
    public void onClick(DialogInterface dialog, int which) {
     /* User clicked Yes so do some stuff */
    }
   });
         mprogressdialog.setButton2(getText(R.string.alert_dialog_cancel), new OnClickListener() {
    
    @Override
    public void onClick(DialogInterface dialog, int which) {
     /* User clicked No so do some stuff */
     
    }
   });
         return mprogressdialog;
        case DIALOG_SINGLE_CHOICE:
         return new AlertDialog.Builder(MainActivity.this)
                    .setIcon(R.drawable.alert_dialog_icon)
                    .setTitle(R.string.alert_dialog_single_choice)
                    .setSingleChoiceItems(R.array.select_dialog_items2, 0, new OnClickListener() {
       
       @Override
       public void onClick(DialogInterface dialog, int which) {
        // TODO Auto-generated method stub
        
       }
      })
      .setPositiveButton(R.string.alert_dialog_ok, new OnClickListener() {
       
       @Override
       public void onClick(DialogInterface dialog, int which) {
        // TODO Auto-generated method stub
        
       }
      })
      .setNegativeButton(R.string.alert_dialog_cancel, new OnClickListener() {
       
       @Override
       public void onClick(DialogInterface dialog, int which) {
        // TODO Auto-generated method stub
        
       }
      })
      .create();
        case DIALOG_MULTIPLE_CHOICE:
         return new AlertDialog.Builder(MainActivity.this)
                    .setIcon(R.drawable.alert_dialog_icon)
                    .setTitle(R.string.alert_dialog_multi_choice)
                    .setMultiChoiceItems(R.array.select_dialog_items3,
                       new boolean[]{false,false,false,true,true,true,true}, new OnMultiChoiceClickListener() {
         
         public void onClick(DialogInterface dialog, int which, boolean isChecked) {
          // TODO Auto-generated method stub
          
         }
        })
        .setPositiveButton(R.string.alert_dialog_ok, new OnClickListener() {
         
         @Override
         public void onClick(DialogInterface dialog, int which) {
          // TODO Auto-generated method stub
          
         }
        })
        .setNegativeButton(R.string.alert_dialog_cancel, new OnClickListener() {
         
         @Override
         public void onClick(DialogInterface dialog, int which) {
          // TODO Auto-generated method stub
          
         }
        })
        .create();
        case DIALOG_MULTIPLE_CHOICE_CURSOR:
         String[] projetion = new String[]{
       Contacts.People._ID,
       Contacts.People.NAME,
       Contacts.People.SEND_TO_VOICEMAIL
         };
         Cursor cursor= managedQuery(Contacts.People.CONTENT_URI, projetion, null, null, null);
         return new AlertDialog.Builder(MainActivity.this)
                   .setIcon(R.drawable.alert_dialog_icon)
                   .setTitle(R.string.alert_dialog_multi_choice_cursor)
                   .setMultiChoiceItems(cursor,
                                Contacts.People.SEND_TO_VOICEMAIL,
                                Contacts.People.NAME,
                                new OnMultiChoiceClickListener() {
            
            @Override
            public void onClick
            (DialogInterface dialog, int which, boolean isChecked) {
             Toast.makeText(MainActivity.this,
                                                "Readonly Demo Only - Data will not be updated",
                                                Toast.LENGTH_SHORT).show();
            }
           })
           .create();
        case DIALOG_TEXT_ENTRY:
         LayoutInflater factory = LayoutInflater.from(this);
         final View textEntryView = factory.inflate(R.layout.alert_dialog_text_entry, null);
         return new AlertDialog.Builder(MainActivity.this)
                .setIcon(R.drawable.alert_dialog_icon)
                .setTitle(R.string.alert_dialog_text_entry)
                .setView(textEntryView)
                .setPositiveButton(R.string.alert_dialog_ok, new OnClickListener() {
      
      @Override
      public void onClick(DialogInterface dialog, int which) {
       // TODO Auto-generated method stub
       
      }
     })
     .setNegativeButton(R.string.alert_dialog_cancel, new OnClickListener() {
      
      @Override
      public void onClick(DialogInterface dialog, int which) {
       // TODO Auto-generated method stub
       
      }
     })
     .create();
        
           
        
      
     }
      return null;
 }


 @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        Button twoButtonsTitle =(Button)findViewById(R.id.two_buttons);
        twoButtonsTitle.setOnClickListener(new View.OnClickListener() {
   
   @Override
   public void onClick(View v) {
    showDialog(DIALOG__YES_NO_MESSAGE);
   }
  });
        Button twoButtons2Title =(Button)findViewById(R.id.two_buttons2);
        twoButtons2Title.setOnClickListener(new View.OnClickListener() {
   
   @Override
   public void onClick(View v) {
    showDialog(DIALOG__YES_NO_LONG_MESSAGE);
   }
  });
       
        Button selectButton =(Button)findViewById(R.id.select_button);
        selectButton.setOnClickListener(new View.OnClickListener() {
   
   @Override
   public void onClick(View v) {
    showDialog(DIALOG_LIST);
   }
  });
        Button progressButton =(Button)findViewById(R.id.progress_button);
        progressButton.setOnClickListener(new View.OnClickListener() {
   
   @Override
   public void onClick(View v) {
      showDialog(DIALOG_PROGRESS);
      mprogress = 0;
      mprogressdialog.setProgress(0);
      mhandler.sendEmptyMessage(0);
 
   }
  });
        Button radioButton =(Button)findViewById(R.id.radio_button);
        radioButton.setOnClickListener(new View.OnClickListener() {
   
   @Override
   public void onClick(View v) {
    showDialog(DIALOG_SINGLE_CHOICE);
   }
  });
        Button multipeButton =(Button)findViewById(R.id.checkbox_button);
        multipeButton.setOnClickListener(new View.OnClickListener() {
   
   @Override
   public void onClick(View v) {
    showDialog(DIALOG_MULTIPLE_CHOICE);
   }
  });
       
        Button checkButton =(Button)findViewById(R.id.checkbox_button2);
        checkButton.setOnClickListener(new View.OnClickListener() {
   
   @Override
   public void onClick(View v) {
    showDialog(DIALOG_MULTIPLE_CHOICE_CURSOR);
   }
  });
        Button textEntry =(Button)findViewById(R.id.text_entry_button);
        textEntry.setOnClickListener(new View.OnClickListener() {
   
   @Override
   public void onClick(View v) {
    showDialog(DIALOG_TEXT_ENTRY);
   }
  });
       
       
        mhandler=new Handler(){

   @Override
   public void handleMessage(Message msg) {
    super.handleMessage(msg);
    if(mprogress >= MAX_PROGRESS){
     mprogressdialog.dismiss();
    }
    else
    {
     
       mprogress++;
       mprogressdialog.incrementProgressBy(1);
       mhandler.sendEmptyMessageDelayed(0, 100);
    }
   }
         
        };
       
       
       
    }
}

dialog看起是没技术含量。但是只有把基础的东西熟悉了,在你以后的程序才能清楚调用了。谢谢。有不懂地方可以留言拍砖。(因为没有加上注释,但是都是基本的东西,所以可以很容易看的懂,相信你!!!)

posted @ 2011-03-28 13:21  邓伟平  阅读(685)  评论(0编辑  收藏  举报