2016-04-24 105 views
0

我已实现了以下库飞旋在我的应用程序,即从XML我怎么能在MaterialBetterSpinner库

<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没有火起来的时候实现onItemSelected从菜单中选择一个项目。将不胜感激关于如何成功实现上述库的任何指导。

回答

1

你可以使用这段代码。

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

创建一个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) {} 
} 
0

使用此

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

}

3

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

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

      } 
     }); 

`