2013-03-15 22 views
5

Trong ứng dụng của tôi, tôi đang tạo spinner động trong mã. Tôi muốn buộc người dùng nhấp vào Spinner và thay đổi giá trị/nội dung của nó. Nếu không, người dùng sẽ không thể truy cập màn hình tiếp theo bằng cách nhấp vào nút Tiếp theo.Cách tìm xem liệu người dùng đã nhấp vào spinner trong Android chưa?

Cách thực hiện điều đó trong Android? Có ai có ý kiến ​​gì không?

Cảm ơn trước.

Rohan

+0

simpally bạn chỉ cần vô hiệu hóa nút "Tiếp theo" cho đến khi người dùng thay đổi giá trị của spinner ... đơn giản này có cần nhiều câu trả lời này không .... ???? – SilentKiller

Trả lời

0

Gọi bạn Intent cho tới Activity trong Click Listener của spinner rằng

0

Tạo một biến boolean toàn cầu như ..

boolean isSelect = false; 

Bây giờ khi sử dụng giá trị chọn từ spinner sau đó làm nó isSelect = false. Và khi người dùng nhấp vào điều kiện kiểm tra nút NEXT cho isSelect là đúng hoặc sai.

Vậy đó.

2

Để giá trị đầu tiên của Spinner giống như "-xin vui lòng chọn-".

khi người dùng nhấp vào nút tiếp theo thực hiện xác thực và kiểm tra xem giá trị của selectedItem trong spinner có là "-please select-" và nếu có, sau đó hiển thị bánh mì nướng và yêu cầu người dùng chọn thứ gì đó từ spinner.

bạn cần mã, hãy cho tôi biết.

0

Bạn có thể nhận giá trị mục đã chọn bằng cách sử dụng phương pháp sau.

  spinner.setOnItemSelectedListener(new OnItemSelectedListener() {  
      public void onItemSelected(AdapterView<?> arg0, View arg1, 
        int arg2, long arg3) { 
       if (spinner.getSelectedItem().toString().equals("YourValue")) { 
        Intent yourIntent = new Intent(this,YourClassName.class); 
        startActivity(yourIntent); 
       } 
      }  
      public void onNothingSelected(AdapterView<?> arg0) { 

      } 
     }); 
0

Trong trường hợp của tôi, tôi thêm một mục thêm vào vị trí đầu tiên của danh sách

1. "Chọn cái gì đó" 2. "Đi tới" 3. "Đi trước" 5 ... . 6 ..

sau đó sử dụng

String item=spinnerObject.getSelectedItem(); 
    now check if("Select Something".equels(item)){ 
show some dialog to select anything from spinner 
    }else{ 
send it to next screen 
} 
0

sử dụng mã này cho kiểm tra các mục spinnerđược chọn hoặc không.

cả hai cờ đều ở cấp lớp (Toàn cầu).

Boolean temp = false; 
Boolean check = false; 


spinner1.setOnItemSelectedListener(new OnItemSelectedListener() { 

     @Override 
     public void onItemSelected(AdapterView<?> arg0, View arg1, 
       int arg2, long arg3) { 
      // TODO Auto-generated method stub 
      if(temp){ 
       check = true; 
      } 
      temp = true; 
     } 

     @Override 
     public void onNothingSelected(AdapterView<?> arg0) { 
      // TODO Auto-generated method stub 
      check = false; 
     } 
}); 

button.setOnClickListener(new OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      // TODO Auto-generated method stub 

      if(check){ 

       //perform when user select spinner item    
      }else{ 

       //put Dialog for alert please select spinner item 

      } 
     } 
} 
+0

Hi Sandip, tôi đã sử dụng mã do u..but cung cấp ngay cả khi tôi không nhấp vào spinner, giá trị cờ sẽ đến đúng. – Waugh

+0

@Waugh vui lòng kiểm tra mã mới trong đó tôi thêm một cờ mới. Và nó là bắt buộc. vì vậy, hãy đặt nó và thử. –

0

Tôi đã tạo một lớp Spinner mới gói gọn các nguyên tắc được đề cập ở trên. Nhưng thậm chí sau đó bạn phải chắc chắn để gọi phương thức chính xác và không setSelection

điều Cùng trong một gist

import android.content.Context; 
import android.util.AttributeSet; 
import android.view.View; 
import android.widget.AdapterView; 

/** 
* Used this to differentiate between user selected and prorammatically selected 
* Call {@link Spinner#programmaticallySetPosition} to use this feature. 
* Created by vedant on 6/1/15. 
*/ 
public class Spinner extends android.widget.Spinner implements AdapterView.OnItemSelectedListener { 

    OnItemSelectedListener mListener; 

    /** 
    * used to ascertain whether the user selected an item on spinner (and not programmatically) 
    */ 
    private boolean mUserActionOnSpinner = true; 

    @Override 
    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { 

     if (mListener != null) { 

      mListener.onItemSelected(parent, view, position, id, mUserActionOnSpinner); 
     } 
     // reset variable, so that it will always be true unless tampered with 
     mUserActionOnSpinner = true; 
    } 

    @Override 
    public void onNothingSelected(AdapterView<?> parent) { 
     if (mListener != null) 
      mListener.onNothingSelected(parent); 
    } 

    public interface OnItemSelectedListener { 
     /** 
     * <p>Callback method to be invoked when an item in this view has been 
     * selected. This callback is invoked only when the newly selected 
     * position is different from the previously selected position or if 
     * there was no selected item.</p> 
     * 
     * Impelmenters can call getItemAtPosition(position) if they need to access the 
     * data associated with the selected item. 
     * 
     * @param parent The AdapterView where the selection happened 
     * @param view The view within the AdapterView that was clicked 
     * @param position The position of the view in the adapter 
     * @param id The row id of the item that is selected 
     */ 
     void onItemSelected(AdapterView<?> parent, View view, int position, long id, boolean userSelected); 

     /** 
     * Callback method to be invoked when the selection disappears from this 
     * view. The selection can disappear for instance when touch is activated 
     * or when the adapter becomes empty. 
     * 
     * @param parent The AdapterView that now contains no selected item. 
     */ 
     void onNothingSelected(AdapterView<?> parent); 
    } 

    public void programmaticallySetPosition(int pos, boolean animate) { 
     mUserActionOnSpinner = false; 
     setSelection(pos, animate); 
    } 

    public void setOnItemSelectedListener (OnItemSelectedListener listener) { 
     mListener = listener; 
    } 

    public Spinner(Context context) { 
     super(context); 
     super.setOnItemSelectedListener(this); 
    } 

    public Spinner(Context context, int mode) { 
     super(context, mode); 
     super.setOnItemSelectedListener(this); 
    } 

    public Spinner(Context context, AttributeSet attrs) { 
     super(context, attrs); 
     super.setOnItemSelectedListener(this); 
    } 

    public Spinner(Context context, AttributeSet attrs, int defStyle) { 
     super(context, attrs, defStyle); 
     super.setOnItemSelectedListener(this); 
    } 

    public Spinner(Context context, AttributeSet attrs, int defStyle, int mode) { 
     super(context, attrs, defStyle, mode); 
     super.setOnItemSelectedListener(this); 
    } 
} 
6

bạn có thể sử dụng này:

if (spin.getSelectedItemPosition() < 0) {//Do something} 

điều này có nghĩa người dùng có không đã chọn bất kỳ thứ gì.

Các vấn đề liên quan