android 入门demo 事件监听

package com.isoftstone.cry;

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 EventListenerActivity extends Activity
{
	private EditText myEdit1,myEdit2 ;
	private CheckBox cb1 ;
	private Button btn1,btn2 ;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) 
	{
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.event_listener_layout);
		//获取组件对象
		myEdit1 = (EditText)this.findViewById(R.id.editText1);
		myEdit2 = (EditText)this.findViewById(R.id.editText2);
		cb1 = (CheckBox)this.findViewById(R.id.checkbox1);
		btn1 = (Button)this.findViewById(R.id.button01);
		btn2 = (Button)this.findViewById(R.id.button02);
		//响应事件
		myEdit1.setOnKeyListener(new OnKeyListener() {
			@Override
			public boolean onKey(View v, int keyCode, KeyEvent event) {
				// TODO Auto-generated method stub
				myEdit1.setText("");
				return false;
			}
		});
		
		myEdit1.setOnFocusChangeListener(new OnFocusChangeListener() {
			@Override
			public void onFocusChange(View v, boolean hasFocus) {
				// TODO Auto-generated method stub
				//使用toast显示内容
				Toast.makeText(getApplicationContext(),myEdit1.getText(), Toast.LENGTH_LONG);
			}
		});
		
		myEdit2.setOnKeyListener(new OnKeyListener() {
			@Override
			public boolean onKey(View v, int keyCode, KeyEvent event) {
				// TODO Auto-generated method stub
				myEdit2.setText("");
				return false;
			}
		});
		
		myEdit2.setOnFocusChangeListener(new OnFocusChangeListener() {
			@Override
			public void onFocusChange(View v, boolean hasFocus) {
				// TODO Auto-generated method stub
				//使用toast显示内容
				myEdit1.setText("获取焦点");
				//Toast.makeText(getApplicationContext(),myEdit2.getText(), Toast.LENGTH_SHORT);
			}
		});
		
		cb1.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			@Override
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				// TODO Auto-generated method stub
				Toast.makeText(getApplicationContext(),cb1.isChecked()+"",Toast.LENGTH_LONG);
			}
		});
		
		btn1.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Toast.makeText(getApplicationContext(),btn1.getText(),Toast.LENGTH_LONG);
			}
		});
		
		btn2.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Toast.makeText(getApplicationContext(),btn2.getText(),Toast.LENGTH_LONG);
			}
		});
	}
}
<?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/tableLayout1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >

        <TableRow>
            <TextView
	        android:id="@+id/textView1"
	        android:layout_width="wrap_content"
	        android:layout_height="wrap_content"
	        android:text="@string/username" />
        	
            <EditText
	        android:id="@+id/editText1"
	        android:text="admin"
	        android:layout_width="fill_parent"
	        android:scrollHorizontally="true"></EditText>
        </TableRow>
        
        <TableRow>
            <TextView
	        android:id="@+id/textView2"
	        android:layout_width="wrap_content"
	        android:layout_height="wrap_content"
	        android:text="@string/password" />
       
            <EditText
	        android:id="@+id/editText2"
	        android:password="true"
	        android:text="123"
	        android:layout_width="fill_parent"
	        android:scrollHorizontally="true"></EditText>
        </TableRow>
        
         <TableRow>
            <TextView
	        android:id="@+id/textView3"
	        android:layout_width="wrap_content"
	        android:layout_height="wrap_content"
	        android:text="@string/autoLogin" />
       
          <CheckBox 
              android:text=""
              android:id="@+id/checkbox1"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content">
          </CheckBox>
        </TableRow>
         
         <TableRow>
          	<Button android:text="login" android:id="@+id/button01"/>
          	<Button android:text="canncel" android:id="@+id/button02"/>
        </TableRow>
         
    </TableLayout>

</LinearLayout>

相关推荐