Android学习第十一天----AlertDialog_2

AlertDialog应用2

xml中添加一个按钮就可以 

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:layout_marginBottom="72dp"
        android:text="点我" />

</RelativeLayout>

项目要求:点击按钮之后显示出一个对话框,对话框里面有内容可供选择,点击确定之后再屏幕中显示所选中的项目的内容;

在values中创建一个array.xml文件

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string-array name="info">
        <item>厦门1</item>
        <item>厦门2</item>
        <item>厦门3</item>
        <item>厦门4</item>
        
        
    </string-array>
</resources>

java代码中,为了区别单选还是多选框,其中注释掉的是单选框,没有注释的显示的是多选框,点击确定按钮之后就可以在屏幕下方显示

package com.example.alertdialog;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.DialogInterface.OnMultiChoiceClickListener;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends Activity implements OnClickListener
{     
    private AlertDialog.Builder mBuilder ;
    private Button mButton;
    private String str;
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        mButton = (Button)findViewById(R.id.button1);
        
        mButton.setOnClickListener(new View.OnClickListener()
        {
            
            @Override
            public void onClick(View v)
            {
                mBuilder = new AlertDialog.Builder(MainActivity.this);
                final String [] info = getResources().getStringArray(R.array.info);
                final boolean flag[] = {false,false,false,false};
                
                mBuilder.setTitle("请选择以下的任何一项:");
                
                mBuilder.setMultiChoiceItems(info, flag, new OnMultiChoiceClickListener()
                {
                    
                    @Override
                    public void onClick(DialogInterface dialog, int which, boolean isChecked)
                    {
                        str="";
                        for (int i = 0; i < flag.length; i++)
                        {
                            if(flag[i])
                            {
                                str+=info[i];
                            }
                        }
                        
                    }
                });
                
                mBuilder.setPositiveButton("确定", MainActivity.this);
                mBuilder.setNegativeButton("取消", MainActivity.this);
                mBuilder.setNeutralButton("退出", MainActivity.this);
//                mBuilder.setSingleChoiceItems(info,0, new OnClickListener()
//                {
//                    
//                    @Override
//                    public void onClick(DialogInterface dialog, int which)
//                    {
//                        // TODO Auto-generated method stub
//                        
//                    }
//                });
                
                mBuilder.create();
                mBuilder.show();
                
            }
        });
        
    }
    @Override
    public void onClick(DialogInterface dialog, int which)
    {
        switch (which)
        {
            case AlertDialog.BUTTON_NEGATIVE:
            
                break;
            case AlertDialog.BUTTON_NEUTRAL:
                        
                break;
            case AlertDialog.BUTTON_POSITIVE:
                Toast.makeText(MainActivity.this, str, Toast.LENGTH_SHORT).show();
                break;
        
        }
        
    }
}

 

posted @ 2013-03-18 21:26  小三小山  阅读(174)  评论(0编辑  收藏  举报