Android OnFocuChangeListener焦点事件详解
本文实例为大家分享了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(); } }
效果图:
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。
赞 (0)