Android中在fragment中实现点击按钮事件
Posted derekhan
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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中实现点击按钮事件的主要内容,如果未能解决你的问题,请参考以下文章
如何在 Fragment 中实现一个按钮并让它打开一个新的 Activity?
android中在fragment A里面点击button跳转到fragment B怎么实现?
Android 在fragment中实现返回键单击提醒 双击退出