android中在fragment A里面点击button跳转到fragment B怎么实现?
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了android中在fragment A里面点击button跳转到fragment B怎么实现?相关的知识,希望对你有一定的参考价值。
android中在fragment A里面点击button跳转到fragment B怎么实现?
方法一:直接getActivity,使用activity的fragmenttransation的replace方法替换
假设
class OneFragment extends Fragmentprivate Button btn;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
View view = inflater.inflate(R.layout.frag_one, null);
btn = (Button)view.findViewById(R.id.button);
btn.setOnClickListener(new OnClickListener()
@Override
public void onClick(View v)
FragmentManager fm = getActivity().getFragmentManager();
fm.beginTransaction()
//替换为TwoFragment
.replace(R.layout.fragment_container,new TwoFragment())
.commit();
);
return super.onCreateView(inflater, container, savedInstanceState);
但是这样做会使OneFragment和原activity的耦合度提高,fragment设计出来就是有重用的目的,所以不好,使用方法二
方法2:定义回调函数,当button按下调用回调
class OneFragment extends Fragmentprivate Button btn;
private OnButtonClick onButtonClick;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
View view = inflater.inflate(R.layout.frag_one, null);
btn = (Button)view.findViewById(R.id.button);
btn.setOnClickListener(new OnClickListener()
@Override
public void onClick(View v)
if(onButtonClick!=null)
onButtonClick.onClick(btn);
);
return super.onCreateView(inflater, container, savedInstanceState);
public OnButtonClick getOnButtonClick()
return onButtonClick;
public void setOnButtonClick(OnButtonClick onButtonClick)
this.onButtonClick = onButtonClick;
public interface OnButtonClick
public void onClick(View view);
这样只要在acitivty中设置回调即可
public class MainActivity extends Activityprivate OneFragment oneFragment = new OneFragment();
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//。。。
//。。。省略代码
oneFragment.setOnButtonClick(new OnButtonClick()
@Override
public void onClick(View view)
//切换到TwoFragment
fm.beginTransaction()
//替换为TwoFragment
.replace(R.layout.fragment_container,new TwoFragment())
.commit();
);
参考技术A 通过onAttach(Activity activity) 这个参数,强转成当前activity,然后调用对应的方法,replace或是add fragmentB, 也可以让当前activity,实现个切换的接口,强转成接口调用切换函数。本回答被提问者采纳
Android中在fragment中实现点击按钮事件
在fragment不能直接进行点击事件,需要放到oncreatActivity中
具体方法如下:
需要注意的是import android.support.v4.app.Fragment;导入的这个包
public class SendFragment extends Fragment public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) return inflater.inflate(R.layout.send_fragment,container,false); @Override public void onActivityCreated(Bundle savedInstanceState) super.onActivityCreated(savedInstanceState); // final User user ; final EditText et_title = (EditText) getActivity().findViewById(R.id.et_title); final EditText et_salary = (EditText) getActivity().findViewById(R.id.et_salary); final EditText et_sex = (EditText) getActivity().findViewById(R.id.et_sex); final EditText et_count = (EditText) getActivity().findViewById(R.id.et_count); final EditText et_phone = (EditText) getActivity().findViewById(R.id.et_phone); final EditText et_time = (EditText) getActivity().findViewById(R.id.et_time); final EditText et_address = (EditText) getActivity().findViewById(R.id.et_address); final EditText et_description = (EditText) getActivity().findViewById(R.id.et_description); Button btn_send = (Button) getActivity().findViewById(R.id.btn_send); btn_send.setOnClickListener(new View.OnClickListener() @Override public void onClick(View v) //String username=user.getUsername().toString(); String title = et_title.getText().toString(); String salary = et_salary.getText().toString(); String sex = et_sex.getText().toString(); String count = et_count.getText().toString(); String phone = et_phone.getText().toString(); String time = et_time.getText().toString(); String address = et_address.getText().toString(); String description = et_description.getText().toString(); JobDao jobDao = new JobDao(getActivity()); jobDao.add(new Job(title, salary, sex, count, phone, time, address, description)); Toast.makeText(getActivity(), "发布成功", Toast.LENGTH_LONG).show(); Intent intent = new Intent(getActivity(),MainActivity.class); startActivity(intent); );
---------------------
作者:shasha1021
来源:CSDN
原文:https://blog.csdn.net/shasha1021/article/details/72717229
版权声明:本文为博主原创文章,转载请附上博文链接!
以上是关于android中在fragment A里面点击button跳转到fragment B怎么实现?的主要内容,如果未能解决你的问题,请参考以下文章
求教android studio大神:用fragment写一个导航栏,在每个fragment里面加listview,能实现点击事件
Android 一个Activity 里面放置多个 Fragment 实现点击切换的Tab 页面效果
在BottomNavigation中在它们之间快速切换时片段崩溃
android fragment 怎样调用activity中的方法