【问题标题】:controlling a button in a fragment [duplicate]控制片段中的按钮[重复]
【发布时间】:2018-07-19 14:43:09
【问题描述】:

我想在片段活动中控制片段 xml 文件中的按钮。在其他活动中,我们可以通过 findviewbyid 方法轻松控制我们的按钮,然后我们可以应用 setonclicklistener。但在片段中,我如何访问按钮并应用 onclicklistener 方法。 我的 Fragment.java

    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.Button;
    import android.widget.LinearLayout;


    /**
     * A simple {@link Fragment} subclass.
     */
    public class QuoteTypeFragment extends Fragment {


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

        LinearLayout  typeOne, typeTwo, typeThree, typeFour;

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
            // Inflate the layout for this fragment
            View vv = inflater.inflate(R.layout.fragment_quote_type, container, false);
            return vv;
        }

    }

我的 Fragment.xml

 <?xml version="1.0" encoding="utf-8"?>
    <FrameLayout 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="#3f525e"
        tools:context=".QuoteTypeFragment">



    <Button
        android:id="@+id/submitbutton"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Submit"/>

    </FrameLayout>

所以这里我要控制fragment.java中的提交按钮。如何通过 findviewbyid 或 findfragmentbyid 访问按钮。在 fragment.java 中,我在哪里使用该代码来访问提交按钮。

【问题讨论】:

  • 对不起,我先搜索但没有找到准确的结果,所以我问了。

标签: java android android-fragments


【解决方案1】:

在 OnCreateView 中

    Button button;

        @Override
            public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                     Bundle savedInstanceState) {
                // Inflate the layout for this fragment
                View vv = inflater.inflate(R.layout.fragment_quote_type, container, false);

            button = vv.findViewById(R.id.submitbutton);

            button.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {

                }
            });

             return vv;
            }

【讨论】:

  • 如果我想让我的按钮全局化我将在哪里初始化按钮
【解决方案2】:

在你的 QuoteTypeFragment.java

1) 使用视图投射小部件。 (view.findViewById())。

2) 设置 onClickistener。

3) 实现 View.OnClickListener 和 onClick 方法。

public class QuoteTypeFragment extends Fragment implements View.OnClickListener {

@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View vv = inflater.inflate(R.layout.fragment_quote_type, container, false);

        Button mSubmitButton = vv.findViewById(R.id.submitbutton);
        mSubmitButton.setonClickListener(this);

        return vv;
    }
}

@Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.submitbutton:
                // Control the button
                break;
        }
}

【讨论】:

    【解决方案3】:

    在片段活动中从片段XML文件控制按钮非常容易。

    在您的 onCreateView 中使用这些行,

    Button button; 
    
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
    
        // Inflate the layout for this fragment
        View vv = inflater.inflate(R.layout.fragment_quote_type, container, false);
        button = vv.view.findViewById(R.id.submitbutton);
        return vv;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-11-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-03-04
      • 2019-09-15
      • 2017-10-21
      相关资源
      最近更新 更多