zoukankan      html  css  js  c++  java
  • Android 弹出选择窗口

    本来使用 SDK 版本 Android 4.2.2

    弹出选择窗口在Android 中十分常见的,掌握是有必要的,弹出窗口也很简单,下面看代码

     values下的 pd_list_item.xml 文件如下

    <?xml version="1.0" encoding="utf-8"?>
    <resources>
        <string-array name="pd_list_itemArray">
            <item>红色</item>
            <item>蓝色</item>
            <item>紫色</item>
        </string-array>
    </resources>

    布局文件非常简单 ,activity中一个Button,点击Button弹出窗口

    <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"
        android:paddingBottom="@dimen/activity_vertical_margin"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        tools:context=".MainActivity" >
    
        <Button
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="点我弹出窗口" />
    
    </RelativeLayout>

    下面是 java代码

    package com.zhoucj.messagedemo;
    
    import com.zhoucj.messagedemo.util.ContentUtil;
    
    import android.app.Activity;
    import android.app.AlertDialog;
    import android.content.DialogInterface;
    import android.content.res.Resources;
    import android.os.Bundle;
    import android.view.Menu;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;
    import android.widget.EditText;
    
    public class MainActivity extends Activity {
    
        private Button button;
        
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            
            button=(Button)findViewById(R.id.button1);
            
            button.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View arg0) {
                    dialog();
                }
            });
            
        }
        
        private void dialog()
        {
            new AlertDialog.Builder(MainActivity.this).
            setSingleChoiceItems(R.array.pd_list_itemArray, 0, new DialogInterface.OnClickListener() {
                
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    String st[]=getResources().getStringArray(R.array.pd_list_itemArray);
                    switch (which) {
                    
                    case 0:
                        ContentUtil.showToast(MainActivity.this, "您选择了  "+st[0]);
                        break;
    
                    case 1:
                        ContentUtil.showToast(MainActivity.this, "您选择了"+st[1]);
                        break;
                    case 2:
                        ContentUtil.showToast(MainActivity.this, "您选择了"+st[2]);
                        break;
                    }
                    dialog.dismiss();
                }
            }).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;
        }
    
    }

    运行之后的效果图如下

    是不是非常简单

    作者:zhoucj

    出处:http://www.cnblogs.com/zhoujian315/archive/2013/06/08/3127667.html

    欢迎转载,原创作品,转载请注明出处,谢谢合作

  • 相关阅读:
    Ansible配置管理Windows主机
    Docker中安装rabbitmq并启用插件
    解决Ubuntu不能直接root用户连接ssh
    Error in invoking target ‘agent nmhs’ of makefile ‘/home/dong/tools/oracle11g/product/11.2.0/dbhome_1/sysman/lib/ins_emagent.mk’
    Ubuntu16.04安装sqlserver-快速入门
    记录一次归档日志爆满事件
    Linux下Mysql忘记root密码
    Ubuntu16.04安装Rabbitmq
    Oracle查看执行计划
    Ubuntu16.04随笔
  • 原文地址:https://www.cnblogs.com/zhoujian315/p/3127667.html
Copyright © 2011-2022 走看看