在我的Android应用程序中,我有一个带有Items的Spinner,如:(1,2)(3,4)..(71,72)。 当我从那个微调选择一个项目,然后我得到例如。 (67,68)在那里。 现在我想通过点击一个或另一个textView以编程方式将所选项目更改为(65,66)或 (69,70)。 也就是说,我想通过点击这两个textViews,这样来减少或增加在字符串数组中选择位置:Android减少或增加Spinner中的选择位置
“< <的SelectedItem >>”。
为此,我可以使用setSelection(position)。
我的应用程序可以通过使用getSelectedItemPosition知道到目前为止选择了哪个项目。
getSelectedItemPosition返回一个整数。所以例如。如果我减少该整数并运行setSelection(position),SelectedItem将更改为该数组中的前一项。
因此,为了这个工作,我必须将整数添加到'position_variable', 然后用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? 我该如何实现我的目标?
哇,它的作品!其实我正在减少或增加spinner2。所以我相应地修改了你的命令。只是不明白我做了什么? – PalCsanyi
我编辑了我的答案,并简化了增加和减少方法。 – Prexx
非常感谢您的帮助! – PalCsanyi