当声明按钮不同片段不承认 [英] When declaring buttons different fragments does not recognize

查看:146
本文介绍了当声明按钮不同片段不承认的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

声明按钮时,我有一个问题。

I have a problem when declaring a button.

我会尽量解释尽可能具体。

I will try to explain as specific as possible.

在我的主要布局我有一个包含一个二级布局片段。在这我有几个按钮。

In my main Layout I have a Fragment containing a secondary Layout. In which I have several buttons.

我的本意是,我的主要fichero.java到片段中声明的按钮。

My intention is that my main fichero.java to declare the buttons within the fragment.

下面我们把主要的布局:

Here we put the main Layout:

<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:baselineAligned="false">

<fragment
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:name="josejuansosarodriguez.radioecca.conocecanarias.TrueoFalseFragment"
    android:id="@+id/fragmentTrueFalse"
    android:layout_alignParentTop="true"
    android:layout_alignParentLeft="true" />


<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/Grp1"
    android:id="@+id/textGrp1"
    android:textSize="25sp"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="5dp"/>


<fragment
    android:layout_width="fill_parent"
    android:layout_height="450dp"
    android:name="josejuansosarodriguez.radioecca.conocecanarias.Grp1FragmentP1"
    android:id="@+id/fragmetaskGRP1"
    android:layout_below="@+id/textGrp1"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="5dp" />

</RelativeLayout>

在这里,我们把具有按钮二次布局:

Here we put the secondary Layout that has the buttons:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent" android:layout_height="match_parent"
    android:id="@+id/fragment_TrueorFalse">


    <Button
        android:layout_width="120sp"
        android:layout_height="wrap_content"
        android:textAllCaps="false"
        android:text="@string/buttontrue"
        android:id="@+id/buttontrue"
        android:layout_marginLeft="40dp"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true" />

    <Button
        android:layout_width="120sp"
        android:layout_height="wrap_content"
        android:textAllCaps="false"
        android:text="@string/buttonfalse"
        android:id="@+id/buttonfalse"
        android:layout_marginRight="40dp"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true" />

</RelativeLayout>

在这里,我离开我的主要活动:

And here I leave the my main activity:

public class Grp1Fragment extends Fragment {

private Button buttonTrue;
private Button buttonFalse;
private Button buttonNextAsk;

private View view;


public Grp1Fragment() {
    // Required empty public constructor


}


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


   buttonTrue = (Button) view.findViewById(R.id.buttontrue);
    buttonTrue.setOnClickListener(this);

// Inflate the layout for this fragment
    view = inflater.inflate(R.layout.fragment_grp1, container, false);
    return view;
}

我的问题我在下面一行找到:
buttonTrue.setOnClickListener(本);
贺电全文:查看不能在应用josejuansosarodriguez.radioecca.conocecanarias.Grp1Fragment

My problem I find in the following line: buttonTrue.setOnClickListener (this); The message reads: View can not be in Applied to josejuansosarodriguez.radioecca.conocecanarias.Grp1Fragment

我希望我有小号$ P $垫远。
非常感谢你的一切,这个论坛是惊人的。

I hope I have spread far. Thank you very much for everything, this forum is amazing.

推荐答案

试试这个:

请注意,您必须首先膨胀的布局,使入店布局的部件。

Note that you have to FIRST inflate the layout to make accesible the widgets of the layout.

然后你可以绑定的窗口小部件,并finallly在这种情况下,将听众的按钮。

Then you can "bind" the widgets, and finallly in this case, set the listeners to the buttons.

我设置你登录,如果你需要将其更改为烤面包,或者code

I set you a Log, if you need change it to Toast, or code

public class Grp1Fragment extends Fragment {

private Button buttonTrue;
private Button buttonFalse;
private Button buttonNextAsk;

private View view;


public Grp1Fragment() {
    // Required empty public constructor
}


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

     // Inflate the layout for this fragment
    view = inflater.inflate(R.layout.fragment_grp1, container, false);

    //Declare your widgets (Buttons)
    buttonTrue = (Button) view.findViewById(R.id.buttontrue);
    buttonFalse = (Button) view.findViewById(R.id.buttonfalse);


    //Set the Listeners
    buttonTrue.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                //Toast or Log, or whateber you need
                Log.d("Fragment1" ,  "Button: True");
            }
        });

    buttonFalse.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            //Toast or Log, or whateber you need
            Log.d("Fragment1" ,  "Button: FAlse");
        }
    });


    //return the view
    return view;
}

这篇关于当声明按钮不同片段不承认的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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