当前位置: 代码迷 >> Android >> android 用Button+dialog兑现spinner
  详细解决方案

android 用Button+dialog兑现spinner

热度:12   发布时间:2016-05-01 19:17:27.0
android 用Button+dialog实现spinner
先上图












代码如下:
package com.zhou.activity;import android.app.Activity;import android.app.AlertDialog;import android.content.DialogInterface;import android.os.Bundle;import android.view.View;import android.view.View.OnClickListener;import android.widget.Adapter;import android.widget.ArrayAdapter;import android.widget.Button;import android.widget.Spinner;import android.widget.Toast;public class MainActivity extends Activity {	Button button;    String[] citys={"北京市","上海市","天津市","重庆市","广州市","深圳市","厦门市","杭州市"};    int a = 0;    @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.main);        button = (Button) this.findViewById(R.id.button);               button.setOnClickListener(new OnClickListener() {            public void onClick(View v) {                new AlertDialog.Builder(MainActivity.this)                .setTitle("选择").setSingleChoiceItems(citys, a,new DialogInterface.OnClickListener() {                    public void onClick(DialogInterface dialog, int which) {                        button.setText(citys[which]);                        a = which;                        dialog.dismiss();                    }                }).create().show();                            }        });        }}


main.xml
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:orientation="vertical"    android:layout_width="fill_parent"    android:layout_height="wrap_content"    >             <Button             android:id="@+id/button"             android:layout_width="wrap_content"             android:layout_height="wrap_content"             android:text="请选择城市"             android:background="@drawable/btn_dropdown"             android:layout_weight="1"             ></Button></LinearLayout>

  相关解决方案