如何使用片段中的按钮启动活动?

时间:2015-02-22 03:54:13

标签: android user-interface android-fragments android-studio findviewbyid

我有一个片段,它有一个浮动动作按钮,我想在按下时启动一个活动。以下是我想要启动的片段的代码:

package com.nhscoding.safe2tell;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;


public class LEARN extends Fragment {


    View rootview;

    private OnFragmentInteractionListener mListener;




    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        rootview = inflater.inflate(R.layout.fragment_learn, container, false);
        return rootview;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        final Button subTip = (Button) getActivity().findViewById(R.id.subTip);
        subTip.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent (v.getContext(),SUBMIT_TIP.class);
                startActivityForResult(intent,0);
            }
        });
    }

    public void onButtonPressed(Uri uri) {
        if (mListener != null) {
            mListener.onFragmentInteraction(uri);
        }
    }

    @Override
    public void onDetach() {
        super.onDetach();
        mListener = null;
    }

    public static LEARN newInstance() {
        LEARN fragment = new LEARN();
        Bundle args = new Bundle();
        fragment.setArguments(args);

        return fragment;


    }
    public interface OnFragmentInteractionListener {
        // TODO: Update argument type and name
        public void onFragmentInteraction(Uri uri);
    }



//public class Stuff extends Activity {
//    Button submit_tip = (Button) findViewById(R.id.submit_tip);
//    submit_tip.setOnClickListener(new View.OnClickListener() {
//        public void onClick(View v) {
//            Intent intent = new Intent(v.getContext(), SUBMIT_TIP.class);
//            startActivityForResult(intent,0);
//        }
}

这会在findViewById上返回错误,因为它不能用于片段,只能用于活动。我是否需要将此更改为活动或是否有其他方法可以实现此目的?

3 个答案:

答案 0 :(得分:1)

View的{​​{1}}方法中获取对onCreate()的引用不起作用,因为Fragment的布局尚未充气

您需要做的就是替换

Fragment

用这个:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    final Button subTip = (Button) getActivity().findViewById(R.id.subTip);
    subTip.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent (v.getContext(),SUBMIT_TIP.class);
            startActivityForResult(intent,0);
        }
    });
}

试试这个。这将有效。

答案 1 :(得分:0)

使用rootview从Fragment布局访问Button:

    final Button subTip = (Button) rootview.findViewById(R.id.subTip);

注意: getActivity()返回主要的Activity上下文,其中添加了当前的Fragment,因此如果我们调用rootview.findViewById,则意味着我们正在从Activity布局而不是Fragment

答案 2 :(得分:0)

你可以用它。您可以按getActivity获取当前活动。

@Override
public void onViewCreated (View rootView, Bundle savedInstanceState){
final Button b= (Button) rootView.findViewById(R.id.b);
b.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        Intent intent = new Intent (v.getContext(),yourActivity.class);
        startActivityForResult(intent,0);
    }
}

);
}