java - 添加到 fragment 时按钮不起作用

标签 java android xml fragment

我在 fragment 中有一个按钮,当我点击它时什么也没有发生

这是该 fragment 的java代码:

public class HomeFragment extends Fragment {
    // TODO: Rename parameter arguments, choose names that match
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    // TODO: Rename and change types of parameters
    private String mParam1;
    private String mParam2;

    private OnFragmentInteractionListener mListener;

    EditText etAddPost;
    Button addPostImage;

    FirebaseDatabase database = FirebaseDatabase.getInstance();
    DatabaseReference myRef;

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

    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @param param1 Parameter 1.
     * @param param2 Parameter 2.
     * @return A new instance of fragment HomeFragment.
     */
    // TODO: Rename and change types and number of parameters
    public static HomeFragment newInstance(String param1, String param2) {
        HomeFragment fragment = new HomeFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }

        database = FirebaseDatabase.getInstance();
        myRef = database.getReference("posts");;

    }
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_home, container, false);
        etAddPost =rootView.findViewById(R.id.etAddPost);
        addPostImage = rootView.findViewById(R.id.addPostBtn);
        addPostImage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(getActivity(), "Clicked", Toast.LENGTH_SHORT).show();
                String postText = etAddPost.getText().toString();
                if(TextUtils.isEmpty(postText)){
                    etAddPost.setError("لا يمكنك ترك هذا الحقل فارغا");
                }
                else {
                    Map<String, String> map = new HashMap<>();
                    map.put("postContent", postText);
                    myRef.push().setValue(map);
                    etAddPost.setText("");
                }
            }
        });        return rootView;
    }

    // TODO: Rename method, update argument and hook method into UI event
    public void onButtonPressed(Uri uri) {
        if (mListener != null) {
            mListener.onFragmentInteraction(uri);
        }
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        if (context instanceof OnFragmentInteractionListener) {
            mListener = (OnFragmentInteractionListener) context;
        } else {
            throw new RuntimeException(context.toString()
                    + " must implement OnFragmentInteractionListener");
        }
    }

    @Override
    public void onDetach() {
        super.onDetach();
        mListener = null;
    }
"http://developer.android.com/training/basics/fragments/communicating.html"
     * >Communicating with Other Fragments</a> for more information.
     */
    public interface OnFragmentInteractionListener {
        // TODO: Update argument type and name
        void onFragmentInteraction(Uri uri);
    }
}

这是我的 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="@color/color3"
    tools:context="com.mk.playAndLearn.fragment.HomeFragment">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/white"
        android:orientation="horizontal">

        <Button
            android:id="@+id/addPostBtn"
            android:layout_width="0dp"
            android:layout_height="30dp"
            android:layout_weight="1"
            android:layout_margin="5dp"
            android:background="@drawable/send"
            android:layout_marginRight="5dp"/>

        <EditText
            android:layout_margin="5dp"
            android:id="@+id/etAddPost"
            android:layout_width="0dp"
            android:gravity="right"
            android:layout_height="wrap_content"
            android:layout_weight="7"
            android:hint="ماذا يخطر في بالك؟"
            android:paddingRight="10dp"/>


    </LinearLayout>
    <android.support.v7.widget.RecyclerView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>

</FrameLayout>

我已经尝试了很多方法来解决这个问题,但我做不到,但我觉得解决问题很容易,所以我希望任何人能帮助我解决这个问题我尝试更改按钮类型并使用 ImageButton 来代替,但这并不能解决问题

最佳答案

我认为您的 xml 布局有问题。您的回收 View 位于线性布局之上。更改布局如下

<?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="@color/color3"
    tools:context="com.mk.playAndLearn.fragment.HomeFragment">

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@color/white"
            android:orientation="horizontal">

            <Button
                android:id="@+id/addPostBtn"
                android:layout_width="0dp"
                android:layout_height="30dp"
                android:layout_weight="1"
                android:layout_margin="5dp"
                android:background="@drawable/send"
                android:layout_marginRight="5dp"/>

            <EditText
                android:layout_margin="5dp"
                android:id="@+id/etAddPost"
                android:layout_width="0dp"
                android:gravity="right"
                android:layout_height="wrap_content"
                android:layout_weight="7"
                android:hint="ماذا يخطر في بالك؟"
                android:paddingRight="10dp"/>


        </LinearLayout>
        <android.support.v7.widget.RecyclerView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>
    </LinearLayout>
</FrameLayout>

关于java - 添加到 fragment 时按钮不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52231099/

相关文章:

Android:CardView 即将推出操作栏

Java线程没有清理

java - 我如何在我的 JTextArea 中设置行号?

java - 代码被重定向到移动网站

java - RecyclerView 花费太长时间从用户存储中填充大量视频

xml - 使用 R 读取 XML 文件,选择几个节点并将其写回另一个 XML

java - Spring上下文错误: SAXParseException cvc-complex-type. 3.2.2忽略无法解析的上下文:属性占位符

javascript - Nashorn 绑定(bind)问题

javascript - 将 google map API 标记限制为每个标记只能点击一次

android - Kotlin 扩展函数来拆分大类