@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.bind(this);
}
@OnClick(R.id.btn_test)
public void onViewClicked() {
//Toast 提示
Toast.makeText(this,tvTest.getText().toString(),Toast.LENGTH_SHORT).show();
}
}
这个时候你应该迫不及待的想要运行一下了吧,我们还有一步就是主题的设置
打开values下面的styles.xml文件
我们不用它这个主题,重新创建一个
styles.xml的完整代码如下:
<?xml version="1.0" encoding="utf-8"?>
然后你会发现少几个颜色,这时候我们在values文件夹下面创建一个colors.xml的文件
colors.xml代码如下:
<?xml version="1.0" encoding="utf-8"?>
#008577
#00574B
#D81B60
#1EBADE
然后就要使用我们新创建的主题了,打开AndroidManifest.xml文件
修改,AppTheme,改成AppTheme2,然后运行项目.运行效果如下图
这个时候你没有想过,我怎么点击这个按钮呢?电视机都是用遥控器的啊,遥控器又怎么操作呢?
这些问题一定在你的脑海里面环绕着,我们注意到,电视机使用遥控器,而我们的手机使用手指触摸点击,这个不能混为一谈,所以电视上需要用到焦点电视上都是通过控件获取焦点来实现点击效果的,我们在布局文件的button中写入
android:focusable=“true”
意思就是可以获取到焦点,为false则不可获取焦点,
在代码里
btnTest.setFocusable(true);
为false则不可获取焦点。
在已知控件ID的情况下我们可以设置上下左右的移动控件,
android:nextFocusUp=“@id/tv_test”
android:nextFocusDown=“@id/tv_test”
android:nextFocusLeft=“@id/tv_test”
android:nextFocusRight=“@id/tv_test”
代码中:
btnTest.setNextFocusUpId(R.id.tv_test);
btnTest.setNextFocusDownId(R.id.tv_test);
btnTest.setNextFocusLeftId(R.id.tv_test);
btnTest.setNextFocusRightId(R.id.tv_test);
了解这个之后,我们还得知道遥控器的按键监听,毕竟是用遥控器来操作的啊,按键监听代码如下:
private String TAG = “key”;
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
switch (keyCode) {
case KeyEvent.KEYCODE_ENTER: //确定键enter
case KeyEvent.KEYCODE_DPAD_CENTER:
Log.d(TAG, “enter—>”);
break;
case KeyEvent.KEYCODE_BACK: //返回键
Log.d(TAG,“back—>”);
return true; //这里由于break会退出,所以我们自己要处理掉 不返回上一层
case KeyEvent.KEYCODE_SETTINGS: //设置键
Log.d(TAG, “setting—>”);
break;
case KeyEvent.KEYCODE_DPAD_DOWN: //向下键
/* 实际开发中有时候会触发两次,所以要判断一下按下时触发 ,松开按键时不触发
• exp:KeyEvent.ACTION_UP
*/
if (event.getAction() == KeyEvent.ACTION_DOWN) {
Log.d(TAG, “down—>”);
}
break;
case KeyEvent.KEYCODE_DPAD_UP: //向上键
Log.d(TAG, “up—>”);
break;
case KeyEvent.KEYCODE_0: //数字键0
Log.d(TAG, “0—>”);
break;
case KeyEvent.KEYCODE_DPAD_LEFT: //向左键
Log.d(TAG, “left—>”);
break;
case KeyEvent.KEYCODE_DPAD_RIGHT: //向右键
Log.d(TAG, “right—>”);
break;
case KeyEvent.KEYCODE_INFO: //info键
Log.d(TAG, “info—>”);
break;
case KeyEvent.KEYCODE_PAGE_DOWN: //向上翻页键
case KeyEvent.KEYCODE_MEDIA_NEXT:
Log.d(TAG, “page down—>”);
break;
case KeyEvent.KEYCODE_PAGE_UP: //向下翻页键
case KeyEvent.KEYCODE_MEDIA_PREVIOUS:
Log.d(TAG, “page up—>”);
break;
case KeyEvent.KEYCODE_VOLUME_UP: //调大声音键
Log.d(TAG, “voice up—>”);
break;
case KeyEvent.KEYCODE_VOLUME_DOWN: //降低声音键
Log.d(TAG, “voice down—>”);
break;
case KeyEvent.KEYCODE_VOLUME_MUTE: //禁用声音
Log.d(TAG, “voice mute—>”);
break;
default:
break;
}
return super.onKeyDown(keyCode, event);
}
如果你要监听Home键的话,就需要通过广播来,
在MainActivity中创建一个class
class HomeReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if(action.equals(Intent.ACTION_CLOSE_SYSTEM_DIALOGS)){
String reason = intent.getStringExtra(SYSTEM_DIALOG_REASON_KEY);
if(SYSTEM_DIALOG_REASON_HOME_KEY.equals(reason)){
Toast.makeText(MainActivity.this,“home键触发”,Toast.LENGTH_SHORT).show();
Log.d(TAG, “home键触发”);
}
}
}
}
在onCreate()方法中注册广播,只要调用initReceiver()方法即可
public final String SYSTEM_DIALOG_REASON_KEY = “reason”;
public final String SYSTEM_DIALOG_REASON_HOME_KEY = “homekey”;
private HomeReceiver homeReceiver;
/**
• 注册广播
*/
private void initReceiver() {
homeReceiver = new HomeReceiver();
IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
registerReceiver(homeReceiver, filter);
}
页面销毁时,注销掉广播
@Override
protected void onDestroy() {
super.onDestroy();
if(homeReceiver!=null){
unregisterReceiver(homeReceiver);
}
}
这段代码我也是从网上找的,
然后我们在确定键的下面弹出这个Toast
case KeyEvent.KEYCODE_ENTER: //确定键enter
case KeyEvent.KEYCODE_DPAD_CENTER:
Log.d(TAG, “enter—>”);
Toast.makeText(this,tvTest.getText().toString(),Toast.LENGTH_SHORT).show();
break;
运行效果如下:
MainActivity.java完整代码如下:
package com.llw.androidtvdemo;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
public class MainActivity extends AppCompatActivity {
@BindView(R.id.tv_test)
TextView tvTest;
@BindView(R.id.btn_test)
Button btnTest;
public final String SYSTEM_DIALOG_REASON_KEY = “reason”;
public final String SYSTEM_DIALOG_REASON_HOME_KEY = “homekey”;
private HomeReceiver homeReceiver;
/**
• 注册广播
*/
private void initReceiver() {
homeReceiver = new HomeReceiver();
IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
registerReceiver(homeReceiver, filter);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.bind(this);
btnTest.setFocusable(true);
initReceiver();
}
@OnClick(R.id.btn_test)
public void onViewClicked() {
//Toast 提示
Toast.makeText(this,tvTest.getText().toString(),Toast.LENGTH_SHORT).show();
}
class HomeReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if(action.equals(Intent.ACTION_CLOSE_SYSTEM_DIALOGS)){
String reason = intent.getStringExtra(SYSTEM_DIALOG_REASON_KEY);
if(SYSTEM_DIALOG_REASON_HOME_KEY.equals(reason)){
Toast.makeText(MainActivity.this,“home键触发”,Toast.LENGTH_SHORT).show();
Log.d(TAG, “home键触发”);
}
}
}
}
private String TAG = “key”;
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
switch (keyCode) {
case KeyEvent.KEYCODE_ENTER: //确定键enter
case KeyEvent.KEYCODE_DPAD_CENTER:
Log.d(TAG, “enter—>”);
Toast.makeText(this,tvTest.getText().toString(),Toast.LENGTH_SHORT).show();
break;
case KeyEvent.KEYCODE_BACK: //返回键
Log.d(TAG,“back—>”);
return true; //这里由于break会退出,所以我们自己要处理掉 不返回上一层
case KeyEvent.KEYCODE_SETTINGS: //设置键
Log.d(TAG, “setting—>”);
break;
case KeyEvent.KEYCODE_DPAD_DOWN: //向下键
/* 实际开发中有时候会触发两次,所以要判断一下按下时触发 ,松开按键时不触发
• exp:KeyEvent.ACTION_UP
*/
if (event.getAction() == KeyEvent.ACTION_DOWN) {
Log.d(TAG, “down—>”);
}
break;
case KeyEvent.KEYCODE_DPAD_UP: //向上键
Log.d(TAG, “up—>”);
break;
case KeyEvent.KEYCODE_0: //数字键0
Log.d(TAG, “0—>”);
break;
case KeyEvent.KEYCODE_DPAD_LEFT: //向左键
Log.d(TAG, “left—>”);
break;
case KeyEvent.KEYCODE_DPAD_RIGHT: //向右键
Log.d(TAG, “right—>”);
break;
case KeyEvent.KEYCODE_INFO: //info键
Log.d(TAG, “info—>”);
break;
case KeyEvent.KEYCODE_PAGE_DOWN: //向上翻页键
case KeyEvent.KEYCODE_MEDIA_NEXT:
Log.d(TAG, “page down—>”);
break;
case KeyEvent.KEYCODE_PAGE_UP: //向下翻页键
case KeyEvent.KEYCODE_MEDIA_PREVIOUS:
Log.d(TAG, “page up—>”);
break;
case KeyEvent.KEYCODE_VOLUME_UP: //调大声音键
Log.d(TAG, “voice up—>”);
break;
case KeyEvent.KEYCODE_VOLUME_DOWN: //降低声音键
Log.d(TAG, “voice down—>”);
break;
case KeyEvent.KEYCODE_VOLUME_MUTE: //禁用声音
Log.d(TAG, “voice mute—>”);
break;
default:
break;
}
return super.onKeyDown(keyCode, event);
}
@Override
protected void onDestroy() {
super.onDestroy();
if(homeReceiver!=null){
unregisterReceiver(homeReceiver);
}
}
}
然后我们就要想一下编码的过程和逻辑问题了,
1.播放视频的来源 本地 和 网络
2.播放视频的的停止播放、继续播放、重新播放
3.播放视频时的时间和进度计算
4.播放时候按遥控器左右键时,前进 后退
先想清楚这些问题,才能使编码过程中变得有条理