如何在微调器中添加默认文本? [英] How to add default text in spinner?

查看:77
本文介绍了如何在微调器中添加默认文本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Android的新手,我知道可以使用Spinner,但是我想在Spinner中添加默认文本,并希望设置如下图所示的布局,任何人都可以帮我这个忙,

I am new to android,I know access of spinner but I want to add default text in spinner and want set lay out like below image,can any one help me with this,

    final String[] items = new String[] {"One", "Two", "Three"};
    final ArrayAdapter<String> adapter123 = new ArrayAdapter<String>(this,
            android.R.layout.simple_spinner_dropdown_item, items);

    sp3.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View w) {
              new AlertDialog.Builder(RegistrationForm.this)
              .setTitle("the prompt")
              .setAdapter(adapter123, new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {

                  dialog.dismiss();
                }
              }).create().show();
            }
    });

推荐答案

对于您的情况,我的建议是使用Button最初设置文本并设置

My suggestion for your case is use Button initially set text and set gravity (not layout_gravity) to left|center_vertical instead of opening an AlertDialog open a PopupWindow set that Button as anchor of that PopUpWindow. In that PopUpWindow place a ListView and in OnItemClick change text with selected value in that Button using setText(java.lang.CharSequence)

代码段

XML

<Button
    android:id="@+id/propertyBtn"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginRight="@dimen/dp_4"
    android:background="@drawable/property_btn_large"
    android:ellipsize="marquee"
    android:gravity="left|center_vertical"
    android:marqueeRepeatLimit="marquee_forever"
    android:minHeight="0dp"
    android:minWidth="0dp"
    android:onClick="showPropertyPopUp"
    android:paddingLeft="42dp"
    android:paddingRight="22dp"
    android:shadowColor="@android:color/white"
    android:shadowDx="1"
    android:shadowDy="1"
    android:shadowRadius="1"
    android:text="@string/select_property" />

在该按钮中单击以打开PopUpWindow的Java代码

Java code for opening PopUpWindow in that Button click

//don't forget to initialize that button in onCreate(...)
public void showPropertyPopUp(View v) {
        propertyList = dbHelper.getAllProperties();
        dbHelper.closeDB();

        if(propertyList != null && propertyList.size() > 0) {
            LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            View popUpView = inflater.inflate(R.layout.pop_up, null, false);
            Bitmap bitmap = BitmapFactory.decodeResource(getResources(),
                    R.drawable.select_dropdown);
            popupWindowProperty = new PopupWindow(popUpView, propertyBtn.getWidth(),
                    300, true);         
            popupWindowProperty.setContentView(popUpView);
            popupWindowProperty.setBackgroundDrawable(new BitmapDrawable(getResources(),
                    bitmap));
            popupWindowProperty.setOutsideTouchable(true);
            popupWindowProperty.setFocusable(true);
            popupWindowProperty.showAsDropDown(propertyBtn, 0, 0);
            ListView dropdownListView = (ListView) popUpView.
                    findViewById(R.id.dropdownListView);
            PropertyDropdownAdapter adapter = new PropertyDropdownAdapter(
                    AddIncomeActivity.this, 
                    R.layout.row_pop_up_list, propertyList);
            dropdownListView.setAdapter(adapter);
            dropdownListView.setOnItemClickListener(this);
        }   
    }

用于在OnItemClick

PropertyInfo addPropertyInfo = propertyList.get(position);
String propertyName = addPropertyInfo.getPropertyName();
propertyBtn.setText(propertyName);
popupWindowProperty.dismiss();

pop_up 布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <ListView
        android:id="@+id/dropdownListView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:cacheColorHint="@null"
        android:fadeScrollbars="false" >
    </ListView>

</LinearLayout>

点击该按钮时的屏幕截图

单击ListView项后的屏幕截图

这篇关于如何在微调器中添加默认文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆