浅析对话框AlertDialog

直接上代码,大神勿喷。


布局简单,三个按钮:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.ershisi.MainActivity"
    android:orientation="vertical" >
    
    <Button
        android:id="@+id/liebiao"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text = "带列表的对话框"
        android:fontFamily="楷体"/>
    
    <Button 
        android:id="@+id/danxuan"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="单选对话框"/>
    
    <Button 
        android:id="@+id/duoxuan"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="多选按钮"/>
    
    </LinearLayout>



主函数

package com.example.ershisi;

import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.test.IsolatedContext;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnMultiChoiceClickListener;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.Toast;
import android.os.Build;

public class MainActivity extends ActionBarActivity {

	AlertDialog.Builder build = null ;
	AlertDialog alert = null ;
	boolean checkItems[] ;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		Button liebiao = (Button) findViewById(R.id.liebiao) ;
		Button danxuan = (Button) findViewById(R.id.danxuan) ;
		Button duoxuan  = (Button) findViewById(R.id.duoxuan) ;
		/*alert = new AlertDialog.Builder(MainActivity.this).create() ;
		alert.setIcon(R.drawable.icon);
		alert.setMessage("这是一个中立的按钮");
		alert.setTitle("按钮学习");
		alert.setButton(DialogInterface.BUTTON_NEGATIVE,"确定", new DialogInterface.OnClickListener() {
			
			@Override
			public void onClick(DialogInterface dialog, int which) {
				// TODO Auto-generated method stub
				
				
			}
		});
     alert.setButton(DialogInterface.BUTTON_POSITIVE,"取消", new DialogInterface.OnClickListener() {
			
			@Override
			public void onClick(DialogInterface dialog, int which) {
				// TODO Auto-generated method stub
				
				
			}
		});
		alert.show();*/
		
		liebiao.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				final String[] items = {"苹果","橘子","香蕉","草莓"} ;
				build = new AlertDialog.Builder(MainActivity.this) ;
				build.setIcon(R.drawable.icon) ;
				build.setTitle("选择你喜欢的水果") ;
				build.setItems(items, new DialogInterface.OnClickListener() {
					
					@Override
					public void onClick(DialogInterface dialog, int which) {
						// TODO Auto-generated method stub
						Toast.makeText(getApplicationContext(), "你选择的是"+items[which],Toast.LENGTH_LONG).show(); ;
						
					}
				}) ;
				alert = build.create() ;
				alert.show();
				
			}
		});
		
		
		danxuan.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				final String[] fruits = {"苹果","橘子","香蕉","草莓"} ;
				build = new AlertDialog.Builder(MainActivity.this) ;
				build.setIcon(R.drawable.icon) ;
				build.setTitle("只能选一个喜欢的水果偶") ;
				build.setSingleChoiceItems(fruits, 0, new DialogInterface.OnClickListener() {
					
					@Override
					public void onClick(DialogInterface dialog, int which) {
						// TODO Auto-generated method stub
						Toast.makeText(getApplicationContext(), "你选择的是"+fruits[which], Toast.LENGTH_LONG).show(); 
						
					}
				}) ;
				alert = build.create() ;
				alert.show();
				
			}
		});
		
		duoxuan.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				checkItems = new boolean[]{false,false,false} ;
				final String[] menu = {"大肉面条","辣子鸡","菜煎饼"} ;
				build = new AlertDialog.Builder(MainActivity.this) ;
				build.setIcon(R.drawable.icon) ;
				build.setMultiChoiceItems(menu, checkItems, new OnMultiChoiceClickListener() {
					
					@Override
					public void onClick(DialogInterface dialog, int which, boolean isChecked) {
						// TODO Auto-generated method stub
						checkItems[which] = isChecked ;
						
					}
				}) ;
				build.setPositiveButton("确定", new DialogInterface.OnClickListener() {
					
					@Override
					public void onClick(DialogInterface dialog, int which) {
						// TODO Auto-generated method stub
						String res = "" ;
						for(int i = 0 ; i<checkItems.length ;i++)  {
							if(checkItems[i])
								res+=menu[i] ;
						}
						
						Toast.makeText(getApplicationContext(), "你选择了"+res, Toast.LENGTH_LONG).show() ;
						
					}
					
				}) ;
				alert = build.create() ;
				alert.show();
				
			}
		
		});
		

		if (savedInstanceState == null) {
			getSupportFragmentManager().beginTransaction()
					.add(R.id.container, new PlaceholderFragment()).commit();
		}
	}

	@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);
	}

	/**
	 * A placeholder fragment containing a simple view.
	 */
	public static class PlaceholderFragment extends Fragment {

		public PlaceholderFragment() {
		}

	}

}




原文地址:https://www.cnblogs.com/emoji/p/4436838.html