【问题标题】:Android decrease or increase the selection position in SpinnerAndroid 减少或增加 Spinner 中的选择位置
【发布时间】:2017-05-30 11:17:21
【问题描述】:

在我的 android 应用程序中,我有一个带有项目的 Spinner,例如:(1,2) (3,4) .. (71,72)。 当我从该 Spinner 中选择一个项目时,我会得到例如。 (67,68)在那里。 现在我想以编程方式将所选项目更改为 (65,66) 或 (69,70) 单击一个或另一个 textView。 也就是我想通过点击两个textView来减少或增加String Array中的选择位置,像这样:

' >' .

为此,我可以使用 setSelection(position)。

我的应用程序可以通过使用 getSelectedItemPosition 知道到目前为止选择了哪个项目。

getSelectedItemPosition 返回一个整数。所以例如。如果我减小该整数并使用它运行 setSelection(position),则 SelectedItem 将更改为该数组中的前一项。

因此,要使其工作,我必须将整数添加到“位置变量”, 然后将该变量减 1 并使用该值运行 setSelection(position_variable)。

程序必须验证位置是 0 还是最大值才不会超出/低于界限。

如何在 Java 中实现这一点?我正在 Android Studio 中开发我的应用程序。

在 activity_main.xml 中,我有以下内容:

        <TextView
            android:id="@+id/forward"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@id/ora_keltezese"
            android:layout_marginStart="5dp"
            android:layout_marginTop="5dp"
            android:layout_toEndOf="@id/orarend"
            android:text="@string/forward"
            android:textSize="20sp"
            android:onClick="increaseSpinnerItemPosition"/>

        <TextView
            android:id="@+id/backward"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@id/ora_keltezese"
            android:layout_marginStart="5dp"
            android:layout_marginTop="5dp"
            android:text="@string/backward"
            android:textSize="20sp"
            android:onClick="decreaseSpinnerItemPosition"/>

在 MainActivity.java 中,我有以下内容:

public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        Spinner spinner = (Spinner) findViewById(R.id.orarend);
        //Create an ArrayAdapter using the string array and a default spinner layout
        ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
                R.array.TanitasiNapok, android.R.layout.simple_spinner_item);
        // Specify the layout to use when the list of choices appears
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        // Apply the adapter to the spinner
        spinner.setAdapter(adapter);


        Spinner spinner2 = (Spinner) findViewById(R.id.blokkora);
        //Create an ArrayAdapter using the string array and a default spinner layout
        ArrayAdapter<CharSequence> adapter2 = ArrayAdapter.createFromResource(this,
                R.array.BlokkoraSorszama, android.R.layout.simple_spinner_item);
        // Specify the layout to use when the list of choices appears
        adapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        // Apply the adapter to the spinner
        spinner2.setAdapter(adapter2);


        Spinner spinner3 = (Spinner) findViewById(R.id.osztaly);
        //Create an ArrayAdapter using the string array and a default spinner layout
        ArrayAdapter<CharSequence> adapter3 = ArrayAdapter.createFromResource(this,
                R.array.OsztalyKivalasztasa, android.R.layout.simple_spinner_item);
        // Specify the layout to use when the list of choices appears
        adapter3.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        // Apply the adapter to the spinner
        spinner3.setAdapter(adapter3);

        }

    public void decreaseSpinnerItemPosition (View v) {
        //What to put here?
    }

    public void increaseSpinnerItemPosition (View v) {
        //What to put here?
    }
}

那么如何在我的应用程序中实现 position_variable? 我怎样才能实现我的目标?

【问题讨论】:

    标签: android spinner


    【解决方案1】:

    你可以试试这样的:

    public void decreaseSpinnerItemPosition (View v) {
        spinner3.setSelection(Math.max(0, spinner3.getSelectedItemPosition() - 1));
    }
    
    public void increaseSpinnerItemPosition (View v) {
       spinner3.setSelection(Math.min(getResources().getStringArray(R.array.OsztalyKivalasztasa).length - 1, spinner3.getSelectedItemPosition() + 1));
    }
    

    Math.max(x, y) 返回更大数量的 x 和 y。因此,如果 x 为 1,y 为 2,这将返回 2。我们在这里使用它以不低于 0。因此,如果 spinner3.getSelectedItemPosition() - 1 小于(或等于)0,Math.max(0, spinner3.getSelectedItemPosition() - 1) 将返回 0。 与 Math.min 相同,但在这种情况下,我们检查上限值(取决于数组的大小)。 你也可以输入

    if(spinner3.getSelectedItemPosition() - 1 <= 0) {
        spinner3.setSelection(0);
    } else {
        spinner3.setSelection(spinner3.getSelectedItemPosition() - 1);
    }
    

    应该是一样的。

    别忘了全局定义你的 spinner3:

    public class MainActivity extends AppCompatActivity {
    
        private Spinner spinner3;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
        [...]
        spinner3 = (Spinner) findViewById(R.id.osztaly);
        [...]
        }
    }
    

    【讨论】:

    • 哇,它有效!实际上我正在减少或增加 spinner2。所以我相应地修改了你的命令。只是不明白我做了什么?
    • 我编辑了我的答案并简化了增加和减少的方法。
    • 非常感谢您的帮助!!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-03
    相关资源
    最近更新 更多