如何为 Android 中的应用程序创建通用菜单

发布于 2024-11-08 19:30:38 字数 1955 浏览 1 评论 0原文

我能够创建一个选项菜单,请参阅下面的代码。但在我的程序中,问题是我必须在每个活动中编写逻辑。

如果我有一个包含 10 项活动的应用程序。我必须为到处的菜单编写代码。

我想为整个应用程序创建一个菜单。无论我在哪里。如果我单击硬件菜单键,那么我一定能够进入菜单。如何创建这种类型的通用菜单?

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.Toast;

public class SimpleOptionMenu extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.animation_test);
    }

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

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        case R.id.icon:
            Toast.makeText(this, "You pressed the icon!", Toast.LENGTH_LONG)
                    .show();
            break;
        case R.id.text:
            Toast.makeText(this, "You pressed the text!", Toast.LENGTH_LONG)
                    .show();
            break;
        case R.id.icontext:
            Toast.makeText(this, "You pressed the icon and text!",
                    Toast.LENGTH_LONG).show();
            break;
        }
        return true;
    }

}

动画测试.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <Button  
        android:id="@+id/btn_ani"
        android:layout_alignParentTop="true"
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" 
        android:text="Click to start animation"/>

</RelativeLayout>

I am able to create an OptionMenu, see code below. But in my program the problem is that I have to write logic in every activity.

If I have an application with 10 activities. I have to write code for the menu everywhere.

I want to create a single menu for the whole application. No matter where i am. If I click on the Hardware Menu key then I must be able to get to the Menu. How do I create this type of universal Menu?

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.Toast;

public class SimpleOptionMenu extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.animation_test);
    }

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

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        case R.id.icon:
            Toast.makeText(this, "You pressed the icon!", Toast.LENGTH_LONG)
                    .show();
            break;
        case R.id.text:
            Toast.makeText(this, "You pressed the text!", Toast.LENGTH_LONG)
                    .show();
            break;
        case R.id.icontext:
            Toast.makeText(this, "You pressed the icon and text!",
                    Toast.LENGTH_LONG).show();
            break;
        }
        return true;
    }

}

animation_test.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <Button  
        android:id="@+id/btn_ani"
        android:layout_alignParentTop="true"
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" 
        android:text="Click to start animation"/>

</RelativeLayout>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

羞稚 2024-11-15 19:30:38

只需使用 onOptionsSelected 方法创建一个新的 Activity 并扩展此创建的 Activity 即可。

class OptionsActivity extends Activity {

    public boolean onOptionsItemSelected(MenuItem item) {
       ...
    }

}

class SimpleOptionsMenu extends OptionsActivity {
   ...
}

Simply create a new Activity with the onOptionsSelected method and extend this created Activity.

class OptionsActivity extends Activity {

    public boolean onOptionsItemSelected(MenuItem item) {
       ...
    }

}

class SimpleOptionsMenu extends OptionsActivity {
   ...
}
善良天后 2024-11-15 19:30:38

如果您想在任何地方创建具有相同操作的相同菜单,您可以创建一个扩展 Activity 的抽象类 ActivityHelper。
您可以重写 onCreateOptionsMenu 和 onOptionsItemSelected 来放置您的逻辑。
然后你只需要在你的活动中扩展 BaseActivity 即可。

那应该可以解决问题!

干杯,

If you want to create the same menu with the same actions everywhere you can create an abstract class ActivityHelper which extends Activity.
You can override onCreateOptionsMenu and onOptionsItemSelected to put your logic.
Then you just have to extends BaseActivity in your activities.

That should do the trick!

Cheers,

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文