简体   繁体   English

在片段而不是主要部分中执行按钮操作

[英]Have button action in fragment instead of main

I have an Android app where i'm using tabs (with ActionBarSherlock). 我有一个Android应用程序,我正在使用选项卡(使用ActionBarSherlock)。 So my main activity creates the tabs for me and from there i load in the fragment layouts. 所以我的主要活动为我创建了标签,并从那里加载了片段布局。

In my MainActivity.java i create a tab (this is just a snippet): 在我的MainActivity.java我创建了一个标签(这只是一个片段):

mTabsAdapter = new TabsAdapter(this, mViewPager);

mTabsAdapter.addTab(
    bar.newTab().setText("Fragment 1"),
    MainMenuFragment.class, null);

My MainMenu.java looks like this: 我的MainMenu.java看起来像这样:

public class MainMenuFragment extends SherlockFragment
{
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState)
    {
        View view = inflater.inflate(R.layout.mainmenu_fragment, container, false);

        return view;
    }

    public void showMainMenu(View view)
    {
        Log.e("app", "olol: button!"); // never called!!
    }
}

And this is mainmenu_fragment 这是mainmenu_fragment

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#000000" >

    <Button
        android:id="@+id/btnMenu"
        android:layout_width="170dp"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="41dp"
        android:text="@string/mainmenu"
        android:onClick="showMainMenu" />
</RelativeLayout>

Now all i have to do is place the method showMainMenu(View view) somewhere. 现在我所要做的就是将方法showMainMenu(View view)放在某个地方。 I thought this would go in the corresponding java file (MainMenuFragment.java in my case). 我以为这会进入相应的java文件(在我的例子中是MainMenuFragment.java)。 But it only works when i put the method in the MainAvtivity.java file. 但它只有在我将该方法放在MainAvtivity.java文件中时才有效。

That means that all my button actions from all kinds of fragment layouts will go in one (the main) java file. 这意味着来自各种片段布局的所有按钮操作都将放在一个(主)java文件中。 Why can't i simply place it inside the java file that calls the Fragment layout..?? 为什么我不能简单地将它放在调用Fragment布局的java文件中。

Now all I have to do is place the method showMainMenu() somewhere - this is wrong. 现在我所要做的就是将方法showMainMenu()放在某处 - 这是错误的。 Please, refer to the documentation of android:onClick : 请参考android:onClick的文档:

For instance, if you specify android:onClick="sayHello", you must declare a public void sayHello(View v) method of your context (typically, your Activity). 例如,如果指定android:onClick =“sayHello”,则必须声明上下文的公共void sayHello(View v) 方法 (通常是您的Activity)。

Seems You cannot place your callback somewhere , because framework won't be able to find that callback. 似乎你无法将回调放在某处 ,因为框架将无法找到该回调。 If You're defining it inside Activity (which is actually, a Context), it's possible for View to find it back. 如果您在Activity(实际上是Context)中定义它,View可以找回它。 Actually, View.java contains the following code: 实际上,View.java包含以下代码:

case R.styleable.View_onClick:
    if (context.isRestricted()) {
        throw new IllegalStateException("The android:onClick attribute cannot " + "be used within a restricted context");
    }

    final String handlerName = a.getString(attr);
    mHandler = getContext().getClass().getMethod(handlerName, View.class);
    ...
    mHandler.invoke(getContext(), View.this);

Seems it's the only possible way to call callback defined in layout file with current android:onClick attribute specification. 似乎这是使用当前的android:onClick属性规范调用布局文件中定义的回调的唯一可行方法。

The short answers is (like already pointed out), you can't. 简短的答案(就像已经指出的那样),你不能。

The only way to do this is by creating an onClick even listener. 唯一的方法是创建一个onClick甚至监听器。 In the MainMenuFragment.java in the onCreate method, do something like this: 在onCreate方法的MainMenuFragment.java中,执行以下操作:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState)
{
    View view = inflater.inflate(R.layout.scan_fragment, container, false);

    Button menuButton = (Button)view.findViewById(R.id.btnMenu);
    menuButton.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            Log.e("app", "onclick listener");
        }
    });

    return view;
}

You can remove the onClick attribute from the layout xml. 您可以从布局xml中删除onClick属性。

I solved this using the following: 我用以下方法解决了这个问题:

Fragment xml contains 片段xml包含

android:onClick="myFunction"

Activity Contains 活动包含

public void myFunction(View v)
{ getSupportFragmentManager().findViewById/Tag(...).myFunction(v); }

Fragment Code can then implement as below to have access to local data 然后,片段代码可以如下实现,以访问本地数据

public void myFunction(View v) {...}

Hope this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM