侧边菜单导航在Android中不起作用

时间:2018-07-23 12:53:16

标签: java android xml android-layout android-studio

当我单击侧面菜单项时无响应。仅在此处创建打开和关闭侧边菜单导航。我尝试单击侧面菜单导航列表中的项目,然后显示为Toast或形成其他形式。

我的问题是在Main XML文件中,所以请告诉我它在哪里。对不起,我的英语不好。

MainPage这是我的代码侧菜单导航:

public class MainPage extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {
    Button btncount, btnlettercounter;
    EditText text;
    public Toolbar toolbar;
    public TabLayout tabLayout;
    public ViewPager viewPager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main_page);
        btncount = (Button) findViewById(R.id.count);
        text = (EditText) findViewById(R.id.text);
        btnlettercounter = (Button) findViewById(R.id.letterscount);
        toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setDisplayShowTitleEnabled(true);

        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
        drawer.addDrawerListener(toggle);
        toggle.syncState();

        NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
        navigationView.setNavigationItemSelectedListener(this);

        btncount.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String words = text.getText().toString().trim();
                int validLetterCount = CountValidLetters(text.getText().toString().trim());
                Toast.makeText(getApplication(), "Total words is: " + CountWords(words), Toast.LENGTH_SHORT).show();
            }
        });

        btnlettercounter.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String words = text.getText().toString().trim();
                int validLetterCount = CountValidLetters(text.getText().toString().trim());
                Toast.makeText(getApplication(), "Total Letters is: " + CountValidLetters(words), Toast.LENGTH_SHORT).show();
            }
        });
    }

    private int CountWords(String words) {
        String[] allWords = words.split("[ ]", -1);
        return allWords.length;
    }

    private int CountValidLetters(String s) {
        int lCount = 0;

        for (int i = 0; i < s.length(); i++) {
            char c = s.charAt(i);

            if (Character.isLetterOrDigit(c))
                lCount++;
        }

        return lCount;
    }

    @Override
    public boolean onNavigationItemSelected(@NonNull MenuItem item) {
        int id = item.getItemId();

        if (id == R.id.word){
            Toast.makeText(getApplication(),"More Apps", Toast.LENGTH_SHORT).show();
        }else if (id == R.id.letter){
            Toast.makeText(getApplication(),"More Apps", Toast.LENGTH_SHORT).show();
        }else if (id == R.id.sentence){
            Toast.makeText(this, item.getTitle(), Toast.LENGTH_SHORT).show();
        }else if (id == R.id.vowel){
            Toast.makeText(this, "sentence is ok", Toast.LENGTH_SHORT).show();
        }else if (id == R.id.moreapp){             Toast.makeText(getApplication(),"MoreApps",Toast.LENGTH_SHORT).show();
        }

        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        drawer.closeDrawer(GravityCompat.START);
        return true;
    }

主要XML

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout 
    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:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:openDrawer="start"
    tools:context="com.rbk.reactnativeapp.MainPage">

    <include
        layout="@layout/swipe_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

    <android.support.design.widget.NavigationView
        android:id="@+id/nav_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:fitsSystemWindows="true"
        app:headerLayout="@layout/nav_header"
        app:menu="@menu/menu" />

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <EditText
            android:id="@+id/text"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:hint="Write something"
            android:inputType="textMultiLine"
            android:gravity="center"/>

        <RelativeLayout
            android:id="@+id/bottomlayout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true">

            <Button
                android:id="@+id/count"
                android:layout_marginTop="20dp"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="words"
                android:textColor="#ffffff"
                android:background="@drawable/button"
                android:layout_centerVertical="true"
                android:padding="10dp"
                android:layout_alignParentLeft="true"
                android:layout_marginLeft="10dp"/>

            <Button
                android:id="@+id/letterscount"
                android:layout_marginTop="20dp"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Letters"
                android:textColor="#ffffff"
                android:background="@drawable/button"
                android:layout_centerVertical="true"
                android:padding="10dp"
                android:layout_alignParentRight="true"
                android:layout_marginRight="10dp"/>

        </RelativeLayout>
    </RelativeLayout>       
</android.support.v4.widget.DrawerLayout>

幻灯片布局XML:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <android.support.design.widget.AppBarLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="?attr/colorAccent">

            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                android:background="@drawable/header"
                app:layout_scrollFlags="scroll|enterAlways"/>

        </android.support.design.widget.AppBarLayout>

    </LinearLayout>

</android.support.constraint.ConstraintLayout>

菜单XML:

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

 <group android:checkableBehavior="single">
     <item
         android:id="@+id/word"
         android:icon="@drawable/ms_word"
         android:title="Word"/>

     <item
         android:id="@+id/letter"
         android:icon="@drawable/letters"
         android:title="Letter"/>

     <item
         android:id="@+id/vowel"
         android:icon="@drawable/vowel"
         android:title="Vowel"/>

     <item
         android:id="@+id/sentence"
         android:icon="@drawable/sentence"
         android:title="Sentence"/>

     <item
         android:id="@+id/digit"
         android:icon="@drawable/digit"
         android:title="Digit"/>
 </group>
    <item android:title="More app">
        <menu>
            <item
                android:id="@+id/moreapp"
                android:icon="@drawable/more"
                android:title="More App"/>
        </menu>
    </item>

</menu>

标题XML

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/header"
    android:gravity="bottom"
    android:orientation="vertical"
    android:padding="16dp"
    android:theme="@style/ThemeOverlay.AppCompat.Dark">

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingTop="16dp"
        android:src="@mipmap/ic_launcher"/>

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingTop="16dp"
        android:text="Words Counter"
        android:textColor="#ffffff"
        android:textAppearance="@style/TextAppearance.AppCompat.Body1"/>

    <TextView
        android:id="@+id/email"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="#ffffff"
        android:text="16bscs03@quest.edu.pk"/>

</LinearLayout>

0 个答案:

没有答案