Android学习历程-事件处理流程
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <TableLayout android:id="@+id/Table" android:layout_width="wrap_content" android:layout_height="wrap_content" > <TableRow> <TextView android:id="@+id/TextView01" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="用户名称:" /> <EditText android:id="@+id/EditText01" android:layout_width="wrap_content" android:scrollHorizontally="true" android:text="Admin" /> </TableRow> <TableRow> <TextView android:id="@+id/TextView02" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="用户密码:" /> <EditText android:id="@+id/EditText02" android:layout_width="wrap_content" android:inputType="textPassword" android:text="" /> </TableRow> <TableRow> <TextView android:id="@+id/TextView03" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="自动登录:" /> <CheckBox android:text="" android:id="@+id/CheckBox01" android:layout_width="wrap_content" android:layout_height="wrap_content"/> </TableRow> <TableRow> <Button android:text="登录" android:id="@+id/Button01"/> <Button android:text="取消" android:id="@+id/Button02"/> </TableRow> </TableLayout> </LinearLayout>
package com.example.testcontextmenu; import android.app.Activity; import android.os.Bundle; import android.view.KeyEvent; 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.Toast; public class TestAction extends Activity { private EditText myEdit1,myEdit2; private CheckBox cb1; private Button btn1,btn2; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.test_action); myEdit1 = (EditText)findViewById(R.id.EditText01); myEdit2 = (EditText)findViewById(R.id.EditText02); cb1 = (CheckBox)findViewById(R.id.CheckBox01); btn1 =(Button)findViewById(R.id.Button01); btn2 =(Button)findViewById(R.id.Button02); myEdit1.setOnKeyListener(new OnKeyListener() { @Override public boolean onKey(View v, int keyCode, KeyEvent event) { //清空EditKey myEdit1.setText(""); return false; } }); myEdit2.setOnFocusChangeListener(new OnFocusChangeListener() { @Override public void onFocusChange(View v, boolean hasFocus) { Toast t1 = Toast.makeText(getApplicationContext(), myEdit2.getText(), Toast.LENGTH_LONG); t1.show(); } }); cb1.setOnCheckedChangeListener(new OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { // TODO Auto-generated method stub Toast t1 = Toast.makeText(getApplicationContext(), cb1.isChecked()+"", Toast.LENGTH_LONG); t1.show(); } }); btn1.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub Toast t1 = Toast.makeText(getApplicationContext(), btn1.getText(), Toast.LENGTH_LONG); t1.show(); } }); btn2.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub Toast t1 = Toast.makeText(getApplicationContext(), btn2.getText(), Toast.LENGTH_LONG); t1.show(); } }); } }