13.事件处理和手势

    技术2022-07-10  111

    基于监听的事件处理

    基于回调的事件处理

    package com.example.demo; import androidx.appcompat.app.AppCompatActivity; import android.os.Bundle; import android.view.KeyEvent; import android.view.MotionEvent; import android.widget.Toast; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } @Override public boolean onTouchEvent(MotionEvent event) { Toast.makeText(MainActivity.this,"触摸",Toast.LENGTH_SHORT).show(); return super.onTouchEvent(event); } @Override public boolean onKeyDown(int keyCode, KeyEvent event) { Toast.makeText(MainActivity.this,"向下",Toast.LENGTH_SHORT).show(); return super.onKeyDown(keyCode, event); } @Override public boolean onKeyUp(int keyCode, KeyEvent event) { Toast.makeText(MainActivity.this,"抬起",Toast.LENGTH_SHORT).show(); return super.onKeyUp(keyCode, event); } }

    Processed: 0.034, SQL: 9