2013-06-12 80 views
27

Tôi muốn hiển thị menu thả xuống để chọn giới tính. Tôi đã thông qua một mảng chuỗi nhưHiển thị giá trị mặc định trong Spinner trong android

String arr[]=new String[]{"male","female"}; 

nhưng vấn đề là đó là chương trình lựa chọn mặc định với giá trị của "male" và tôi muốn thể hiện "Gender" như giá trị mặc định. Nếu tôi vượt qua "Giới tính" trong mảng tại vị trí 0, thì nó cũng hiển thị trong trình đơn thả xuống. Tôi chỉ muốn "Giới tính" làm gợi ý nhưng không được hiển thị trong menu thả xuống.

Ai đó có thể cho tôi biết cách tôi có thể thực hiện việc này. Cảm ơn trước.

+3

[Kiểm tra câu hỏi này tương tự được hỏi ở đây] [1] [1]: http://stackoverflow.com/questions/867518/how-to-make-an-android -spinner-with-initial-text-select-one – Nargis

+0

Bạn có thể tạo một ảnh chụp màn hình về thiết kế mong muốn của mình không? Cảm ơn – Jarvis

Trả lời

-8

Bạn có thể thiết lập nhanh chóng để các spinner ... có thể được thiết lập trong xml với android: nhắc = "Giới tính"

+5

Tôi nhận được lời nhắc làm tiêu đề của cửa sổ tùy chọn và tùy chọn đã chọn vẫn giữ nguyên –

+6

Điều này là sai vì dấu nhắc là tiêu đề của cửa sổ bật lên. – pioneerBhawna

+7

Không hoạt động cho trình quay số – youssefhassan

44
Spinner sp = (Spinner)findViewById(R.id.spinner); 
sp.setSelection(pos); 

đây pos là số nguyên (mảng mục vị trí của bạn)

mảng là như dưới đây sau đó pos = 0;

String str[] = new String{"Select Gender","male", "female" }; 

sau đó trong onItemSelected

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

     if(position > 0){ 
      // get spinner value 
     }else{ 
      // show toast select gender 
     } 

    } 
+7

đã đọc lại câu hỏi. – Tarun

+0

ý tưởng tuyệt vời .. bạn đã giải quyết được vấn đề của mình .. +1 – Ranjit

-5

Hãy thử dưới đây:

<Spinner 
    android:id="@+id/YourSpinnerId" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:prompt="Gender" /> 
0

Tôi đã tìm thấy giải pháp bằng cách mở rộng ArrayAdapter và ghi đè phương pháp getView.

import android.content.Context; 
import android.support.annotation.NonNull; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.ArrayAdapter; 
import android.widget.Spinner; 
import android.widget.TextView; 

/** 
* A SpinnerAdapter which does not show the value of the initial selection initially, 
* but an initialText. 
* To use the spinner with initial selection instead call notifyDataSetChanged(). 
*/ 
public class SpinnerAdapterWithInitialText<T> extends ArrayAdapter<T> { 

    private Context context; 
    private int resource; 

    private boolean initialTextWasShown = false; 
    private String initialText = "Please select"; 

    /** 
    * Constructor 
    * 
    * @param context The current context. 
    * @param resource The resource ID for a layout file containing a TextView to use when 
    *     instantiating views. 
    * @param objects The objects to represent in the ListView. 
    */ 
    public SpinnerAdapterWithInitialText(@NonNull Context context, int resource, @NonNull T[] objects) { 
     super(context, resource, objects); 
     this.context = context; 
     this.resource = resource; 
    } 

    /** 
    * Returns whether the user has selected a spinner item, or if still the initial text is shown. 
    * @param spinner The spinner the SpinnerAdapterWithInitialText is assigned to. 
    * @return true if the user has selected a spinner item, false if not. 
    */ 
    public boolean selectionMade(Spinner spinner) { 
     return !((TextView)spinner.getSelectedView()).getText().toString().equals(initialText); 
    } 

    /** 
    * Returns a TextView with the initialText the first time getView is called. 
    * So the Spinner has an initialText which does not represent the selected item. 
    * To use the spinner with initial selection instead call notifyDataSetChanged(), 
    * after assigning the SpinnerAdapterWithInitialText. 
    */ 
    @Override 
    public View getView(int position, View recycle, ViewGroup container) { 
     if(initialTextWasShown) { 
      return super.getView(position, recycle, container); 
     } else { 
      initialTextWasShown = true; 
      LayoutInflater inflater = LayoutInflater.from(context); 
      final View view = inflater.inflate(resource, container, false); 

      ((TextView) view).setText(initialText); 

      return view; 
     } 
    } 
} 

Android nào khi khởi tạo Spinner, gọi getView cho tất cả các mục trong T[] objects. SpinnerAdapterWithInitialText trả về số TextView với số initialText, lần đầu tiên được gọi. Tất cả các lần khác nó gọi super.getView là phương pháp getView của ArrayAdapter được gọi nếu bạn đang sử dụng Spinner bình thường.

Để tìm hiểu xem người dùng đã chọn mục xoay hoặc nếu trình quay số vẫn hiển thị initialText, hãy gọi selectionMade và chuyển giao bộ xoay được bộ chuyển đổi.

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