2016-10-15 4 views
0

私はプログラムで説明されたプロセスを使用してプログラムで生成されたボタンのカスタム状態をプログラムしようとしていますが、メソッドをコールしようとすると "メソッドを解決できません"私のカスタムボタンクラス。私が呼び出そうとしているメソッドはpublicですので、なぜそれが呼び出されようとしているのかわかりません。思考?アンドロイド:方法を解決できません

@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ですが、あなただけの通常のButtonとしてbtnTagを宣言しました。

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

Buttonは、カスタムメソッドを持っていないので、コンパイラはsetButtonTypeOne()を見つけることができません。

あなたのCustomButtonタイプとしてbtnTagを宣言することができ、次のいずれか

final CustomButton btnTag = ... 

それとも上のメソッドを呼び出す前に、その型にキャスト:

((CustomButton) btnTag).setButtonTypeOne(true); 
関連する問題