2016-10-15 61 views
0

我试图使用过程解释here在我的应用程序生成的按钮编程自定义的状态,但我得到“无法解析法”编译器错误,当我尝试调用的方法我的自定义按钮类。该方法我试图打电话是公开的,所以我不知道为什么它不在范围内我试图从调用它是可见的。思考?安卓:无法解析方法

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
    View v = inflater.inflate(R.layout.menu_fragment, container, false); 
    typeOneArray = new boolean[21];   
    typeTwoArray = new int[21];  
    //programmatic button generation 
    buttonHolder = (LinearLayout) v.findViewById(R.id.buttonLayoutParent); 
    buttonHolder.setGravity(Gravity.RIGHT);         
    for (int j = 0; j < 21; j++) { 
     final int iteration = j; 
     final Button btnTag = new CustomButton(getActivity());      //CUSTOM BUTTON INSTANTIATION 
     btnTag.setId(j);         
     btnTag.setBackgroundResource(R.drawable.custom_button); 
     buttonHolder.addView(btnTag); 
     btnTag.setOnTouchListener(new View.OnTouchListener() { 
      @Override 
      public boolean onTouch(View v, MotionEvent event) { 
       if (event.getAction() == MotionEvent.ACTION_DOWN) {  //button was clicked 
        //clickCounter0++; 
        typeTwoArray[iteration] = typeTwoArray[iteration] + 1; 
        Handler handler = new Handler(); 
        Runnable singleClickLogic = new Runnable() { 
         @Override 
         public void run() { 
          if (typeTwoArray[iteration] !=0) { 
           Toast.makeText(getActivity(), "single click", Toast.LENGTH_SHORT).show(); 
           typeTwoArray[iteration] = 0; 
          } 
         } 
        }; 
        //Double click 
        if (typeTwoArray[iteration] == 2) { 
         btnTag.setButtonTypeOne(true);    //THIS IS WHERE THE ERROR HAPPENS 

         Toast.makeText(getActivity(), "double click", Toast.LENGTH_SHORT).show(); 
         typeTwoArray[iteration] = 0; 
        } 
        //Single click 
        else if (typeTwoArray[iteration] == 1) { 
         handler.postDelayed(singleClickLogic, 250);     
        } 
       } else if (event.getAction() == MotionEvent.ACTION_UP) {     

       } 
       return false; 
      } 
     }); 
    } 
    return v; 
} 

CustomButton.java

public class CustomButton extends Button { 
private boolean stateTwo = false; 
private boolean stateOne = false; 
private static final int[] STATE_ONE = {R.attr.state_one}; 
private static final int[] STATE_TWO = {R.attr.state_two}; 

public CustomButton(Context context) { 
    super(context); 
} 

public void setButtonTypeOne(boolean buttonState) { 
    stateOne = buttonState; 
} 
public void setButtonTypeTwo(boolean buttonState) { 
    stateTwo = buttonState; 
} 

@Override 
protected int[] onCreateDrawableState(int extraSpace) { 
    final int[] drawableState = super.onCreateDrawableState(extraSpace + 2); 
    if (stateOne) { 
     mergeDrawableStates(drawableState, STATE_ONE); 
    } 
    if (stateTwo) { 
     mergeDrawableStates(drawableState, STATE_TWO); 
    } 
    return drawableState; 
} 

}

回答

0

虽然btnTag实例是CustomButton,你声明btnTag作为只是一个普通的Button

final Button btnTag = new CustomButton(getActivity()); 

Button没有你的自定义的方法,所以编译器无法找到setButtonTypeOne()

您可以声明btnTag为您CustomButton类型:

final CustomButton btnTag = ... 

或调用它的方法之前,将它转换为该类型:

((CustomButton) btnTag).setButtonTypeOne(true);