基本的微调器示例

时间:2012-10-27 06:17:02

标签: android android-spinner

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="fill_parent"
>
    <Spinner
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:id="@+id/spin"
        android:entries="@array/num"
    />
    <Button
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:id="@+id/btn"
        android:text="Click ME"
        android:gravity="center"
    />
    <TextView  
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" 
        android:id="@+id/txtv"
    />
</LinearLayout>

Spin.java

package com.and.spin;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.TextView;

public class spin extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        final TextView tv=(TextView)findViewById(R.id.txtv);

        Button b=(Button)findViewById(R.id.btn);
        final Spinner s=(Spinner)findViewById(R.id.spin);

        b.setOnClickListener(new OnClickListener() {                            
            public void onClick(View arg0) {
                String spin=s.toString();
                tv.setText(spin);
            }
        });
    }
}

在这个程序中,我正在尝试显示从Spinner到TextView的所选选项。但输出dsiplays android.widget.Spinner@44c0d7f8

我希望输出如(1,2,3,4或5)作为在Spinner中选择的选项而不是android.widget.Spinner@44c0d7f8

4 个答案:

答案 0 :(得分:7)

b.setOnClickListener(new OnClickListener()
    {

        public void onClick(View arg0)
        {
            String spin=s.getSelectedItem().toString();
                        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
            tv.setText(spin);
        }
    });

答案 1 :(得分:4)

您不需要共享首选项来在微调器中加载值。你只需要在string.xml文件中声明数组并加载它。我给你我的代码。只是使用它。: -

STEP-1: -

在string.xml中声明spinner的数组(res-&gt; values-&gt; strings.xml): -

<string-array name="country_array">
    <item>Greece</item>
    <item>United Kingdom</item>
    <item>Italy</item>
    <item>France</item>
    <item>Germany</item>
    <item>Turkey</item>
    <item>Poland</item>
    <item>India</item>
</string-array>

第2步: -

在布局xml文件中声明Spinner小部件

<Spinner
     android:id="@+id/spinCountry"
     android:layout_width="match_parent"
     android:layout_height="wrap_content"
     android:layout_marginTop="5dp"
     android:paddingLeft="8dp"
     android:popupBackground="@android:color/white"
     android:scrollbars="none"
     android:spinnerMode="dropdown" />

第3步: -

在您的活动中声明微调器

Spinner spinCountry;
spinCountry= (Spinner) findViewById(R.id.spinCountry);//fetch the spinner from layout file
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
            android.R.layout.simple_spinner_item, getResources()
                    .getStringArray(R.array.country_array));//setting the country_array to spinner
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinCountry.setAdapter(adapter);
//if you want to set any action you can do in this listener
spinCountry.setOnItemSelectedListener(new OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> arg0, View arg1,
                int position, long id) {
        }

        @Override
        public void onNothingSelected(AdapterView<?> arg0) {
        }
    });

答案 2 :(得分:0)

您正在调整spinner上的toString()方法以获取错误的选择。您需要调用getSelectedItemPosition()方法来获取选择。

答案 3 :(得分:0)

    spinCountry.setOnItemSelectedListener(new OnItemSelectedListener() {
        @Override
         public void onItemSelected(AdapterView<?> parentView,
                 View selectedItemView, int position, long id) {
             try {

                  String select_item =parentView.getItemAtPosition(position).toString();
                 } 
           catch (Exception e) {

            }
        }
        @Override
        public void onNothingSelected(AdapterView<?> parentView) {

        }

    });