按钮没有焦点

时间:2018-11-26 22:58:49

标签: java android view focus

当我在诸如Amazon firestick之类的设备上添加隐私政策时,不会使用遥控器选择任何一个按钮。我认为这是因为此视图背后的导航抽屉具有焦点。在按下隐私策略中的“确认”按钮之前,如何从该视图中删除焦点?这样,我只需将NextfocusLeft和right添加到XML文件即可。

隐私政策Xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="@drawable/splash_background">

<TextView
    android:layout_marginTop="100sp"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:maxLines="1"
    android:text="P9p Studios privacy policy"
    android:textStyle="bold|italic"
    android:textColor="@color/buttontext_light"/>

<TextView
    android:id="@+id/privacy_text_view"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_margin="20sp"
    android:layout_marginTop="20dp"
    android:scrollbars="vertical"
    android:textColor="@color/buttontext_light"
    android:text="@string/Privacy_Text" />

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center_horizontal"
    android:orientation="horizontal">

    <Button
        android:id="@+id/accept"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/button"
        android:layout_marginRight="10sp"
        android:text="accept"
        android:nextFocusRight="@id/decline"
        />

    <Button
        android:id="@+id/decline"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/button"
        android:text="decline"  />
</LinearLayout>

我的导航抽屉和工具栏

 private void initToolbar() {
    toolbar = findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    ActionBar actionbar = getSupportActionBar();
    actionbar.setDisplayHomeAsUpEnabled(true);
    actionbar.setHomeAsUpIndicator(R.drawable.ic_menu_orange);
}

private void initNavDrawer() {
    drawerLayout = findViewById(R.id.drawer_layout);
    NavigationView navigationView = findViewById(R.id.navigation_view);

    setNavigationViewWidth(navigationView);

    navigationView.setNavigationItemSelectedListener(
            menuItem -> {
                menuItem.setChecked(true);
                selectedMenuItem = menuItem;

                drawerLayout.closeDrawers();

                if(menuItem.getItemId() == R.id.nav_add) {
                    Intent intent = new Intent(this, Add_Genre.class);
                    startActivity(intent);
                } else if(menuItem.getItemId() == R.id.nav_report) {
                    Intent intent = new Intent(this, Report.class);
                    startActivity(intent);
                }

显示我的隐私政策的首次运行代码

public void checkFirstRun() {

    final String PREFS_NAME = "MyPrefsFile";
    final String PREF_VERSION_CODE_KEY = "version_code";
    final int DOESNT_EXIST = -1;

    // Get current version code
    int currentVersionCode = BuildConfig.VERSION_CODE;

    // Get saved version code
    SharedPreferences prefs = c.getSharedPreferences(PREFS_NAME, MODE_PRIVATE);
    int savedVersionCode = prefs.getInt(PREF_VERSION_CODE_KEY, DOESNT_EXIST);

    // Check for first run or upgrade
    if (currentVersionCode == savedVersionCode) {
        // This is just a normal run

        return;

    } else if (savedVersionCode == DOESNT_EXIST) {
        db = FirebaseDatabase.getInstance();
        dbref = db.getReference().child(p9p_strings.APPNAME).child("User_Count").child("Newusers");
        App_DownLoadCounter();
        Button btn1;
        View view;
        LayoutInflater inflater = (LayoutInflater) c.getSystemService
                (Context.LAYOUT_INFLATER_SERVICE);
        view = inflater.inflate(R.layout.privacy_fragment, null);

        view.setLayoutParams(new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
        final RelativeLayout comp = rootview. findViewById(R.id.priv_layout_holder);
       Button btn0,btn;
        btn = view.findViewById(R.id.accept);
        btn.setOnClickListener(v -> comp.removeView(view));
        btn0 = view.findViewById(R.id.decline);
        btn0.setOnClickListener(v -> {
            comp.removeView(view);

            Intent intent = new Intent(Intent.ACTION_DELETE);
            intent.setData(Uri.parse("my package name:"));
            c.startActivity(intent);
            System.exit(0);
        });
        comp.addView(view);

        // TODO This is a new install (or the user cleared the shared preferences)

    }

使用

调用了我的MAin活动onstart()
 First_Run first_run = new First_Run(MainActivity.this,rlt);
    first_run.checkFirstRun();

1 个答案:

答案 0 :(得分:-1)

我不确定,但可以尝试

Button btn= (Button)findViewById(R.id.accept);
Button btn0= (Button)findViewById(R.id.decline);
btn.setOnClickListener(v -> {
        comp.removeView(view);

        btn0.requestFocus();

    });    
相关问题