主要是掌握怎样在Fragment类型的.java文件中实现对于文本框或者按钮点击事件的触发操作。
相应的出发时间都是之前的代码。主要是怎样在Fragment怎样实现相应的操作主要是对于getActivity()。的相关应用。
对于相关的知识点:
1、
onCreatView是碎片的生命周期中的一种状态,在为碎片创建视图(加载布局)时调用
使用:View view=inflater.inflate(R.layout.right_fragment,container,false);
三个参数的含义及作用
LayoutInflater inflater:作用类似于findViewById(),findViewById()用来寻找xml布局下的具体的控件(Button、TextView等),LayoutInflater inflater()用来找res/layout/下的xml布局文件
ViewGroup container:表示容器,View放在里面(还不理解)
Bundle savedInstanceState:保存当前的状态,在活动的生命周期中,只要离开了可见阶段,活动很可能就会被进程终止,这种机制能保存当时的状态
2、
关于android中onCreateView和onCreate之间的区别
onCreate是指创建该fragment,类似于Activity.onCreate,你可以在其中初始化除了view之外的东西。
onCreateView是创建该fragment对应的视图,你必须在这里创建自己的视图并返回给调用者、
3、
getActivity();来代替context获取他的相应的对象这样在Fragment中好运行点击事件。
主要的两个.java文件的代码如下:
WeixinFragment.java
package com.example.wenandroidcheck11; import android.app.AlertDialog; import android.content.DialogInterface; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.DatePicker; import android.widget.LinearLayout; import android.widget.TextView; import android.widget.TimePicker; import androidx.annotation.Nullable; import androidx.fragment.app.Fragment; import java.util.Calendar; public class WeixinFragment extends Fragment implements View.OnClickListener, DatePicker.OnDateChangedListener, TimePicker.OnTimeChangedListener { private LinearLayout llDate, llTime; private TextView tvDate, tvTime; private int year, month, day, hour, minute; //在TextView上显示的字符 private StringBuffer date, time; @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { View view = inflater.inflate(R.layout.tab1, container, false); llDate = (LinearLayout) view.findViewById(R.id.ll_date); tvDate = (TextView) view.findViewById(R.id.tv_date); llTime = (LinearLayout) view.findViewById(R.id.ll_time); tvTime = (TextView)view.findViewById(R.id.tv_time); llDate.setOnClickListener(this); llTime.setOnClickListener(this); date = new StringBuffer(); time = new StringBuffer(); initDateTime(); return view; } /** * 获取当前的日期和时间 */ private void initDateTime() { Calendar calendar = Calendar.getInstance(); year = calendar.get(Calendar.YEAR); month = calendar.get(Calendar.MONTH)+1; day = calendar.get(Calendar.DAY_OF_MONTH); hour = calendar.get(Calendar.HOUR); minute = calendar.get(Calendar.MINUTE); } @Override public void onClick (View v){ switch (v.getId()) { case R.id.ll_date: AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); builder.setPositiveButton("设置", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { if (date.length() > 0) { //清除上次记录的日期 date.delete(0, date.length()); } month += 1; tvDate.setText(date.append(String.valueOf(year)).append("年").append(String.valueOf(month)).append("月").append(day).append("日")); dialog.dismiss();//取消对话框的作用 } }); builder.setNegativeButton("取消", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); } }); final AlertDialog dialog = builder.create();//创建对话框 View dialogView = View.inflate(getActivity(), R.layout.dialog_date, null); /*这个方法可以得到view,但是对view中设置居中等什么属性都是无效的,还有就是设置的宽度也是无效的,默认只能是wrap*/ final DatePicker datePicker = (DatePicker) dialogView.findViewById(R.id.datePicker); dialog.setTitle("设置日期"); dialog.setView(dialogView);//可以通过自定义一个View,设计成自己想要的不同的dialog,(会话) dialog.show();//显示对话框 //初始化日期监听事件 datePicker.init(year, month - 1, day, this); break; case R.id.ll_time: AlertDialog.Builder builder2 = new AlertDialog.Builder(getActivity()); builder2.setPositiveButton("设置", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { if (time.length() > 0) { //清除上次记录的日期 time.delete(0, time.length()); } tvTime.setText(time.append(String.valueOf(hour)).append("时").append(String.valueOf(minute)).append("分")); dialog.dismiss(); } }); builder2.setNegativeButton("取消", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); } }); AlertDialog dialog2 = builder2.create(); View dialogView2 = View.inflate(getActivity(), R.layout.dialog_time, null); TimePicker timePicker = (TimePicker) dialogView2.findViewById(R.id.timePicker); timePicker.setCurrentHour(hour); timePicker.setCurrentMinute(minute); timePicker.setIs24HourView(true); //设置24小时制 timePicker.setOnTimeChangedListener(this); dialog2.setTitle("设置时间"); dialog2.setView(dialogView2); dialog2.show(); break; } /** * 日期改变的监听事件 * * @param view * @param year * @param monthOfYear * @param dayOfMonth */ } @Override public void onDateChanged (DatePicker view,int year, int monthOfYear, int dayOfMonth){ this.year = year; this.month = monthOfYear; this.day = dayOfMonth; } /** * 时间改变的监听事件 * * @param view * @param hourOfDay * @param minute */ @Override public void onTimeChanged (TimePicker view,int hourOfDay, int minute){ this.hour = hourOfDay; this.minute = minute; } }
Frdfragment.java
package com.example.wenandroidcheck11; import android.content.Intent; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.Button; import androidx.annotation.Nullable; import androidx.fragment.app.Fragment; public class FrdFragment extends Fragment implements View.OnClickListener{ private Button btn_register; @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { View view = inflater.inflate(R.layout.tab2, container, false); btn_register =(Button) view.findViewById(R.id.button_register); btn_register.setOnClickListener(this); return view; } @Override public void onClick(View view){ //String text = "csdnicjbsndicnsdicnsojdn"; //Toast.makeText(MainActivity.this, text, Toast.LENGTH_SHORT).show(); Intent intent = new Intent(getActivity(),RegisterActivity1.class); startActivity(intent); } }
这样实现在tap中界面中按钮的触发时间。并且实现界面的跳转。
点击注册后的页面跳转
点击返回登录,会跳转到初始的界面
原文地址:https://www.cnblogs.com/dazhi151/p/12257495.html