Android的底部栏菜单的onclick行动 [英] Android bottom bar menu onclick actions

查看:141
本文介绍了Android的底部栏菜单的onclick行动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过有一个共同的底部菜单栏出与每个页面的应用程序。我设计了底栏,但我用的菜单图标onClick事件混为一谈。我是否需要写code代表的onClick监听器与每一个活动课,使栏可见,并在每一页的工作,或是否有任何其他的方式,我可以创建与每一个页面就在于没有写$ C共同底栏在每一个活动课的菜单$ C。

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.

我试图通过创建一个基类,在其他子类扩展到创造,
如在后的Andr​​oid创造底栏菜单通过dave.c说,但它没有工作为了我。
请建议。谢谢你。

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.

推荐答案

简单的例子(如dave.c建议):

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);
    }
}

您MyFirstActivity看起来像:

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);
    }
}

您MySecondActivity活动:

Your MySecondActivity activity:

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

在my_first_activity.xml布局,包括:

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>

在my_second_activity.xml:

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>

在bottom_bar定义按钮的onClick与处理程序:

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>

您可能会遇到采用这种设计的问题。例如,当你想在你的一些活动中使用ListView和要继承ListActivity(TabActivity是另一个例子),这将是不可能的。

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.

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

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.

还有一种方法是使用TabHost和TabActivity

Yet another way is to use TabHost and TabActivity.

这篇关于Android的底部栏菜单的onclick行动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆