android dialog

 /**
 * @Title MenuTest.java
 * @package     com.example.standardview
 * @since  
 * @version     1.0.0 
 * @author      Vic Lee
 * @date        Aug 26, 2016-2:10:07 PM
 */
package com.example.standardview;

import com.example.standardview.R.menu;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.ListActivity;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.AdapterContextMenuInfo;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;


//public class MenuTest extends Activity {
public class MenuTest extends ListActivity {
    ArrayAdapter<String> arrayAdapter;
    //private String[] mItemStrings=new String[]{"hello1","hello2","hello3"};
    private String[] mItemStrings=new String[]{"hello1 Dialog","hello2 Dialog","hello3 Dialog"};
    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
        // TODO Auto-generated method stub
        super.onListItemClick(l, v, position, id);
        if (position==position) 
        {
            AlertDialog alertDialog= 
                    new AlertDialog.Builder(MenuTest.this)
                     .setTitle("Hello Dialog")
                     .setIcon(android.R.drawable.alert_dark_frame)
                     .setMessage("Do you want to exit?")
                     .setPositiveButton("Yes", new  OnClickListener() {
                        
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            // TODO Auto-generated method stub
                            MenuTest.this.finish();
                            
                        }
                    })
                    .setNegativeButton("No", new  OnClickListener() {
                        
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            // TODO Auto-generated method stub
                            dialog.cancel();
                        }
                    }).create();
            
            alertDialog.show();
        }
        
        
    }
 @Override
    protected void onCreate(Bundle savedInstanceState) {
    
        super.onCreate(savedInstanceState);
        //setContentView(R.layout.activity_main);
         arrayAdapter=new  ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, mItemStrings);
        setListAdapter(arrayAdapter);
     
    }

}

效果:

 

===============

    private String[] mItemStrings=new String[]{"hello1 Dialog","hello2 Dialog","hello3 Dialog"};
    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
        // TODO Auto-generated method stub
        super.onListItemClick(l, v, position, id);
        if (position==position) 
        {
            AlertDialog alertDialog= 
                    new AlertDialog.Builder(MenuTest.this)
                 .setTitle("Hello Dialog")
//                     .setIcon(android.R.drawable.alert_dark_frame)
//                     .setMessage("Do you want to exit?")
                     .setSingleChoiceItems(mItemStrings, 1, new  OnClickListener() {
                        
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            Toast.makeText(MenuTest.this, ""+which, 3000).show();
                            
                        }
                    })
                     .setPositiveButton("Yes", new  OnClickListener() {
                        
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            // TODO Auto-generated method stub
                            MenuTest.this.finish();
                            
                        }
                    })
                    .setNegativeButton("No", new  OnClickListener() {
                        
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            // TODO Auto-generated method stub
                            dialog.cancel();
                        }
                    }).create();
            
            alertDialog.show();
        }
        
        
    }

效果:

posted @ 2016-08-26 18:12  每天进步一点点!  阅读(379)  评论(0编辑  收藏  举报