话不多说直接上代码
fragment_lamp.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/white"
android:gravity="center"
android:orientation="horizontal"
tools:context=".fragment.LampFragment">
<LinearLayout
android:id="@+id/buju2"
android:layout_width="wrap_content"
android:layout_height="300dp"
android:gravity="center"
android:orientation="vertical">
<TextView
android:id="@+id/wenben_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:paddingLeft="20dp"
android:paddingRight="35dp"
android:text="@string/wenben_1"
android:textColor="@color/colorPrimary"
android:textSize="25sp" />
<LinearLayout
android:id="@+id/buju4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/bg_time"
android:gravity="bottom"
android:padding="10dp">
<EditText
android:id="@+id/ed_hour"
android:layout_width="70dp"
android:layout_height="50dp"
android:gravity="center"
android:inputType="number"
android:maxLength="2"
android:text="00"
android:textColor="@color/colorPrimary" />
<TextView
android:id="@+id/maohao1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:text=":"
android:textColor="@color/colorPrimary"
android:textSize="20sp"
android:textStyle="bold" />
<EditText
android:id="@+id/ed_min"
android:layout_width="70dp"
android:layout_height="50dp"
android:gravity="center"
android:inputType="number"
android:maxLength="2"
android:text="00"
android:textColor="@color/colorPrimary" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:text=":"
android:textColor="@color/colorPrimary"
android:textSize="20sp"
android:textStyle="bold" />
<EditText
android:id="@+id/ed_second"
android:layout_width="70dp"
android:layout_height="50dp"
android:gravity="center"
android:inputType="number"
android:maxLength="2"
android:text="00"
android:textColor="@color/colorPrimary" />
</LinearLayout>
</LinearLayout>
<ImageView
android:id="@+id/fengexian"
android:layout_width="1dp"
android:layout_height="300dp"
android:layout_marginLeft="20dp"
android:layout_toRightOf="@+id/buju2"
android:background="@color/colorPrimary" />
<LinearLayout
android:id="@+id/buju3"
android:layout_width="wrap_content"
android:layout_height="300dp"
android:layout_marginLeft="20sp"
android:gravity="center_horizontal"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/wenben_2"
android:textColor="@color/colorPrimary"
android:textSize="25sp" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center">
<TextView
android:id="@+id/tv_time"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:gravity="center"
android:background="@drawable/bg_time"
android:padding="10dp"
android:textColor="@color/colorPrimary"
android:textSize="30sp" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="bottom"
android:padding="10dp">
<Button
android:id="@+id/btn_start"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/shapes"
android:text="@string/start"
android:enabled="true"
android:textColor="@color/white"
android:textSize="20sp" />
<Button
android:id="@+id/btn_continues"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:layout_marginLeft="20dp"
android:background="@drawable/btn_selector"
android:text="@string/suspend"
android:textColor="@color/white"
android:enabled="false"
android:textSize="20sp" />
<Button
android:id="@+id/btn_stop"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:background="@drawable/btn_selector"
android:text="@string/end"
android:enabled="false"
android:textColor="@color/white"
android:textSize="20sp" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
LampFragment.java
public class LampFragment extends BaseFragment<FragmentLampBinding> {
private long time = 0;
public IConnectionManager manager;
CountDownTimer timer;
public static int mm = 0;
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
init();
}
private void init() {
manager = SocketManager.getInstance();
// 暂停按键监听事件
viewBinding.btnContinues.setOnClickListener(View -> {
if(timer==null) {
Toast.makeText(getActivity(), R.string.toast_mes, Toast.LENGTH_SHORT).show();
}else {
if (viewBinding.btnContinues.getText().toString().equals(getResources().getString(R.string.continues))) {
viewBinding.btnContinues.setText(R.string.suspend);
if ("00:00:00".equals(viewBinding.tvTime.getText().toString())) {
Toast.makeText(getActivity(), R.string.toast_mes, Toast.LENGTH_SHORT).show();
} else {
String[] item = viewBinding.tvTime.getText().toString().split(":");
int hour = Integer.valueOf(item[0]);
int min = Integer.valueOf(item[1]);
int send = Integer.valueOf(item[2]);
try {
time = CountDownUntil.formatTurnSecond(hour, min, send);
} catch (ParseException e) {
e.printStackTrace();
}
startTime(time);
}
} else {
timer.cancel();
viewBinding.btnContinues.setText(R.string.continues);
}
}
});
// 开始按键监听事件
viewBinding.btnStart.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (MainActivity.flag2 ==0) {
InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(viewBinding.edHour.getWindowToken(), 0);
viewBinding.btnContinues.setText(R.string.suspend);
int hour = Integer.valueOf(viewBinding.edHour.getText().toString());
int min = Integer.valueOf(viewBinding.edMin.getText().toString());
int send = Integer.valueOf(viewBinding.edSecond.getText().toString());
try {
time = CountDownUntil.formatTurnSecond(hour, min, send);
} catch (ParseException e) {
e.printStackTrace();
}
if (time == 0) {
Toast.makeText(getActivity(), R.string.toast_mes, Toast.LENGTH_SHORT).show();
} else {
startTime(time);
viewBinding.btnStart.setEnabled(false);
viewBinding.btnStart.setBackground(getResources().getDrawable(R.drawable.btn_selector));
viewBinding.btnContinues.setEnabled(true);
viewBinding.btnContinues.setBackground(getResources().getDrawable(R.drawable.shapes));
viewBinding.btnStop.setEnabled(true);
viewBinding.btnStop.setBackground(getResources().getDrawable(R.drawable.shapes));
Log.e("wer", "" + time);
}
} else {
Toast.makeText(getActivity(), R.string.lamp, Toast.LENGTH_SHORT).show();
}
}
});
// 结束按键监听事件
viewBinding.btnStop.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
LampFragment.mm=0;
viewBinding.tvTime.setText("00:00:00");
timer.cancel();
viewBinding.btnStart.setEnabled(true);
viewBinding.btnStart.setBackground(getResources().getDrawable(R.drawable.shapes));
viewBinding.btnContinues.setEnabled(false);
viewBinding.btnContinues.setBackground(getResources().getDrawable(R.drawable.btn_selector));
viewBinding.btnStop.setEnabled(true);
viewBinding.btnStop.setBackground(getResources().getDrawable(R.drawable.btn_selector));
}
});
}
private void startTime(long T) {
LampFragment.mm=1;
timer = new CountDownTimer(T, 1000) {
@Override
public void onTick(long millisUntilFinished) {
Logger.d(millisUntilFinished / 1000);
viewBinding.tvTime.setText(CountDownUntil.formatTime(millisUntilFinished));
}
@Override
public void onFinish() {
LampFragment.mm=0;
viewBinding.tvTime.setText("00:00:00");
timer.cancel();
viewBinding.btnStart.setEnabled(true);
viewBinding.btnStart.setBackground(getResources().getDrawable(R.drawable.shapes));
viewBinding.btnContinues.setEnabled(false);
viewBinding.btnContinues.setBackground(getResources().getDrawable(R.drawable.btn_selector));
viewBinding.btnStop.setEnabled(true);
viewBinding.btnStop.setBackground(getResources().getDrawable(R.drawable.btn_selector));
}
};
timer.start();
}
@Subscribe(threadMode = ThreadMode.MAIN)
public void onEventMainThread(MessageWrap event) {
}
}