Android 底栏菜单 onclick 操作

发布于 2024-11-08 12:20:26 字数 335 浏览 0 评论 0原文

我希望每个页面的应用程序都有一个通用的底部菜单栏。我设计了底部栏,但我对菜单图标的 onClick 事件感到困惑。我是否必须为每个活动类编写 onClick 侦听器的代码以使栏可见并在每个页面中工作,或者是否有任何其他方式我可以创建一个位于每个页面的公共底部栏,而无需在每个页面中编写菜单代码活动课。

我尝试通过创建一个基类并在其他子类中扩展它来创建, 正如 dave.c 在帖子 Android 创建底部栏菜单 中所述,但它不起作用为我。 请建议。谢谢。

I want to have a common bottom menu bar through out the applications with every page. I have designed the bottom bar but i am confused with onClick event of the menu icons. whether i have to write the code for onClick listener with every activity class to make bar visible and working in every page or if there is any other way i can create a common bottom bar that lies with every page without writing the code of menu in every activity class.

I tried to create through a creating a base class and extending it in other child classes,
as stated by dave.c in the post Android creating Bottom Bar Menu but it didnt work for me.
Please suggest. Thanks.

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

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

发布评论

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

评论(1

心欲静而疯不止 2024-11-15 12:20:26

简单示例(如 dave.c 建议的那样):

public class BaseActivity extends Activity {

    public void onClickButton1(View view) {
            Toast toast = Toast.makeText(this, "Button 1 clicked", Toast.LENGTH_SHORT);
            toast.show();
    }

    public void onClickButton2(View view) {
            Intent i = new Intent(this, MyFirstActivity.class);
            startActivity(i);
    }

    public void onClickButton3(View view) {
            Intent i = new Intent(this, MySecondActivity.class);
            startActivity(i);
    }
}

您的 MyFirstActivity 将如下所示:

public class MyFirstActivity extends BaseActivity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.my_first_activity);
    }
}

您的 MySecondActivity 活动:

public class MySecondActivity extends BaseActivity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.my_second_activity);
    }
}

在 my_first_activity.xml 布局中,您包括:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">    
    <TextView android:text="My first activity" android:layout_width="wrap_content"
         android:layout_height="wrap_content"/>
    <include android:layout_width="fill_parent" android:layout_height="wrap_content" 
         layout="@layout/bottom_bar" />
</LinearLayout>

在 my_second_activity.xml 中:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <TextView android:text="My second activity" android:layout_width="wrap_content" 
        android:layout_height="wrap_content"/>
    <include android:layout_width="fill_parent" android:layout_height="wrap_content" 
        layout="@layout/bottom_bar" />
</LinearLayout>

在 Bottom_bar 中,您定义带有 onClick 处理程序的按钮:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
    <Button android:text="Button1" android:id="@+id/button1" android:layout_width="wrap_content" 
        android:layout_height="wrap_content" android:onClick="onClickButton1"/>
    <Button android:text="Button2" android:id="@+id/button2" android:layout_width="wrap_content" 
        android:layout_height="wrap_content" android:onClick="onClickButton2"/>
    <Button android:text="Button3" android:id="@+id/button3" android:layout_width="wrap_content" 
        android:layout_height="wrap_content" android:onClick="onClickButton3"/>
</LinearLayout>

使用此设计可能会遇到问题。例如,当您想在某些活动中使用 ListView 并希望子类化 ListActivity(TabActivity 是另一个示例)时,这是不可能的。

另一种方法是子类化 Activity 并定义一个处理 onClick 事件的通用处理程序。在这种情况下,您需要在每个活动中定义 onClick 处理程序并调用相应的公共处理程序的方法。

另一种方法是使用 TabHost 和 TabActivity。

Simple example (as dave.c suggested):

public class BaseActivity extends Activity {

    public void onClickButton1(View view) {
            Toast toast = Toast.makeText(this, "Button 1 clicked", Toast.LENGTH_SHORT);
            toast.show();
    }

    public void onClickButton2(View view) {
            Intent i = new Intent(this, MyFirstActivity.class);
            startActivity(i);
    }

    public void onClickButton3(View view) {
            Intent i = new Intent(this, MySecondActivity.class);
            startActivity(i);
    }
}

Your MyFirstActivity will look like:

public class MyFirstActivity extends BaseActivity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.my_first_activity);
    }
}

Your MySecondActivity activity:

public class MySecondActivity extends BaseActivity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.my_second_activity);
    }
}

In my_first_activity.xml layout you include:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">    
    <TextView android:text="My first activity" android:layout_width="wrap_content"
         android:layout_height="wrap_content"/>
    <include android:layout_width="fill_parent" android:layout_height="wrap_content" 
         layout="@layout/bottom_bar" />
</LinearLayout>

In my_second_activity.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <TextView android:text="My second activity" android:layout_width="wrap_content" 
        android:layout_height="wrap_content"/>
    <include android:layout_width="fill_parent" android:layout_height="wrap_content" 
        layout="@layout/bottom_bar" />
</LinearLayout>

In bottom_bar you define buttons with onClick handlers:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
    <Button android:text="Button1" android:id="@+id/button1" android:layout_width="wrap_content" 
        android:layout_height="wrap_content" android:onClick="onClickButton1"/>
    <Button android:text="Button2" android:id="@+id/button2" android:layout_width="wrap_content" 
        android:layout_height="wrap_content" android:onClick="onClickButton2"/>
    <Button android:text="Button3" android:id="@+id/button3" android:layout_width="wrap_content" 
        android:layout_height="wrap_content" android:onClick="onClickButton3"/>
</LinearLayout>

You might run into problems using this design. For example when you want to use ListView in some of your activities and you want to subclass ListActivity (TabActivity is another example) it will be not possible.

Another way is to subclass Activity and define one common handler that handles onClick events. In this case you will need to define onClick handlers in each activity and call corresponding common handler's methods.

Yet another way is to use TabHost and TabActivity.

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