我想知道如何在fragment.java文件中进行编码。首先,我是编码的初学者。我已经在底部导航栏中创建了带有三个导航按钮的Bottomnavigationview,并且为每个导航按钮都创建了Fragment活动。底部导航栏中的导航选项之一是“更多”,我添加了一个ID为"btn_Logoff"
的按钮。当我单击按钮时,我希望它进入activity_login.xml
或LoginActivity.java
。我不知道它的代码是什么,以及在默认片段Java文件中的什么位置添加代码,因此,这是默认情况下写在片段文件上的内容:
文件名为MoreFragment.java
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.Toast;
/**
* A simple {@link Fragment} subclass.
* Activities that contain this fragment must implement the
* {@link MoreFragment.OnFragmentInteractionListener} interface
* to handle interaction events.
* Use the {@link MoreFragment#newInstance} factory method to
* create an instance of this fragment.
*/
public class MoreFragment 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;
public MoreFragment() {
// 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 MoreFragment.
*/
// TODO: Rename and change types and number of parameters
public static MoreFragment newInstance(String param1, String param2) {
MoreFragment fragment = new MoreFragment();
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);
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_more, container, false);
}
// 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 {
Toast.makeText(context, "Notification Fragment Attached", Toast.LENGTH_SHORT).show();
}
}
@Override
public void onDetach() {
super.onDetach();
mListener = null;
}
/**
* This interface must be implemented by activities that contain this
* fragment to allow an interaction in this fragment to be communicated
* to the activity and potentially other fragments contained in that
* activity.
* <p>
* See the Android Training lesson <a href=
* "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);
}
}
我经历了很多试验和错误,但仍然没有解决方法。任何帮助表示赞赏。谢谢。
答案 0 :(得分:1)
您能解释一下这部分吗?
当我单击按钮时,我希望它进入activity_login.xml或LoginActivity.java。
您想在片段中单击按钮后在活动中执行某些操作吗?
您可以通过某些方式进行操作。最简单的方法是将监听器设置为单击按钮时的监听器。
创建界面:
public interface OnNavigationButtonClickListener {
void onMoreClick();
}
在片段中创建侦听器变量:
private OnNavigationButtonClickListener navigationListener;
并为其设置:
public void setNavigationListener(OnNavigationButtonClickListener listener) {
this.navigationListener = listener;
}
3。单击更多按钮时,通知您的听众片段:
onMoreButton.setOnClickListener(new OnClickListener {
navigationListener.onMoreClick();
});
4。然后在您的活动中设置监听器:
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
fragment.setNavigationListener(new OnNavigationButtonClickListener() {
@Override
public void onMoreClick() {
// you clicked 'more' button in fragment
// do what you want
}
});
答案 1 :(得分:0)
假设您已经使用findViewById和所有内容标识了btn_Logoff 您需要做的就是添加一个onclickListener来启动您想要的活动,您的clicklistener代码将类似于此
Button btn_Logoff;
your class{
btn_Logoff = (Button) findViewById(R.id.IdOfYourBtnInTheXmlFile);
btn_Logoff.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(getApplicationContext(), LoginActivity.class);
startActivity(intent);
}
});
}