为 firestick 制作应用程序,但按钮单击和悬停不起作用

问题描述

我正在为 firestick 制作一个应用程序,但没有点击或导航抽屉无法使用 Firestick Remote。

public class MainActivity extends AppCompatActivity {

    int currentApiVersion;
    SharedPreferences sharedpreferences;
    String code  = "";
    public static  DrawerLayout drawer_layout;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getwindow().setSoftInputMode(WindowManager.LayoutParams.soFT_INPUT_STATE_ALWAYS_HIDDEN);
        currentApiVersion = Build.VERSION.SDK_INT;
        HideHomeButton.hidebar2(this);
        setContentView(R.layout.activity_new_main);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            getwindow().setNavigationBarColor(ContextCompat.getColor(this,android.R.color.black));
        }


        TextView  menu_code = findViewById(R.id.menu_code);
         drawer_layout = findViewById(R.id.drawer_layout);

        sharedpreferences = this.getSharedPreferences("code_prefrances",Context.MODE_PRIVATE);
        code = sharedpreferences.getString("code","");
        menu_code.setText(code);

        HomeFragment fragment = new HomeFragment();
        FragmentManager manager =  getSupportFragmentManager();;
        FragmentTransaction transaction = manager.beginTransaction();
        transaction.replace(R.id.container,fragment).addToBackStack(null);
        transaction.commit();

        findViewById(R.id.reset_code).setonClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {


                Intent intent = new Intent(getApplicationContext(),CodeInputActvity.class);
                startActivity(intent);
                if (drawer_layout.isDrawerOpen(GravityCompat.END)) {
                    drawer_layout.closeDrawer(GravityCompat.END);
                }
            }
        });

        findViewById(R.id.refresh).setonClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                Intent intent = new Intent(MainActivity.this,MainActivity.class);
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_CLEAR_TOP|Intent.FLAG_ACTIVITY_CLEAR_TASK);
                startActivity(intent);
                finish();
                if (drawer_layout.isDrawerOpen(GravityCompat.END)) {
                    drawer_layout.closeDrawer(GravityCompat.END);
                }
            }
        });

        findViewById(R.id.reboot_app).setonClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                SharedPreferences sharedpreferences = getSharedPreferences("code_prefrances",Context.MODE_PRIVATE);
                SharedPreferences.Editor editor = sharedpreferences.edit();
                editor.remove("code");
                editor.commit();
                Intent intent = new Intent(getApplicationContext(),SplashActivity.class);
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_CLEAR_TOP|Intent.FLAG_ACTIVITY_CLEAR_TASK);
                startActivity(intent);
                finish();
                if (drawer_layout.isDrawerOpen(GravityCompat.END)) {
                    drawer_layout.closeDrawer(GravityCompat.END);
                }
            }
        });
    }

解决方法

在 XML 中对我有用

<requestFocus/>

为了获得焦点,你需要像这样的背景选择器

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/button_text_hover" android:state_pressed="true"></item>
    <item android:drawable="@drawable/button_text_hover" android:state_activated="true"></item>
    <item android:drawable="@drawable/button_text_hover" android:state_focused="true"></item>

   <item android:drawable="@drawable/button_text_normal" ></item>
</selector>

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...