智能TV开发笔记(三)简单的焦点控制--继续篇

来源:互联网 发布:福州天勤网络 编辑:程序博客网 时间:2024/06/06 16:58

          最初接手电视时,有个朋友发我一个demo,是关于焦点的。今天,我就想来仔细阅读一下这个demo.

         从第一个MainActivity开始


        

布局代码:activity_main.xml

<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent">    <LinearLayout        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_centerInParent="true"        android:orientation="vertical">        <Button            android:id="@+id/btn_normal"            android:layout_width="200dp"            android:layout_height="80dp"            android:textAllCaps="false"            android:text="normal focus"/>        <Button            android:id="@+id/btn_module"            android:layout_width="200dp"            android:layout_height="80dp"            android:textAllCaps="false"            android:text="module focus - horizontal"/>        <Button            android:id="@+id/btn_module_vertical"            android:layout_width="200dp"            android:layout_height="80dp"            android:textAllCaps="false"            android:text="module focus - vertical"/>        <Button            android:id="@+id/btn_auto_focus"            android:layout_width="200dp"            android:layout_height="80dp"            android:textAllCaps="false"            android:text="auto process focus"/>        <Button            android:id="@+id/btn_maul_focus"            android:layout_width="200dp"            android:layout_height="80dp"            android:textAllCaps="false"            android:text="no focus - horizontal"/>        <Button            android:id="@+id/btn_maul_vertical_focus"            android:layout_width="200dp"            android:layout_height="80dp"            android:textAllCaps="false"            android:text="no focus - vertical"/>        <TextView            android:id="@+id/tv_test"            android:layout_width="200dp"            android:layout_height="80dp"            android:text="test"            android:background="@color/brown"            android:gravity="center"            android:focusable="true"            />    </LinearLayout></RelativeLayout>
   tv_test这个按钮是我自己添加的。我主要是想说textview和button两者的区别。如果控件是button,可以不用添加focusable=true,不添加点击事件,它自己都可以获取到焦点。按着从上到下的顺序随着遥控器移动。如果是textview,则一定要写上focusable=true,否则,即使你写了点击事件也是没有用的。

MainActivity.java

package com.app.tvrecyclerview;import android.content.Intent;import android.os.Bundle;import android.support.v7.app.AppCompatActivity;import android.view.View;import android.widget.Button;import android.widget.TextView;import android.widget.Toast;public class MainActivity extends AppCompatActivity {    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        Button btnNormal = (Button) findViewById(R.id.btn_normal);        btnNormal.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                Intent intent = new Intent(MainActivity.this, NormalFocusActivity.class);                startActivity(intent);            }        });        Button btnModule = (Button) findViewById(R.id.btn_module);        btnModule.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                Intent intent = new Intent(MainActivity.this, ModuleFocusActivity.class);                startActivity(intent);            }        });        Button btnModuleV = (Button) findViewById(R.id.btn_module_vertical);        btnModuleV.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                Intent intent = new Intent(MainActivity.this, ModuleFocusVerticalActivity.class);                startActivity(intent);            }        });        Button btnAuto = (Button) findViewById(R.id.btn_auto_focus);        btnAuto.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                Intent intent = new Intent(MainActivity.this, AutoCarouselActivity.class);                startActivity(intent);            }        });        Button btnMaulHorizontal = (Button) findViewById(R.id.btn_maul_focus);        btnMaulHorizontal.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                Intent intent = new Intent(MainActivity.this, maulCarouselActivity.class);                startActivity(intent);            }        });        Button btnMaulVertical = (Button) findViewById(R.id.btn_maul_vertical_focus);        btnMaulVertical.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                Intent intent = new Intent(MainActivity.this, maulVerticalCarouselActivity.class);                startActivity(intent);            }        });        final TextView tv_test= (TextView) findViewById(R.id.tv_test);        tv_test.setOnFocusChangeListener(new View.OnFocusChangeListener() {            @Override            public void onFocusChange(View v, boolean hasFocus) {                if(hasFocus){                    //选中的焦点改变颜色                    tv_test.setBackgroundColor(0xff795548);                }else{                    tv_test.setBackgroundColor(0xffd2d6d8);                }            }        });    }}

再如我写一个选择对话框:


<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:orientation="vertical" android:layout_width="@dimen/width_dialog"    android:background="@drawable/bg_dialog_1"    android:padding="@dimen/main_padding_30"    android:layout_height="wrap_content">    <TextView        android:id="@+id/tv_content"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="检测到新版本,是否更新"        style="@style/text_white_14"        />   <LinearLayout       android:layout_width="match_parent"       android:layout_height="wrap_content"       android:layout_marginTop="@dimen/main_padding_20"       android:orientation="horizontal"       >       <Button           android:id="@+id/tv_ok"           android:layout_width="0dp"           android:layout_height="wrap_content"           android:layout_weight="1"           android:text="是"           style="@style/text_white_14"           android:gravity="center"           android:background="@drawable/btn_unselected"           />       <Button           android:id="@+id/tv_cancel"           android:layout_width="0dp"           android:layout_height="wrap_content"           android:layout_weight="1"           android:text="否"           style="@style/text_white_14"           android:gravity="center"           android:layout_marginLeft="@dimen/main_padding_20"           android:background="@drawable/btn_unselected"           />   </LinearLayout></LinearLayout>
public static Dialog getVersionUpdateDialog(Activity thisRef, String content,OnClickListener oklistener,OnClickListener closelistener) {    final Dialog dialog = new Dialog(thisRef, R.style.PickDialog);    dialog.setCanceledOnTouchOutside(false);    dialog.show();    Window window = dialog.getWindow();    window.setContentView(R.layout.dialog_version);    window.setBackgroundDrawableResource(android.R.color.transparent);    WindowManager.LayoutParams lp = window.getAttributes();    lp.width = ViewGroup.LayoutParams.WRAP_CONTENT;    lp.height = ViewGroup.LayoutParams.WRAP_CONTENT;    lp.gravity = Gravity.CENTER_HORIZONTAL;    window.setAttributes(lp);    TextView tvcontent = (TextView) window.findViewById(R.id.tv_content);    final Button tv_ok = (Button) window.findViewById(R.id.tv_ok);    final Button tv_cancel = (Button) window.findViewById(R.id.tv_cancel);    tvcontent.setText(Html.fromHtml(content));    if(oklistener!=null){        tv_ok.setOnClickListener(oklistener);    }else{        tv_ok.setOnClickListener(new OnClickListener() {            @Override            public void onClick(View v) {                dialog.dismiss();            }        });    }    if(closelistener!=null){        tv_cancel.setOnClickListener(closelistener);    }else {        tv_cancel.setOnClickListener(new OnClickListener() {            @Override            public void onClick(View v) {                dialog.dismiss();            }        });    }    tv_ok.setOnFocusChangeListener(new View.OnFocusChangeListener() {        @Override        public void onFocusChange(View v, boolean hasFocus) {            if(hasFocus){                tv_ok.setBackgroundResource(R.drawable.btn_selected);            }else{                tv_ok.setBackgroundResource(R.drawable.btn_unselected);            }        }    });    tv_cancel.setOnFocusChangeListener(new View.OnFocusChangeListener() {        @Override        public void onFocusChange(View v, boolean hasFocus) {            if(hasFocus){                tv_cancel.setBackgroundResource(R.drawable.btn_selected);            }else{                tv_cancel.setBackgroundResource(R.drawable.btn_unselected);            }        }    });    return dialog;}


阅读全文
0 0