2016-04-24 104 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); 

      } 
     }); 

`