用户退出微调器时调用哪个事件

时间:2017-01-12 14:44:41

标签: java android android-layout android-activity

我有一个微调器,当用户退出微调器时我想显示/隐藏某些东西而不做任何事情。例如,当用户触摸旋转器外部的区域时。 附:容器布局的onTouchEvent(在我的例子中也是LinearLayout)也没有被调用。 这是我对自定义管理器的实现:

public SpinnerHintAdapter(Activity context, int resourceId, int textViewId, List<SpinnerItem> list, Spinner parent){

    super(context,resourceId,textViewId, list);
    flater = context.getLayoutInflater();
    this.items = list;
    this.gender = parent;

}

 @Override
 public int getCount() {
     return items.size();
 }

 @Nullable
 @Override
 public SpinnerItem getItem(int position) {
     return items.get(position);
 }

 @Override
 public long getItemId(int position) {
     return position;
 }

 @Override
public View getView(int position, View convertView, ViewGroup parent) {
     SpinnerItem spinnerItem = getItem(position);
     View rowView = flater.inflate(R.layout.gender_item ,null,true);
     TextView txtTitle = (TextView) rowView.findViewById(R.id.main_text);
     txtTitle.setText(spinnerItem.getName());
     txtTitle.setTextColor(txtTitle.getResources().getColor(R.color.color_white));
     Log.i(Tags.byEmail, "VVVVVVVVVVVVv");;
     gender.setVisibility(View.VISIBLE);
     return rowView;
}


 @Override
 public View getDropDownView(int position, View convertView, ViewGroup parent) {
     firstTime = false;
     SpinnerItem rowItem = getItem(position);
     View rowView = flater.inflate(R.layout.gender_drop_down_item ,null,true);
     TextView txtTitle = (TextView) rowView.findViewById(R.id.drop_down_text);
     txtTitle.setText(rowItem.getName());
     if(!isEnabled(position)){
         txtTitle.setBackground(txtTitle.getResources().getDrawable(R.drawable.normal_rounded_text_field));
         txtTitle.setTextColor(Color.parseColor("#777777"));
     }
     parent.setBackground(parent.getResources().getDrawable(R.drawable.normal_rounded_text_field));
     txtTitle.setEnabled(isEnabled(position));
     gender.setVisibility(View.INVISIBLE);
     Log.i(Tags.byEmail, "DDDDDDDDDDDDDDDDDD");
     return rowView;
 }

 @Override
 public boolean isEnabled(int position) {
     if(position == 0)
         return false;
     return super.isEnabled(position);
 }

}

当用户退出而未选择项目时,不会调用getView函数,因此性别(是微调器对象本身)将不可见。 我试过OnItemSelected和OnNothingSelected也没有被调用。 事件OnTouch事件未被调用。

用户退出时不会调用以下事件: 1- OnItemSlected 2- OnNothingSelected 3- OnFocusChanged。 4- OnTouch

2 个答案:

答案 0 :(得分:1)

我认为这个事件会对你有用,不是吗?

public class CustomSpinner extends Spinner {

    ... Constructors ...

    @Override
    public void onWindowFocusChanged(boolean hasWindowFocus) {
        super.onWindowFocusChanged(hasWindowFocus);
        Log.d(CustomSpinner.class.getSimpleName(), "onWindowFocusChanged: " + hasWindowFocus);
        if (hasWindowFocus) {
            // User click out of window
        } else {
            // User click in spinner window
        }
    }
}

答案 1 :(得分:0)

使用Spinner设置AdapterView.OnItemSelectedListener,它实现了两种方法; onItemSelectedonNothingSelected

像这样:

spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
    @Override
    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
       // This is called when the user selects an item in the Spinner
    }

    @Override
    public void onNothingSelected(AdapterView<?> parent) {
        // This is called when the user closes the spinner selecting nothing
    }
});