简体   繁体   中英

How do I open a new page on menu item select in Kotlin?

I have an options menu in my app with a single item:

The menu is defined in 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_about"
        android:icon="@android:drawable/ic_menu_info_details"
        android:orderInCategory="100"
        android:title="@string/action_about"
        app:showAsAction="never" />
</menu>

The menu seems to be built in MainActivity.kt :

    override fun onCreateOptionsMenu(menu: Menu): Boolean {
        // Inflate the menu; this adds items to the action bar if it is present.
        menuInflater.inflate(R.menu.main, menu)
        return true
    }

I've created a simple Activity that I would like to display when the About item is selected from the menu defined in activity_about.xml :

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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=".About">

    <TextView
        android:id="@+id/aboutView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:linksClickable="false"
        android:text="@string/author"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

And an associated class in About.kt :

package com.example.rollme

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle

class About : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_about)
    }
}

How do I display the About activity when the About menu item is clicked?

You need override method onOptionsItemSelected and handle click on your item. Java example:

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    int id = item.getItemId();
    if(id == R.id.action_about) {
       startActivity(New Intent(this, About.class))
       return true;
    }else {
        return super.onOptionsItemSelected(item);
    }
}

Add this in your MainActivity :

override fun onOptionsItemSelected(item: MenuItem): Boolean {
    when(item.itemId)
    {R.id.action_about -> startActivity(Intent(this , About::class.java)) }
    return true
}

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