Android - How to display a spinner with value but display a different one

筅森魡賤 提交于 2019-12-04 03:50:13

问题


For my Android app, I need to use a spinner which displays a XML tree. This tree is necesarry for the user to understand the level of the value he can select.

Here is a picture of what I have right now:

As you can see, I've put some little '>' and some spaces to simulate the tree. What I want now, is when the user select an entry, to display the value without those symbols.

Today, the spinner displays the value like that:

THe word is " > Supermarket" but what I want is "Supermarket", and only that. Is there a way to modifiy the selected value displayed by the spinner but not the list?

I hope you'll be able to help me.

Regards.

V.


回答1:


you can do this with the idea that

make your spinner invisible put an textView behind the spinner set TextView text to spinner selected item with

textView.settext(spinner.getSelectedItem().replace(">","").trim()));

dont forget to put spinner over the textView otherwise you will not get spinner click event




回答2:


Here is the code I used if someone needs it:

First, replace your spinner by the folowing portion of code:

<RelativeLayout
    android:layout_width="190dp"
    android:layout_height="40dp"
    android:layout_marginRight="5dp">    

    <Spinner 
        android:id="@+id/spinner"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" 
        android:background="@drawable/custom_spinner"
        android:visibility="invisible" />

    <Button
        android:id="@+id/fake_btn"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:background="@drawable/custom_spinner" />

    <TextView
        android:id="@+id/fake_text"
        android:layout_width="140dp"
        android:layout_height="40dp"
        android:layout_marginLeft="10dp"
        android:gravity="center_vertical"
        android:textColor="#000000" />

</RelativeLayout>

Create a new XML file in your drawable folder named custom_spinner.xml and puts that code:

<?xml version="1.0" encoding="utf-8"?>

<selector xmlns:android="http://schemas.android.com/apk/res/android">

     <item
         android:state_focused="true" 
         android:state_pressed="false" 
         android:drawable="@drawable/spinner_pressed" />

    <item
        android:state_focused="true" 
        android:state_pressed="true"
        android:drawable="@drawable/spinner_pressed" />

    <item
        android:state_focused="false" 
        android:state_pressed="true"
        android:drawable="@drawable/spinner_pressed" />

    <item
        android:drawable="@drawable/spinner_normal" />

</selector>

Download those two pictures and add them into your drawable folder (spinner_pressed.9.png and spinner_normal.9.png):

In your activity, add the following variables:

private Spinner spinner;
private Button fake_btn;
private TextView fake_text;

In the onCreate method, add:

spinner = (Spinner) findViewById(R.id.spinner);
fake_btn = (Button) findViewById(R.id.fake_btn);
fake_text = (TextView) findViewById(R.id.fake_text);

fake_btn.setOnClickListener(new OnClickListener() {
    public void onClick(View v)
    {
        spinner.performClick();
    }
});

spinner_manual.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
    public void onItemSelected(AdapterView parentView, View childView, int position, long id)
    {           
        String value = (String) spinner.getItemAtPosition(position);
        fake_text.setText(value);
    }
    public void onNothingSelected(AdapterView parentView) {}  
});

There you are, it should work.



来源:https://stackoverflow.com/questions/9972771/android-how-to-display-a-spinner-with-value-but-display-a-different-one

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!