1、工程目录
2、TestEventActivity.java
package com.example.test_event; import android.os.Bundle; import android.app.Activity; import android.view.KeyEvent; import android.view.Menu; import android.view.View; import android.view.View.OnClickListener; import android.view.View.OnFocusChangeListener; import android.view.View.OnKeyListener; import android.widget.Button; import android.widget.CheckBox; import android.widget.CompoundButton; import android.widget.CompoundButton.OnCheckedChangeListener; import android.widget.EditText; //import android.widget.TextView; import android.widget.Toast; public class TestEventActivity extends Activity { // private TextView myTextview1, myTextview2, myTextView3; private Button myButton1, myButton2; private EditText myEditText1, myEditText2; private CheckBox myCheckbox1; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.test_event); // myTextview1 = (TextView) findViewById(R.id.textviewLayout01); // myTextview2 = (TextView) findViewById(R.id.textviewLayout02); // myTextView3 = (TextView) findViewById(R.id.textviewLayout03); myButton1 = (Button) findViewById(R.id.buttonLayout01); myButton2 = (Button) findViewById(R.id.buttonLayout02); myEditText1 = (EditText) findViewById(R.id.edittextLayout01); myEditText2 = (EditText) findViewById(R.id.editviewLayout02); myCheckbox1 = (CheckBox) findViewById(R.id.checkboxLayout01); // 编辑文本框的按键事件 myEditText1.setOnKeyListener(new OnKeyListener() { @Override public boolean onKey(View arg0, int arg1, KeyEvent arg2) { // TODO Auto-generated method stub myEditText1.setText(""); return false; } }); myEditText2.setOnKeyListener(new OnKeyListener() { @Override public boolean onKey(View arg0, int arg1, KeyEvent arg2) { // TODO Auto-generated method stub myEditText2.setText(""); return false; } }); // 编辑文本框的焦点事件 myEditText1.setOnFocusChangeListener(new OnFocusChangeListener() { @Override public void onFocusChange(View arg0, boolean arg1) { // TODO Auto-generated method stub Toast.makeText(getApplicationContext(), myEditText1.getText(), Toast.LENGTH_LONG).show(); } }); myEditText2.setOnFocusChangeListener(new OnFocusChangeListener() { @Override public void onFocusChange(View arg0, boolean arg1) { // TODO Auto-generated method stub Toast.makeText(getApplicationContext(), myEditText2.getText(), Toast.LENGTH_LONG).show(); } }); // 多选框的选择事件 myCheckbox1.setOnCheckedChangeListener(new OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton arg0, boolean arg1) { // TODO Auto-generated method stub Toast.makeText(getApplicationContext(), myCheckbox1.isChecked() + "", Toast.LENGTH_LONG).show(); } }); // 按钮的选择事件 myButton1.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { // TODO Auto-generated method stub Toast.makeText(getApplicationContext(), myButton1.getText(), Toast.LENGTH_LONG).show(); } }); myButton2.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { // TODO Auto-generated method stub Toast.makeText(getApplicationContext(), myButton2.getText(), Toast.LENGTH_LONG).show(); } }); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.test_event, menu); return true; } }
3、布局文件
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <TableLayout android:id="@+id/myTableLayout" android:layout_width="wrap_content" android:layout_height="wrap_content" > <TableRow> <TextView android:id="@+id/textviewLayout01" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="用户名称" /> <EditText android:id="@+id/edittextLayout01" android:layout_width="fill_parent" android:scrollHorizontally="true" android:text="admin" /> </TableRow> <TableRow> <TextView android:id="@+id/textviewLayout02" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="用户密码" /> <EditText android:id="@+id/editviewLayout02" android:layout_width="fill_parent" android:password="true" android:scrollHorizontally="true" android:text="123" /> </TableRow> <TableRow> <TextView android:id="@+id/textviewLayout03" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="自动登录" /> <CheckBox android:id="@+id/checkboxLayout01" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </TableRow> <TableRow> <Button android:id="@+id/buttonLayout01" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="登录" /> <Button android:id="@+id/buttonLayout02" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="取消" /> </TableRow> </TableLayout> </LinearLayout>
【知识】
android中常见的事件监听器:
(1)点击事件View.onClickListener:当用户碰触到某个组件或者方向键被按下时产生该事件,处理方法是onClick()
(2)焦点事件View.OnFocusChangeListener:组件得到或者失去焦点时产生该事件,处理方法是onFocusChange()
(3)按键事件View.OnKeyListener:用户按下或者释放设备上的某个按键时产生,处理方法onKey()
(4)碰触事件View.OnTouchListener:设备具有碰触功能时,碰触屏幕产生该事件,处理方法onTouch()
(5)创建上下文菜单事件View.OnCreateContextMenuListener:创建上下文菜单时产生该事件,处理方法是onCreateContextMenu()
事件处理步骤:
(1)创建事件监听器
(2)给要响应事件的组件注册事件监听器
(3)在事件处理方法中编写实现代码
时间: 2024-10-26 23:22:00