12

即使选择了任何其他列表,我也想设置微调器以显示名称国家。我使用了一个 textview 来显示 Spinner List 的选定项目。当我使用 setselection 方法将微调器标题设置为 Country 时,EditText 最终也会发生变化。我浏览了有关此问题的各种主题,但找不到合适的回复

我在下面附上我的代码

MainActivity.java

package com.example.spinner;

import android.os.Bundle;
import android.app.Activity;
import android.database.Cursor;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.TextView;

public class MainActivity extends Activity {

    Spinner sp;
    TextView t;
    String[] country;
    int sp_position;
    String selected;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        String myString = "Country"; 

        sp = (Spinner)findViewById(R.id.spinner1);
        t = (TextView)findViewById(R.id.textView1);
        country = getResources().getStringArray(R.array.spinner);
        ArrayAdapter<String> ad = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_spinner_dropdown_item,country);   
        sp_position = ad.getPosition(myString);
        sp.setAdapter(ad);
        ad.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

        sp.setOnItemSelectedListener(new OnItemSelectedListener() {

            @Override
            public void onItemSelected(AdapterView<?> arg0, View arg1,
                    int arg2, long arg3) {
                selected = sp.getSelectedItem().toString();
                System.out.println(selected);
                setid();

            }

            private void setid() {
                sp.setSelection(sp_position);


            }

            @Override
            public void onNothingSelected(AdapterView<?> arg0) {
                // TODO Auto-generated method stub

            }
        });

        t.setText(selected);

    }




    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

}

我的 XML 文件

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="48dp"
        android:layout_marginTop="69dp"
        android:maxLines="4"
        android:hint="Address" />

    <Spinner
        android:id="@+id/spinner1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignRight="@+id/textView1"
        android:layout_below="@+id/textView1"
        android:layout_marginTop="72dp" 
        />

</RelativeLayout>

我的 STRINGS.XML

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

    <string name="app_name">Spinner</string>
    <string name="action_settings">Settings</string>
    <string name="hello_world">Hello world!</string>
    <string-array name="spinner">
        <item>Country</item>
        <item>India</item>
        <item>Russia</item>
        <item>USA</item>
        <item>France</item>
        <item>United Kingdom</item>
    </string-array>
<string name ="Country">Country</string>
</resources>

我的要求是在微调器中选择的任何内容,Textview 应显示所选项目,但微调器应始终显示第一个项目,或者在我的情况下为 Country

4

11 回答 11

13

使用此代码

宣言

String selected, spinner_item;

微调器代码

sp.setOnItemSelectedListener(new OnItemSelectedListener() {

        @Override
        public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
            selected = sp.getSelectedItem().toString();
            if (!selected.equals("Country"))
                spinner_item = selected;
            System.out.println(selected);

            setid();
        }

        private void setid() {
            sp.setSelection(sp_position);
            t.setText(spinner_item);
        }

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

        }
    });
于 2013-11-13T05:15:41.107 回答
9
spinner.setPrompt("Pick One");
于 2014-01-19T05:27:31.980 回答
5

为此,您只需要 3 行代码:

(1) 声明一个全局变量,用于保存和加载微调器项 ID。全局,我的意思是在“public class MainActivity extends Activity {”之后声明它。

int mySpinner_selectedId; 

(2) 然后,在用户做出选择后添加以下代码。一个好的位置是与您的微调器一起出现的按钮。

mySpinner_selectedId = mySpinner.getSelectedItemPosition();

(3) 最后,在“mySpinner.setAdapter(adapter);”之后添加如下代码 加载最后选择的项目。

mySpinner.setSelection(mySpinner_selectedId);

你很受欢迎。

于 2014-11-01T10:57:24.313 回答
3

在微调器中使用android:prompt="@string/select"......

于 2013-11-13T05:14:28.827 回答
2

这样做。将 textView setSelection 设置为微调器列表的第 0 位后

sp.setOnItemSelectedListener(new OnItemSelectedListener() {

    @Override
    public void onItemSelected(AdapterView<?> arg0, View arg1,
        int arg2, long arg3) {

        selected = sp.getSelectedItem().toString();
        System.out.println(selected);
        t.setText(selected);
        sp.setSelection(0);
    }
}
于 2013-11-13T05:16:10.477 回答
1

试试这个方法。

sp.setOnItemSelectedListener(new OnItemSelectedListener() {

    @Override
    public void onItemSelected(AdapterView<?> arg0, View arg1,
            int arg2, long arg3) {
        //selected = sp.getSelectedItem().toString();
        //System.out.println(selected);
        if(arg2!=0)
           t.setText(sp.getSelectedItem().toString());
        sp.setSelection(0);
    }
}

我希望这能帮到您。让我知道发生什么事。谢谢你。

于 2013-11-13T05:46:22.097 回答
1

尝试这个:

    public void onItemSelected(AdapterView<?> parent, View view, 
        int pos, long id) {

           spinner.setSelection(pos);

    }
于 2014-08-26T20:44:49.237 回答
0

尝试这个

Countryspinner.setOnItemSelectedListener(new OnItemSelectedListener() {

        public void onItemSelected(AdapterView<?> arg0, View arg1,
                int position, long arg3) {
            if (position == 0) {
                txtSpinner1.setHint("Select Country");
            } else {
                txtSpinner1.setText(CountryList.get(position));
            }

        }

        @Override
        public void onNothingSelected(AdapterView<?> arg0) {
            // TODO Auto-generated method stub
            txtSpinner1.setHint("Select Country");
        }

    });
于 2013-11-13T05:26:12.100 回答
0

onItemSelected方法中添加此代码sp.setSelection(0);

于 2013-11-13T05:40:28.580 回答
0

我以一种简单的方式做到了这一点

  1. 在适配器第一个或结束位置(在我的情况下为 end )添加默认文本并将此项目设置为默认选择。---- 同时获取默认选择位置,这样在 setOnItemSelectedListener() 中不会执行任何操作。

    taskName.add("one");
    taskName.add("two");
    taskName.add("select a task");
    ArrayAdapter<String> adapter = new ArrayAdapter<>(this, android.R.layout.simple_spinner_dropdown_item, taskName);
    binding.spnTask.setAdapter(adapter);
    binding.spnTask.setSelection(adapter.getPosition("select a task name"));
    final int defaultPosition = binding.spnTask.getSelectedItemPosition();
    
  2. 检查所选位置是默认文本位置

      binding.spnTask.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
    
            if (position == defaultPosition) {
                Toast.makeText(this, "No Action", Toast.LENGTH_SHORT).show();
            }else{  Toast.makeText(this, "Your Action" , Toast.LENGTH_SHORT).show();   }
        }
    
        @Override
        public void onNothingSelected(AdapterView<?> parent) {
    
        }
    });
    
于 2018-04-13T07:52:50.680 回答
0
mySpinner = (Spinner) findViewById(R.id.spinner);
        mySpinner.setPrompt("Categorie");
        final ArrayList<String> strDeviceArr = new ArrayList<>();

        mySpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                String valueSelected = mySpinner.getSelectedItem().toString();
                if(!valueSelected.equals("Categorie"))
                    Toast.makeText(MenuActivity.this, ""+valueSelected, Toast.LENGTH_SHORT).show();

            }

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

            }
        });
于 2021-01-05T17:29:49.560 回答