-2

在这里,我添加了 Java 和 Xml 文件,我只想在用户单击按钮时在其中显示一个 Toast。

这只是为了检查 Fragment 是否正常工作。

这里 onClickListener 在 Fragment 中不适合我。当我尝试时,甚至 TextView 上的 setText 也不起作用。我认为组件有问题,或者 java 和 xml 文件的连接有问题

设置.java

    package com.example.dell.jsonexp;

    import android.content.Context;
    import android.content.Intent;
    import android.content.SharedPreferences;
    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;
    import android.widget.TextView;
    import android.widget.Toast;

    public class setting extends Fragment {
        BackgroundTask b;
    String e;
        @Override
        public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
            View RootView = inflater.inflate(R.layout.setting, container, false);

            final Button logout= (Button) RootView.findViewById(R.id.logout);
            logout.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    Toast.makeText(getActivity(),"Button Pressed",Toast.LENGTH_LONG).show();


                }
            });

            return inflater.inflate(R.layout.setting, container, false);
        }


    }

设置.xml

        <?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">

        <Button
            android:id="@+id/logout"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_centerHorizontal="true"
            android:layout_marginBottom="50dp"
            android:text="Logout" />
    </RelativeLayout>
4

3 回答 3

2

您正在将单击侦听器设置为从未显示的按钮。

您需要返回 rootView 而不是膨胀另一个视图。

return rootView;
于 2018-10-05T02:23:12.043 回答
1

Fragment您无法在方法中访问视图组件时onCreateView(),您必须使用Override onViewCreated()方法。请更新您的代码,如下所示。

    package com.example.dell.jsonexp;

    import android.content.Context;
    import android.content.Intent;
    import android.content.SharedPreferences;
    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;
    import android.widget.TextView;
    import android.widget.Toast;

    public class setting extends Fragment {
        BackgroundTask b;
        String e;

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

            return inflater.inflate(R.layout.setting, container, false);
        }

        @Override
        public void onViewCreated(View view, Bundle savedInstanceState) {
            super.onViewCreated(view, savedInstanceState);

            Button logout= (Button) view.findViewById(R.id.logout);
            logout.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    Toast.makeText(getActivity(),"Button Pressed",Toast.LENGTH_LONG).show();


                }
            });
        }

    }

谢谢!

于 2018-10-05T02:36:18.140 回答
0

你需要返回

return RootView;

对于Toast你必须写

getActivity().runOnUiThread(new Runnable() {
        @Override
        public void run() {
            Toast.makeText(getContext(), "Whatever you want here", Toast.LENGTH_SHORT).show();
        }
    });
于 2018-10-05T03:42:22.047 回答