亲宝软件园·资讯

展开

Android OnFocuChangeListener焦点事件 Android OnFocuChangeListener焦点事件详解

whaletosea 人气:0
想了解Android OnFocuChangeListener焦点事件详解的相关内容吗,whaletosea在本文为您仔细讲解Android OnFocuChangeListener焦点事件的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:Android,OnFocuChangeListener,焦点事件,下面大家一起来学习吧。

界面

打开“res/layout/activity_main.xml”文件。

<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:orientation="vertical" >

  <EditText
    android:id="@+id/mobile"
    android:layout_width="190dp"
    android:layout_height="wrap_content"
    android:text="手机号码" />

  <EditText
    android:id="@+id/address"
    android:layout_width="190dp"
    android:layout_height="wrap_content"
    android:text="地址" />

</LinearLayout>

MainActivity.java

package com.example.whaletosea.application04;

import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.widget.EditText;
import android.view.View.OnClickListener;
import android.view.View.OnFocusChangeListener;
import android.widget.Toast;

public class MainActivity extends Activity {
  //声明 EditText
  private EditText etMobile=null;
  private EditText etAddress=null;
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    //获取EditText
    etMobile = (EditText) super.findViewById(R.id.mobile);
    etAddress = (EditText) super.findViewById(R.id.address);
    //注册OnClick OnFocusChange监听器
    etMobile.setOnClickListener(new MobileOnClickListener());
    etMobile.setOnFocusChangeListener(new MobileOnFocusChanageListener());
    etAddress.setOnClickListener(new AddressOnClickListener());
    etAddress.setOnFocusChangeListener(new AddressOnFocusChanageListener());
  }
  //MobileOnClickListener单击监听器
   private class MobileOnClickListener implements OnClickListener{
    @Override
    public void onClick(View view ){
      etMobile.setText("");
    }
  }
  //MobileOnFocusChanageListener焦点监听器
  private class MobileOnFocusChanageListener implements OnFocusChangeListener{
    @Override
    public void onFocusChange(View view ,boolean hasFocus){
      if(view.getId()==etMobile.getId())
        Toast.makeText(getApplicationContext(),"手机文本框获得焦点!",Toast.LENGTH_LONG).show();

    }
  }
  //AddressOnClickListener单击监听器
  private class AddressOnClickListener implements OnClickListener{
    @Override
    public void onClick(View view){
      etAddress.setText("");
    }
  }
  //MobileOnFocusChanageListener焦点监听器
  private class AddressOnFocusChanageListener implements OnFocusChangeListener{
    @Override
    public void onFocusChange(View view,boolean hasFocus){
      if(view.getId()==etAddress.getId())
        Toast.makeText(getApplicationContext(), "地址文本框获得焦点!",Toast.LENGTH_LONG).show();
    }
}


效果图:

加载全部内容

相关教程
猜你喜欢
用户评论