0

所以这里是简单转换的代码,从 top_left 到 bottom_right,我不明白为什么我必须返回 false 才能使 onTouch() 正常工作,

1.如果我设置 return true, one a single touch ,它将 counter 从 0 设置为 3 ,或者只是将 2 或 3 添加到 counter 并且大部分时间保持在当前位置。

2.如果我设置return false,程序运行正常,按照写的,即counter++;

public class MainActivity extends AppCompatActivity {
    Button b;
    TextView tv;

    public static int counter_button=0,counter=0;
    RelativeLayout.LayoutParams old_rules;
    RelativeLayout rl;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        b= (Button) findViewById(R.id.b1);
        tv= (TextView) findViewById(R.id.tv1);
        rl= (RelativeLayout) findViewById(R.id.rl);
        old_rules= (RelativeLayout.LayoutParams) b.getLayoutParams();
        rl.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
               my_changes_touch();
                return false;
            }
        });
    }

    public void my_changes_touch(){

        RelativeLayout.LayoutParams rules= new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,RelativeLayout.LayoutParams.WRAP_CONTENT);
        if(counter%2==0) {
            counter++;
            tv.setText(" "+counter);
            rules.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
            rules.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
            b.setLayoutParams(rules);
            rules.height=400;
            rules.width=400;
            b.setLayoutParams(rules);
            getContentTransitionManager().beginDelayedTransition(rl);
        } else {
            counter++;
            tv.setText(" "+counter);
            b.setLayoutParams(old_rules);
            getContentTransitionManager().beginDelayedTransition(rl);
        }    
    }
}
4

1 回答 1

2

返回值决定事件是否被视图消费。

如此真实意味着您也对其他事件感兴趣。

如果您返回 false,则触摸事件将被传递到视图层次结构中更靠前的下一个视图,您将不会收到任何进一步的调用。

请检查此答案 https://stackoverflow.com/a/3756619/2783541

于 2016-06-30T05:19:32.660 回答