温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Android中怎么实现OnFocuChangeListener焦点事件

发布时间:2021-06-26 17:24:48 来源:亿速云 阅读:307 作者:Leah 栏目:移动开发

这篇文章将为大家详细讲解有关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();
    }
}

效果图:

Android中怎么实现OnFocuChangeListener焦点事件

Android中怎么实现OnFocuChangeListener焦点事件

关于Android中怎么实现OnFocuChangeListener焦点事件就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI