Android OnFocuChangeListener焦点事件详解
时间:2021-08-01 08:56:51|栏目:Android代码|点击: 次
本文实例为大家分享了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代码
下一篇:Android GridView 滑动条设置一直显示状态(推荐)
本文标题:Android OnFocuChangeListener焦点事件详解
本文地址:http://www.codeinn.net/misctech/163339.html