首页 > 解决方案 > 为火棒制作应用程序,但按钮单击和悬停不起作用

问题描述

我正在为 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);
                }
            }
        });
    }

标签: javaandroidandroid-tvfirestick

解决方案


在 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>

推荐阅读