简体   繁体   English

我如何在 MaterialBetterSpinner 库上实现 onItemSelected

[英]How can i implement onItemSelected on MaterialBetterSpinner library

I have implemented the following library spinner in my app ie from xml我已经在我的应用程序中实现了以下库微调器,即从 xml

<com.weiwangcn.betterspinner.library.material.MaterialBetterSpinner
        android:id="@+id/insurer_code"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="@string/hint_insurer_code"
        android:textColor="@color/smart_primary"
        android:textColorHint="@color/input_register_hint"
        app:met_floatingLabel="normal" />

and java code和java代码

public class testActivity extends Activity implements OnItemSelectedListener

@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {

    Toast.makeText(adapterView.getContext(), "Selected: " , Toast.LENGTH_LONG).show();
    // On selecting a spinner item
    String item = adapterView.getItemAtPosition(i).toString();
    // Showing selected spinner item
    Toast.makeText(adapterView.getContext(), "Selected: " + item, Toast.LENGTH_LONG).show();

}

but the onItemSelected doesn't fire up when an item is selected from the menu.但是当从菜单中选择一个项目时 onItemSelected 不会启动。 Any guidance on how to successfully implement the above library will be appreciated.任何有关如何成功实施上述库的指导将不胜感激。

just simple add this one and works like a charm!!只需简单地添加这个,就像一个魅力! ` `

materialDesignSpinner.setAdapter(arrayAdapter);
        materialDesignSpinner.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                quantity=materialDesignSpinner.getText().toString();
Log.d("value",quantity);

            }
        });

` `

The setOnItemSelectedListener does not work, because the Material/BetterSpinner is a EditText with auto-complete. setOnItemSelectedListener 不起作用,因为 Material/BetterSpinner 是具有自动完成功能的 EditText。 Not a really Spinner.不是一个真正的旋转器。 you can see the answer here: https://github.com/Lesilva/BetterSpinner/issues/42你可以在这里看到答案: https : //github.com/Lesilva/BetterSpinner/issues/42

You can use this code.您可以使用此代码。

MaterialBetterSpinner MBS = (MaterialBetterSpinner) findViewById(R.id.Spinner);
MBS.addTextChangedListener(new myTextWatcher() {
@Override
public void afterTextChanged(Editable s) {
  Log.e("Text",MBS.getText().toString()); 
}
});

Create a myTextWatcher class then copy paste the code below.创建一个 myTextWatcher 类,然后复制粘贴下面的代码。

import android.text.Editable;
import android.text.TextWatcher;
public class myTextWatcher implements TextWatcher {
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
    public void onTextChanged(CharSequence s, int start, int before, int count) {}
    public void afterTextChanged(Editable s) {}
}

Use this使用这个

public class DropDownList extends MaterialBetterSpinner {

    private AdapterView.OnItemSelectedListener listener;

    public DropDownList(Context context) {
        super(context);
    }

    public DropDownList(Context arg0, AttributeSet arg1) {
        super(arg0, arg1);
    }

    public DropDownList(Context arg0, AttributeSet arg1, int arg2) {
        super(arg0, arg1, arg2);
    }

    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int i,   long l) {
        super.onItemClick(adapterView, view, i, l);

        if (listener != null)
            listener.onItemSelected(adapterView, view, i, l);
    }

    @Override
    public void setOnItemSelectedListener(AdapterView.OnItemSelectedListener l) {
        super.setOnItemSelectedListener(l);

        listener = l;
    }

} }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM