且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在微调器中添加默认文本?

更新时间:2022-11-27 16:48:30

对于您的情况,我的建议是使用Button最初设置文本并设置 PopupWindow 将该Button设置为该PopUpWindow的锚点.在该PopUpWindow中放置一个ListView,然后在OnItemClick中使用

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项后的屏幕截图