英文:
Spinner already Selected Item Selection event
问题
场景:
我正在使用 Spinner 来选择日期范围,这个功能运行得很好。问题是,有一个选项叫做“自定义范围”,我需要在这个选项上打开一个自定义日期范围选择器。虽然第一次使用时它运行得很好。
问题是:
当用户再次点击它时,不会调用 onItemSelected(parent: AdapterView<*>?, view: View?, position: Int, id: Long)
方法(这是 Spinner 的功能)。但从用户的角度来看,这似乎是个错误。
有没有办法在已选中的 Spinner 项上获得任何选择事件?
英文:
Scenario:
I am using Spinner for date range selection, which is working perfectly. The issue is, there is an option "Custom Range" on which I have to open a custom date range picker. Although it is working fine for the first time.
The issue is:
When users click it again, it doesn't call onItemSelected(parent: AdapterView<*>?, view: View?, position: Int, id: Long)
(which is a functionality of Spinner). But from the user point of view, it's a bug.
Is there any way, we can get any selection event on an already selected item of Spinner?
答案1
得分: 4
要在已选择的Spinner
项上调用事件,您可以按照以下方式进行操作:
- 通过扩展
AppCompatSpinner
创建您自己的Spinner类:
import android.content.Context
import android.util.AttributeSet
import android.util.Log
class MySpinner(context: Context, attrs: AttributeSet?) : androidx.appcompat.widget.AppCompatSpinner(
context,
attrs
) {
var listener: OnItemSelectedListener? = null
override fun setSelection(position: Int) {
super.setSelection(position)
if (position == selectedItemPosition) {
listener!!.onItemSelected(this, selectedView, position, selectedItemId)
}
}
override fun setOnItemSelectedListener(listener: OnItemSelectedListener?) {
this.listener = listener
}
}
- 在您的布局中使用它:
<com.yourcompany.kotlintest.MySpinner
android:id="@+id/spinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
- 在创建布局时执行以下操作:
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val spinner = findViewById<MySpinner>(R.id.spinner)
spinner.adapter = ArrayAdapter(
this,
android.R.layout.simple_spinner_dropdown_item,
arrayListOf("One", "Two", "Three")
)
spinner.onItemSelectedListener = object :
AdapterView.OnItemSelectedListener {
override fun onItemSelected(
parent: AdapterView<*>,
view: View, position: Int, id: Long
) {
Log.d("MyTag", "Click item at pos $position")
}
override fun onNothingSelected(parent: AdapterView<*>) {
Log.d("MyTag", "Nothing selected")
}
}
}
}
现在,当您重新选择相同的项时,函数将被执行。
英文:
To call an event on the already selected item in Spinner
You can do it in this way:
- Create Your own Spinner class by extending
AppCompatSpinner
:
import android.content.Context
import android.util.AttributeSet
import android.util.Log
class MySpinner(context: Context, attrs: AttributeSet?) : androidx.appcompat.widget.AppCompatSpinner(
context,
attrs
)
{
var listener: OnItemSelectedListener? = null
override fun setSelection(position: Int)
{
super.setSelection(position)
if (position == selectedItemPosition)
{
listener!!.onItemSelected(this, selectedView, position, selectedItemId)
}
}
override fun setOnItemSelectedListener(listener: OnItemSelectedListener?)
{
this.listener = listener
}
}
- Use it in Your layout:
<com.yourcompany.kotlintest.MySpinner
android:id="@+id/spinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
- When creating a layout do this:
class MainActivity : AppCompatActivity()
{
override fun onCreate(savedInstanceState: Bundle?)
{
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val spinner = findViewById<MySpinner>(R.id.spinner)
spinner.adapter = ArrayAdapter(
this,
android.R.layout.simple_spinner_dropdown_item,
arrayListOf("One", "Two", "Three")
)
spinner.onItemSelectedListener = object :
AdapterView.OnItemSelectedListener
{
override fun onItemSelected(
parent: AdapterView<*>,
view: View, position: Int, id: Long
)
{
Log.d("MyTag", "Click item at pos $position")
}
override fun onNothingSelected(parent: AdapterView<*>)
{
Log.d("MyTag", "Nothing selected")
}
}
}
}
Now when You reselect the same item function will be executed
答案2
得分: -1
spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView> parentView, View selectedItemView, int position, long id) {
// 在这里编写处理选择的代码,使用选定的项目位置
}
@Override
public void onNothingSelected(AdapterView<?> parentView) {
// 在这里编写处理未选择项的代码
}
});
英文:
did you use
spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
// your code here with selected **item position**
}
@Override
public void onNothingSelected(AdapterView<?> parentView) {
// your code here
}
});
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论