如何在MaterialBetterSpinner库

时间:2016-04-24 13:02:39

标签: android spinner material

我在我的应用程序中实现了以下库微调器,即来自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" />

和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();

}

但是当从菜单中选择一个项目时,onItemSelected不会启动。有关如何成功实施上述图书馆的任何指导将不胜感激。

4 个答案:

答案 0 :(得分:9)

只需简单添加这个,就像一个魅力! `

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);

            }
        });

`

答案 1 :(得分:3)

setOnItemSelectedListener不起作用,因为Material / BetterSpinner是具有自动完成功能的EditText。不是真正的微调。 你可以在这里看到答案: https://github.com/Lesilva/BetterSpinner/issues/42

答案 2 :(得分:3)

您可以使用此代码。

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) {}
}

创建一个myTextWatcher类,然后复制粘贴下面的代码。

SELECT * FROM CoachService WHERE shop_id in (?) and brand_id = ?

答案 3 :(得分:2)

使用此

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;
    }

}