一切准备就绪,开始项目开发了。
智能电视开发难点之一在于焦点的控制。我来发一个简单的封装焦点的登录功能
----------------------------------------------------------
输入时样式:默认焦点在第一个输入框
验证成功时样式:设置焦点在右边按钮上
点击设置按钮更换门店重新返回到输入时样式
-----------------------------------------------------------
代码如下:
view_login.xml
<?xml version="1.0" encoding="utf-8"?> <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="@dimen/width_login_view" android:layout_height="@dimen/height_login_view"> <LinearLayout android:id="@+id/ll_fail" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_gravity="right" android:background="#0affffff" android:focusable="false" android:orientation="horizontal"> <EditText android:id="@+id/et_shop_id" style="@style/text_white_12" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="3" android:focusableInTouchMode="true" android:focusable="true" android:hint="请输入门店编号" android:background="@null" android:nextFocusRight="@id/et_shop_pwd" android:padding="@dimen/main_padding_06" /> <TextView android:layout_width="@dimen/line_height" android:layout_height="match_parent" android:background="@mipmap/bg_login_light" /> <EditText android:id="@+id/et_shop_pwd" style="@style/text_white_12" android:layout_width="0dp" android:layout_height="wrap_content" android:background="@null" android:focusableInTouchMode="true" android:layout_weight="3" android:hint="请输入密码" android:nextFocusLeft="@id/et_shop_id" android:nextFocusRight="@id/tv_login_on" android:padding="@dimen/main_padding_06" /> <TextView android:id="@+id/tv_login_on" style="@style/text_white_12" android:layout_width="0dp" android:layout_height="wrap_content" android:focusableInTouchMode="true" android:layout_weight="1" android:background="#0affffff" android:focusable="true" android:gravity="center" android:nextFocusLeft="@id/et_shop_pwd" android:padding="@dimen/main_padding_06" android:text="确定" /> </LinearLayout> <LinearLayout android:id="@+id/ll_success" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:layout_gravity="right" android:layout_marginLeft="@dimen/main_padding_30" > <TextView android:layout_width="0dp" android:layout_weight="1" android:layout_height="wrap_content" android:id="@+id/tv_shopname" android:gravity="right" style="@style/text_white_15" android:layout_gravity="center_vertical" /> <TextView android:id="@+id/tv_shezhi" android:layout_width="@dimen/main_padding_25" android:layout_height="@dimen/main_padding_25" android:background="@drawable/shezhi" android:focusable="true" android:layout_marginLeft="@dimen/main_padding_06" /> </LinearLayout> </FrameLayout>
LoginView.java
/** * Created by dawn on 2017/11/29. * 用来控制登录成功或失败的显示 */ public class LoginView extends LinearLayout implements View.OnFocusChangeListener { private LinearLayout ll_success, ll_fail; private TextView tv_shopname, tv_shezhi; private TextView tv_login_on;//登录成功 private EditText et_shop_id, et_shop_pwd;//输入用户id和密码 public LoginView(Context context) { super(context); } public LoginView(Context context, AttributeSet attrs) { super(context, attrs); LayoutInflater inflater = (LayoutInflater) context .getSystemService(Context.LAYOUT_INFLATER_SERVICE); inflater.inflate(R.layout.view_login, this); setupView(); } private void setupView() { ll_success = (LinearLayout) findViewById(R.id.ll_success); ll_fail = (LinearLayout) findViewById(R.id.ll_fail); tv_shopname = (TextView) findViewById(R.id.tv_shopname); tv_shezhi = (TextView) findViewById(R.id.tv_shezhi); et_shop_id = (EditText) findViewById(R.id.et_shop_id); et_shop_pwd = (EditText) findViewById(R.id.et_shop_pwd); tv_login_on = (TextView) findViewById(R.id.tv_login_on); et_shop_id.setOnFocusChangeListener(this); et_shop_pwd.setOnFocusChangeListener(this); tv_login_on.setOnFocusChangeListener(this); } public void setSuccess(String shopname) { ll_success.setVisibility(View.VISIBLE); ll_fail.setVisibility(View.GONE); if (!SysUtils.isEmpty(shopname)) { tv_shopname.setText(shopname); tv_shezhi.requestFocus(); tv_shezhi.setBackgroundResource(R.drawable.shezhi_after); } else { set_fail(); } tv_shezhi.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { set_fail(); } }); } public void set_fail() { ll_success.setVisibility(View.GONE); ll_fail.setVisibility(View.VISIBLE); et_shop_id.requestFocus(); } public void setOnLoginListener(OnClickListener listener) { if (listener == null) return; tv_login_on.setOnClickListener(listener); } public String getSid() { return et_shop_id.getText().toString(); } public String getSPwd() { return et_shop_pwd.getText().toString(); } @Override public void onFocusChange(View v, boolean hasFocus) { if (hasFocus) { switch (v.getId()) { case R.id.et_shop_id: et_shop_id.setBackgroundColor(0x1effffff); break; case R.id.et_shop_pwd: et_shop_pwd.setBackgroundColor(0x1effffff); break; case R.id.tv_login_on: tv_login_on.setBackgroundColor(0x1effffff); break; case R.id.tv_shezhi: tv_shezhi.setBackgroundResource(R.drawable.shezhi_after); break; } } else { switch (v.getId()) { case R.id.et_shop_id: et_shop_id.setBackgroundColor(0x0affffff); break; case R.id.et_shop_pwd: et_shop_pwd.setBackgroundColor(0x0affffff); break; case R.id.tv_login_on: tv_login_on.setBackgroundColor(0x0affffff); break; case R.id.tv_shezhi: tv_shezhi.setBackgroundResource(R.drawable.shezhi); break; } } } }
调用方式:
在xml中定义:
<com.*******.custom.LoginView android:id="@+id/loginview" android:layout_alignParentRight="true" android:layout_width="wrap_content" android:layout_height="wrap_content"/>
.java中:
private LoginView loginview=null;
loginview = (LoginView) findViewById(R.id.loginview);
//为确认按钮设置的监听事件
loginview.setOnLoginListener(new View.OnClickListener() { @Override public void onClick(View v) { //执行具体的登录操作 } });
根据返回结果来设置状态:
loginview.setSuccess(sname);
loginview.set_fail();
tips: 1) nextFocusLeft 或nextFocusRight来设置焦点
2)需要设置焦点的控件 focusable必须为true
3)requestFocus 强制获取焦点