如何使用与活动相同的Fragment?

时间:2017-02-26 17:29:33

标签: android android-studio

我已按照本教程在活动http://www.androidhive.info/2015/09/android-material-design-working-with-tabs/

中创建了一个很好的标签

但它使用Fragment并且在片段中不可能使用任何正常的代码:

Toast.makeText(this, "Error...", Toast.LENGTH_LONG).show();

或单击一个简单按钮

btnSum =(Button)findViewById(R.id.button4);
        btnSum.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mAuthTask = new TaskAsincrono();
                mAuthTask.execute((Void) null);
            }
        });

Fragment上没什么可行的,可能吗?

我需要标签,因为活动有多个功能,结果很好但是在级别代码没有,1片段有登录按钮,文字但是如果我打电话按钮点击不工作ex:

Button btnSum =(Button)findViewById(R.id.button4);
        btnSum.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });

enter image description here

enter image description here

片段Tab2:

<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:id="@+id/tab2"
    tools:context="net.appsite.Tab2">

    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:inputType="textPersonName"
        android:text="Name"
        android:ems="10"
        android:layout_x="49dp"
        android:layout_y="226dp"
        android:id="@+id/editText8"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="59dp" />

    <Button
        android:text="Button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/button3"
        android:layout_x="114dp"
        android:layout_y="280dp"
        android:elevation="0dp"
        android:layout_marginTop="42dp"
        android:layout_below="@+id/editText8"
        android:layout_centerHorizontal="true" />

</RelativeLayout>

Tab2.jar:

public class Tab2 extends Fragment{

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


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

        //ANY NORMAL CODE HERE NOT WORK, BUT IN NORMAL ACTIVITY WORK!
        Toast.makeText(this, "Error...", Toast.LENGTH_LONG).show();
    }

    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_tab2, container, false);
    }
}

这是我第一次使用android,请帮助我。

4 个答案:

答案 0 :(得分:2)

使用活动上下文显示Toast

要将点击监听器添加到Button,请使用片段view.findviewByID()

你的Fragment类看起来应该是这样的:

    public class Tab2 extends Fragment{

   View view;

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


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

        //ANY NORMAL CODE HERE NOT WORK, BUT IN NORMAL ACTIVITY WORK!
        Toast.makeText(getActivity(), "Error...", Toast.LENGTH_LONG).show();  //use activity context
    }

    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
       view =inflater.inflate(R.layout.fragment_tab2, container, false);
       Button btnSum =(Button)view.findViewById(R.id.button3);  // get Button with the view object
        btnSum.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });

       return view;
    }
}

答案 1 :(得分:2)

在Fragment中“这个”不起作用,你必须获得像“getActivity()”这样的父活动的上下文,或者将上下文从活动传递到片段。

您的代码就像这样

Toast.makeText(getActivity(), "Error...", Toast.LENGTH_LONG).show();

Tab2活动如下所示:

public class Tab2 extends Fragment{
private View mRootView;

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




public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
  mRootView= inflater.inflate(R.layout.fragment_tab2, container,false);

   Button btnSum =(Button)mRootView.findViewById(R.id.button4);
    btnSum.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
             //Your action goes here
            }
          });  

        }
  }

答案 2 :(得分:1)

getActivity().findViewById(..)

时,您应该Toast.makeText(getActivity(), "Error...", Toast.LENGTH_LONG).show();Fragment

这是因为在findViewById上定义了Context,而Toast要求第一个参数为Context。由于Activity间接延伸Context,因此可行。但是在Fragment中你需要得到上下文。一种方法是在getActivity()中执行Fragment

答案 3 :(得分:1)

FragmentActivity不完全相同,请参阅docs了解详情

另外Fragment不是Context,请参阅更多信息here因此,在传递this时您无法使用Context,而是使用getActivity()

 getApplicationContext()

onViewCreated

还需要在you_fragment public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { view =inflater.inflate(R.layout.fragment_tab2, container, false); Button btnSum =(Button)view.findViewById(R.id.button4); // note that button4 should be existing in fragment_tab2 btnSum.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { } }); // other views binding code // and other code return view; } 方法中完成绑定视图:

hotelregionid
相关问题