我有一个Multiselectorspinner(旁边有复选框),并且想在单击某些内容时接到电话。 现在,我实现了OnClickListener,但是只有在应用启动后才打开,之后才调用。 这是我的代码:
override fun onCreate(savedInstanceState: Bundle?) {
var spinner: MultiSelectionSpinner
spinner = findViewById(R.id.mySpinner1) as MultiSelectionSpinner
spinner.setItems(Busliste)
spinner.onItemSelectedListener = object : AdapterView.OnItemSelectedListener{
override fun onNothingSelected(parent: AdapterView<*>?) {
println("works")
}
override fun onItemSelected(parent: AdapterView<*>?, view: View?, position: Int, id: Long) {
println("listener works")
}
}
我正在使用的MultiselectorSpinner具有以下代码:
package com.hdmc.smartristraveller;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnMultiChoiceClickListener;
import android.os.AsyncTask;
import android.util.AttributeSet;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.support.v7.widget.AppCompatSpinner;
import android.widget.SpinnerAdapter;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
/**
* Created by Aneh Thakur on 5/7/2015.
*/
public class MultiSelectionSpinner extends AppCompatSpinner implements
OnMultiChoiceClickListener {
String[] _items = null;
boolean[] mSelection = null;
ArrayAdapter<String> simple_adapter;
public MultiSelectionSpinner(Context context) {
super(context);
simple_adapter = new ArrayAdapter<String>(context,
android.R.layout.simple_spinner_item);
super.setAdapter(simple_adapter);
}
public MultiSelectionSpinner(Context context, AttributeSet attrs) {
super(context, attrs);
simple_adapter = new ArrayAdapter<String>(context,
android.R.layout.simple_spinner_item);
super.setAdapter(simple_adapter);
}
public void onClick(DialogInterface dialog, int which, boolean isChecked) {
if (mSelection != null && which < mSelection.length) {
mSelection[which] = isChecked;
simple_adapter.clear();
simple_adapter.add(buildSelectedItemString());
} else {
throw new IllegalArgumentException(
"Argument 'which' is out of bounds.");
}
}
@Override
public boolean performClick() {
AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
builder.setMultiChoiceItems(_items, mSelection,this);
builder.show();
return true;
}
@Override
public void setAdapter(SpinnerAdapter adapter) {
throw new RuntimeException(
"setAdapter is not supported by MultiSelectSpinner.");
}
public void setItems(String[] items) {
_items = items;
mSelection = new boolean[_items.length];
simple_adapter.clear();
simple_adapter.add(_items[0]);
Arrays.fill(mSelection, false);
}
public void setItems(List<String> items) {
_items = items.toArray(new String[items.size()]);
mSelection = new boolean[_items.length];
simple_adapter.clear();
simple_adapter.add(_items[0]);
Arrays.fill(mSelection, false);
}
public void setSelection(String[] selection) {
for (String cell : selection) {
for (int j = 0; j < _items.length; ++j) {
if (_items[j].equals(cell)) {
mSelection[j] = true;
}
}
}
}
public void setSelection(List<String> selection) {
for (int i = 0; i < mSelection.length; i++) {
mSelection[i] = false;
}
for (String sel : selection) {
for (int j = 0; j < _items.length; ++j) {
if (_items[j].equals(sel)) {
mSelection[j] = true;
}
}
}
simple_adapter.clear();
simple_adapter.add(buildSelectedItemString());
}
public void setSelection(int index) {
for (int i = 0; i < mSelection.length; i++) {
mSelection[i] = false;
}
if (index >= 0 && index < mSelection.length) {
mSelection[index] = true;
} else {
throw new IllegalArgumentException("Index " + index
+ " is out of bounds.");
}
simple_adapter.clear();
simple_adapter.add(buildSelectedItemString());
}
public void setSelection(int[] selectedIndicies) {
for (int i = 0; i < mSelection.length; i++) {
mSelection[i] = false;
}
for (int index : selectedIndicies) {
if (index >= 0 && index < mSelection.length) {
mSelection[index] = true;
} else {
throw new IllegalArgumentException("Index " + index
+ " is out of bounds.");
}
}
simple_adapter.clear();
simple_adapter.add(buildSelectedItemString());
}
public List<String> getSelectedStrings() {
List<String> selection = new LinkedList<String>();
for (int i = 0; i < _items.length; ++i) {
if (mSelection[i]) {
selection.add(_items[i]);
}
}
return selection;
}
public List<Integer> getSelectedIndicies() {
List<Integer> selection = new LinkedList<Integer>();
for (int i = 0; i < _items.length; ++i) {
if (mSelection[i]) {
selection.add(i);
}
}
return selection;
}
private String buildSelectedItemString() {
StringBuilder sb = new StringBuilder();
boolean foundOne = false;
for (int i = 0; i < _items.length; ++i) {
if (mSelection[i]) {
if (foundOne) {
sb.append(", ");
}
foundOne = true;
sb.append(_items[i]);
}
}
return sb.toString();
}
public String getSelectedItemsAsString() {
StringBuilder sb = new StringBuilder();
boolean foundOne = false;
for (int i = 0; i < _items.length; ++i) {
if (mSelection[i]) {
if (foundOne) {
sb.append(", ");
}
foundOne = true;
sb.append(_items[i]);
}
}
return sb.toString();
}
}
答案 0 :(得分:0)
好吧,我无法使它工作,但我要做的是使用处理程序来创建计时器。该计时器将每2秒打开一次,看看是否选中了任何框,然后将其返回给我。 这是代码片段
val handler_spinner=Handler()
handler_spinner.post(Timer_identify_spinnerchange);
private val Timer_identify_spinnerchange = object : Runnable {
override fun run() {
handler_spinner.postDelayed(this,2000)
if(spinner.selectedItemsAsString.....){do sth}
}
}
也许有一天能对某人有所帮助