添加到片段时按钮不起作用
Posted
技术标签:
【中文标题】添加到片段时按钮不起作用【英文标题】:Button doesn't work when adding to fragment 【发布时间】:2019-02-13 07:30:13 【问题描述】:我在片段中有一个按钮,当我点击它时根本没有任何反应
这是片段的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_
android:layout_
android:background="@color/color3"
tools:context="com.mk.playAndLearn.fragment.HomeFragment">
<LinearLayout
android:layout_
android:layout_
android:background="@color/white"
android:orientation="horizontal">
<Button
android:id="@+id/addPostBtn"
android:layout_
android:layout_
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_
android:gravity="right"
android:layout_
android:layout_weight="7"
android:hint="ماذا يخطر في بالك؟"
android:paddingRight="10dp"/>
</LinearLayout>
<android.support.v7.widget.RecyclerView
android:layout_
android:layout_/>
</FrameLayout>
我已经尝试了很多来解决这个问题,但我不能,但我觉得解决这个问题很容易所以我希望任何人帮助我解决这个问题我尝试更改按钮类型并改用 ImageButton但这并不能解决问题
【问题讨论】:
按下按钮时是否动画? 【参考方案1】:我认为您的 xml 布局有问题。你的 recyclerview 覆盖在线性布局上。改变布局如下
<?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_
android:layout_
android:background="@color/color3"
tools:context="com.mk.playAndLearn.fragment.HomeFragment">
<LinearLayout
android:orientation="vertical"
android:layout_
android:layout_>
<LinearLayout
android:layout_
android:layout_
android:background="@color/white"
android:orientation="horizontal">
<Button
android:id="@+id/addPostBtn"
android:layout_
android:layout_
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_
android:gravity="right"
android:layout_
android:layout_weight="7"
android:hint="ماذا يخطر في بالك؟"
android:paddingRight="10dp"/>
</LinearLayout>
<android.support.v7.widget.RecyclerView
android:layout_
android:layout_/>
</LinearLayout>
</FrameLayout>
【讨论】:
我已经删除了框架布局并使用了线性布局,但这并没有解决问题以上是关于添加到片段时按钮不起作用的主要内容,如果未能解决你的问题,请参考以下文章
添加到 viewDidLayoutSubviews 时,按钮渐变不起作用
我想从片段中隐藏片段容器视图(在 MainActivity 布局内),但是当我单击任务按钮然后重新打开应用程序时它不起作用