子视图中的[Android] onTouch没有响应

时间:2017-05-16 04:43:41

标签: android ontouch

我一直致力于制作数独网格,每当我触摸一个单元格时,其单元格的值会发生变化。所以我已经在子视图的LinearLayout中实现了这个数独网格,并尝试使用OnTouch方法,但它无法正常工作。我尝试使用log方法来检查onTouch是否实际被调用,但它似乎完全忽略了这个方法。我一直在寻找其他问题的解决方案,但似乎这些解决方案都没有帮助。我觉得有点傻逼,任何帮助都会非常感激。

这是我的代码:

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>

1 个答案:

答案 0 :(得分: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();
 }