在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 版权声明:本文为博主原创文章,转载请附上博文链接!
原文地址:https://www.cnblogs.com/derekhan/p/11096235.html
时间: 2024-11-02 17:37:35