2017-05-16 9 views
1

私はセルに触れるたびにセルの値が変化する数独グリッドを作ることに取り組んでいます。だから、私はChild ViewでLinearLayoutにこのsudokuグリッドを実装し、OnTouchメソッドを使ってみましたが、動作しません。 onTouchが実際に呼び出されたかどうかを確認するためにlogメソッドを使ってみましたが、このメソッドは完全に無視されているようです。私は他の質問で解決策を探していましたが、その解決策のどれも助けられなかったようです。私はちょっとここで吸う気がして、どんな助けでも大歓迎です。[Android] onTouch in childビューが応答していません

SudokuActivity.java

package snacker.nonogramsolver; 

import ...; /*many things are imported here*/ 

public class SudokuActivity extends AppCompatActivity { 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_sudoku); 
    Button btn = (Button)findViewById(R.id.btn_clear); 
    Sudoku sdk = new Sudoku(this); 
    sdk.setOnTouchListener(sdk); 
} 

} 
; 

Sudoku.java

package snacker.nonogramsolver; 

import ...; 

public class Sudoku extends View implements View.OnTouchListener { 
    int mWidth = 9; 
    int mHeight = 9; 
    int mCellWidth, mCellHeight; 
    int mCellMargin; 
    int mEdgeThick; 
    int mStatus; 
    int mTextSize; 
    int mXNow = -1, mYNow = -1; 
    int[][] mBoard = new int[9][9]; 
    Point mBoardPt; 

    Paint mTextPaint, mTileEdgePaint; 

    final static int VALID = 0; 

    public Sudoku(Context context){ 
     super(context); 
     initializeBoard(); 
    } 

    public Sudoku(Context context, AttributeSet attrs){ 
     super(context, attrs); 
     initializeBoard(); 
    } 

    @Override 
    protected void onDraw(Canvas canvas){ 
     /* There are some codes here */ 
     Log.d("LogTest","OnDraw Complete"); 
    } 

    public void initializeBoard(){ 
     for (int x=0; x< mWidth; x++){ 
      for (int y=0; y< mHeight; y++){ 
       mBoard[x][y] = 0; 
      } 
     } 
     invalidate(); 
    } 

    public boolean onTouch(View v, MotionEvent event){ 
     Log.d("LogTest","Touched?"); /* LOG NOT ACTIVE HERE */ 
     if(event.getAction() == MotionEvent.ACTION_DOWN){ 
      mXNow = getBoardX(event.getX()); 
      Log.d("LogTest","" + mXNow); /* LOG NOT ACTIVE HERE */ 
      mYNow = getBoardY(event.getY()); 
      Log.d("LogTest","" + mYNow); /* LOG NOT ACTIVE HERE */ 
      mBoard[mXNow][mYNow] = mBoard[mXNow][mYNow] + 1; 
      invalidate(); 
      return true; 
     } 
     else return false; 
    } 

    int getBoardX(float scrx){ 
     int x = (int)((scrx)/mCellWidth); 
     if (x < 0) x = 0; 
     if (x > 8) x= 8; 
     return x; 
    } 
    int getBoardY(float scry){ 
     int y = (int)((scry)/mCellHeight); 
     if (y < 0) y = 0; 
     if (y > 8) y = 8; 
     return y; 
    } 
} 

編集:追加の活動のXMLファイル

は、ここに私のコードです。

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:orientation="vertical" 
    android:id="@+id/activity_sudoku" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:paddingBottom="@dimen/activity_vertical_margin" 
    android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    tools:context="snacker.nonogramsolver.SudokuActivity"> 

    <snacker.nonogramsolver.Sudoku 
     android:id="@+id/SudokuGrid" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" /> 

    <Button 
     android:id="@+id/btn_clear" 
     android:layout_width="150dp" 
     android:layout_height="30dp" 
     android:layout_weight="0.06" 
     android:text="Clear" /> 

</LinearLayout> 
+0

私はあなたが活動中でこのカスタム数独ビューにonTouchListener設定し、そこonTouch()メソッドを処理する必要が推測します。あなたはリスナーを設定していませんか?私はあなたのアプローチがカスタムビューにするのが好きです。 –

+0

@GauravChauhan私はSudokuActivityでOnCreateのリスナーを持っています。多分、これでは十分ではない、あるいは間違ったアプローチでしたか? – SnackerH

+0

sdk.setOnTouchListener(this)のようなアクティビティコンテキストを渡して、アクティビティでonTouchを実装してみてください。 –

答えて

0

あなたは直接だけ 数独クラスのオブジェクトを作成することによって、touchListenerを追加することはできません。ビューをXMLまたはプログラムで追加する必要があります。

あなたの活動

public class MyActivity extends Activity{ 
    @Override 
protected void onCreate(Bundle savedInstanceState){ 
super.onCreate(savedInstanceState); 

//initializing custom views 
MyCustomView1 myCustomView1 = new MyCustomView1(parameterList); 
MyCustomView2 myCustomView2 = new MyCustomView2(parameterList); 

//adding both custom views to the main activity 
mainView.addView(myCustomView1); 
mainView.addView(myCustomView1); 

//adding custom listener to the custom view 1 
myCustomView1.setCustomEventListener(new OnCustomEventListener() { 

    @Override 
    public void onEvent() { 
     //firing an event of custom view 1 
     Toast.makeText(MainActivity.this, "Touched custom view 1", 
       Toast.LENGTH_SHORT).show(); 
    } 
}); 

//adding custom listener to the custom view 2 
myCustomView2.setCustomEventListener(new OnCustomEventListener() { 

    @Override 
    public void onEvent() { 
     //firing an event of custom view 2 
     Toast.makeText(MainActivity.this, "Touched custom view 2", 
       Toast.LENGTH_SHORT).show(); 
    } 
}); 
} 
} 

あなたのCustomView 1

public class MyCustomView1 extends LinearLayout{ 
OnCustomEventListener myCustomEventListener; 

public MyCustomView1(ParameterList){ 
super(ContextFromParameterList); 
//Just adding something to the custom view 1 in order to distinguish it on the screen 
TextView tv = new TextView(ContextFromParameterList); 
tv.setText("Hello world from custom view 1"); 
addView(tv); 

this.setOnTouchListener(new OnTouchListener() { 

    @Override 
    public boolean onTouch(View v, MotionEvent event) { 
     //delegating one event to another (delegating touch event to custom event) 
     if (MyCustomView1.this.myCustomEventListener != null) 
      MyCustomView1.this.myCustomEventListener.onEvent(); 
     return false; 
    } 
}); } 

ます。public void setCustomEventListener(OnCustomEventListener のEventListener){// 活動からカスタムリスナーを設定 myCustomEventListener =のEventListener。 }}

あなたCustomView2

public class MyCustomView2 extends LinearLayout { 
OnCustomEventListener myCustomEventListener; 

public MyCustomView2(ParameterList) { 
    super(ContextFromParameterList); 
//Just adding something to the custom view 1 in order to distinguish it on the screen 
TextView tv = new TextView(ContextFromParameterList); 
tv.setText("Hello world from custom view 2"); 
addView(tv); 

this.setOnTouchListener(new OnTouchListener() { 

    @Override 
    public boolean onTouch(View v, MotionEvent event) { 
     //delegating one event to another (delegating touch event to custom event) 
     if (MyCustomView2.this.myCustomEventListener != null) 
      MyCustomView2.this.myCustomEventListener.onEvent(); 
     return false; 
    } 
}); 
} 

    public void setCustomEventListener(OnCustomEventListener eventListener) { 
    //setting custom listener from activity 
    myCustomEventListener = eventListener; 
    } 
} 
Your listener interface: 

public interface OnCustomEventListener{ 
//interface defines one method. Can be more and methods may have parameters 
public void onEvent(); 
} 
+0

おっと、私はXMLファイルでカスタムビューを追加したことに言及しなかった... – SnackerH

+0

あなたはそのコードを追加できますか?私は考えを得ることができます。 – Dany

+0

そこにはあまりコードはありませんが、完了しました。 – SnackerH

関連する問題