首页 > 代码库 > 事件(5)

事件(5)

焦点事件:OnFocusChangeListener
  et.setOnFocusChangeListener(new OnFocusChangeListener() {
    @Override
    public void onFocusChange(View v, boolean hasFocus) {
      if(hasFocus){
        et.setText("");
      }else{
        String str=et.getText().toString();
        tv.setText(str);
      }
    }
  });

技术分享

技术分享
 1 package com.hh;
 2 
 3 import android.app.Activity;
 4 import android.os.Bundle;
 5 import android.view.View;
 6 import android.view.View.OnFocusChangeListener;
 7 import android.widget.EditText;
 8 import android.widget.TextView;
 9 
10 public class Day061 extends Activity{
11     private EditText et;
12     private TextView tv;
13     protected void onCreate(Bundle savedInstanceState) {
14         super.onCreate(savedInstanceState);
15         setContentView(R.layout.day06focus);
16         tv=(TextView) findViewById(R.id.tv061);
17         et=(EditText) findViewById(R.id.et061);
18         et.setOnFocusChangeListener(new OnFocusChangeListener() {
19             
20             @Override
21             public void onFocusChange(View v, boolean hasFocus) {
22                 if(hasFocus){
23                     et.setText("");
24                 }else{
25                     String str=et.getText().toString();
26                     tv.setText(str);
27                 }
28             }
29         });
30     }
31 }
代码示例
技术分享
 1 <?xml version="1.0" encoding="utf-8"?>
 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     android:orientation="vertical"
 4     android:layout_width="fill_parent" 
 5     android:layout_height="fill_parent" 
 6     >
 7       <TextView
 8           android:id="@+id/tv061"
 9         android:layout_width="fill_parent" 
10         android:layout_height="wrap_content" 
11            android:background="#FF0000"
12       />
13        <EditText
14                android:id="@+id/et061"
15             android:layout_width="fill_parent" 
16             android:layout_height="wrap_content"
17        />
18        <EditText
19                android:id="@+id/et062"
20             android:layout_width="fill_parent" 
21             android:layout_height="wrap_content"
22        />
23 </LinearLayout>
xml配置文件

 

事件(5)