Android笔记二十四.Android基于回调的事件处理机制
package com.example.android_event2; import android.content.Context; import android.util.AttributeSet; import android.view.KeyEvent; import android.widget.Button; import android.widget.Toast; public class MyButton extends Button { //构造方法 public MyButton(Context context, AttributeSet attrs) { super(context, attrs); } //回调方法 @Override public boolean onKeyDown(int keyCode, KeyEvent event) { super.onKeyDown(keyCode, event); Toast.makeText(getContext(),"您按下了数字:"+keyCode,Toast.LENGTH_SHORT).show(); return true; //返回true,表明该事件不会向外扩散 } }
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" > <!--方法一:在XML布局文件中创建组件--> <com.example.android_event2.MyButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:text="回调测试" /> </LinearLayout> (3)MainActivity.java:主Activity package com.example.android_event2; import android.app.Activity; import android.os.Bundle; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); //方法二:Java代码中配置自定义组件 } }
package com.example.android_event2; import android.app.Activity; import android.os.Bundle; import android.view.KeyEvent; import android.view.View; import android.view.View.OnKeyListener; import android.widget.Button; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); //采用监听模式 Button myButton = (Button)findViewById(R.id.btn); myButton.setOnKeyListener(new OnKeyListener(){ public boolean onKey(View v, int keyCode, KeyEvent event) { System.out.println("组件所绑定的事件监听器被触发"); return false; } }); } @Override public boolean onKeyDown(int keyCode, KeyEvent event) { super.onKeyDown(keyCode, event); System.out.println("该组件所在的Activity的回调方法onKeyDown被调用"); return false; //返回true,表明该事件不会向外扩散 } }
package com.example.android_event2; import android.content.Context; import android.util.AttributeSet; import android.view.KeyEvent; import android.widget.Button; public class MyButton extends Button { //构造方法 public MyButton(Context context, AttributeSet attrs) { super(context, attrs); } //回调方法 @Override public boolean onKeyDown(int keyCode, KeyEvent event) { super.onKeyDown(keyCode, event); System.out.println("自定义组件中的回调方法onKeyDown被调用"); return false; //返回true,表明该事件不会向外扩散 } }
package com.example.android_event2; import android.content.Context; import android.util.AttributeSet; import android.view.KeyEvent; import android.widget.Button; public class MyButton extends Button { //构造方法 public MyButton(Context context, AttributeSet attrs) { super(context, attrs); } //回调方法 @Override public boolean onKeyDown(int keyCode, KeyEvent event) { super.onKeyDown(keyCode, event); System.out.println("自定义组件中的回调方法onKeyDown被调用"); return false; //返回true,表明该事件不会向外扩散 } }
package com.example.android_event3; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.widget.EditText; public class MainActivity extends Activity { private EditText text; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); text = (EditText)findViewById(R.id.result); } //将clickHandler方法直接绑定到标签 public void clickHandler(View source) { text.setText("中华人民共和国万岁!"); } }
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" > <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="单击我" android:onClick="clickHandler"/> <EditText android:id="@+id/result" android:inputType="none" android:layout_width="match_parent" android:layout_height="wrap_content"/> </LinearLayout>
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。