简体   繁体   中英

How do I set focus to the title on the Action bar?

I don't seem to be able to set focus to top and beginning of the Action Bar. Right now it goes to the editText box in the middle of the screen. I am using the default method for creating the Action Bar and I have an Overflow Menu setup. Is there a way to requestFocus to the Action Bar title?

thanks

MainActivity.java

package com.example.grady.actiondemo;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuItem;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu){
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item){

        int id = item.getItemId();

        if (id == R.id.action_settings){
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

activity_main.xml

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.grady.actiondemo.MainActivity">

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="16dp"
        android:layout_marginRight="16dp"
        android:layout_marginTop="16dp"
        android:gravity="center_vertical|center"
        android:text="Hello World!"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintHorizontal_bias="0.051"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.032"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"/>

    <TextView
        android:id="@+id/fName"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="16dp"
        android:layout_marginTop="29dp"
        android:text="First Name:"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/textView2"
        android:labelFor="@+id/fNameEditText"
        android:layout_below="@+id/textView2"
        android:layout_alignStart="@+id/fNameEditText"/>

    <EditText
        android:id="@+id/fNameEditText"
        android:layout_width="281dp"
        android:layout_height="24dp"
        android:ems="10"
        android:inputType="textPersonName"
        android:text=""
        app:layout_constraintBottom_toBottomOf="@+id/fName"
        app:layout_constraintLeft_toRightOf="@+id/fName"
        android:layout_marginLeft="16dp"
        android:layout_marginRight="16dp"
        app:layout_constraintRight_toRightOf="parent"
        android:layout_marginTop="24dp"
        android:layout_below="@+id/fName"
        android:layout_alignEnd="@+id/textView2"/>

</RelativeLayout>

menu_main.xml

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:app="http://schemas.android.com/apk/res-auto">

    <item
        android:id="@+id/action_settings"
        android:title="Settings"
        android:orderInCategory="100"
        app:showAsAction="never" />

    <item
        android:id="@+id/action_file"
        android:title="File"
        android:orderInCategory="200"
        app:showAsAction="never" />

    <item
        android:id="@+id/action_directory"
        android:title="Directory"
        android:orderInCategory="300"
        app:showAsAction="never"/>
</menu>

By default, android automatically focuses on the Edittext present in your activity where ever it is present in your activity. So to stop that from happening we need to set

android:windowSoftInputMode="stateHidden"

from your AndroidManifest.xml file for your activity containing EditText elements.This also prevents from opening keyboard on the screen. I hope this can solve your problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM