简体   繁体   English

按钮onclick中的呼叫片段问题

[英]call fragment issue in button onclick

I create a fragment extend class to call layout as following 我创建一个片段扩展类以调用布局,如下所示

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

   <TextView
        android:id="@+id/date_time_label"
        android:layout_centerVertical="true"
        android:layout_marginLeft="200dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" 
        android:textSize="32dp"
        android:textStyle="bold"

        android:text="time_label"
        />

    <TextView
        android:id="@+id/last_view_time"
        android:layout_toRightOf="@+id/date_time_label"
        android:layout_alignBaseline="@+id/date_time_label"
        android:layout_marginLeft="8dip"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textSize="32dp"

        />

</LinearLayout>



package com.appnetics;

import java.text.SimpleDateFormat;
import java.util.Date;



import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;




/**
 * DateTime
 */
public class details extends Fragment {
    private static String getDateTimeString(Date time) {
        return new SimpleDateFormat("d MMM yyyy HH:mm:ss")
            .format(time);
    }

    private final String time = getDateTimeString(new Date());

    /** @see android.app.Fragment#onCreateView(android.view.LayoutInflater, android.view.ViewGroup, android.os.Bundle) */
    @Override
    public View onCreateView(
        LayoutInflater inflater,
        ViewGroup container,
        Bundle b)
    {
        View view = inflater.inflate(
            R.layout.details,
            container,
            false);  //!!! this is important

        ((TextView) view.findViewById(R.id.last_view_time))
            .setText(time);

        return view;
    }
}

now to use it I create layout like this 现在使用它,我创建这样的布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="horizontal" >

    <Button
        android:id="@+id/btn_showdetails"
        android:layout_width="100dip"
        android:layout_height="wrap_content"
        android:layout_marginLeft="110dip"
        android:layout_marginTop="15dip"
        android:text="Show Details" />

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="horizontal" 
     android:id="@+id/my_parent_layout"
    >
    </LinearLayout>



</LinearLayout>

on the click event of the button I say 关于按钮的点击事件,我说

setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {

                FragmentManager fragmentManager = getFragmentManager();
                        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
                        details fragment = new details();
                        fragmentTransaction.add(R.id.my_parent_layout, fragment);
                        fragmentTransaction.commit();


            }
        }); 

but it raise error The method getFragmentManager() is undefined for the type new View.OnClickListener(){} 但是会引发错误对于新View.OnClickListener(){}类型的方法getFragmentManager()未定义

any idea how to solve that 任何想法如何解决

if call activity from fragment then code below or call fragment to fragment then simply replace the code instead of startactivity onclick method 如果从片段调用活动,然后在下面的代码中调用,或者在片段之间调用片段,则只需替换代码而不是startactivity onclick方法

public class FragmentA extends Fragment implements OnClickListener{

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

        View v = inflater.inflate(R.layout.fragment_start, container, false);

        Button button1 = (Button) v.findViewById(R.id.StartButton);
        button1.setOnClickListener(this);
        return v;
    }

    @Override
    public void onClick(View v) {
        startActivity(new Intent(getActivity(),testing.class));
        }
    }
}

Declare and set the fragmentManager as final, before the listener definition. 在侦听器定义之前,声明并将fragmentManager设置为final。

final FragmentManager fragmentManager = getFragmentManager();

<your listener declaration>

you should be able to use it in the listener. 您应该能够在侦听器中使用它。

Below code works 下面的代码有效

    Button publishButton =(Button)                                                                                                  
                          fragmentView.findViewById(R.id.publishButton);
    publishButton.setOnClickListener(new View.OnClickListener() {
           @Override
           public void onClick(View view) {
              publish();
           }
      });

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

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