当前位置: 首页 > news >正文

网站复制按钮怎么做的百度权重5的网站能卖多少钱

网站复制按钮怎么做的,百度权重5的网站能卖多少钱,动漫做h在线观看网站,大名企业做网站推广Android中简单封装Livedata工具类 前言: 之前讲解过livedata和viewmodel的简单使用,也封装过room工具类,本文是对livedata的简单封装和使用,先是封装了一个简单的工具类,然后实现了一个倒计时工具类的封装. 1.LiveD…

Android中简单封装Livedata工具类

前言:

之前讲解过livedata和viewmodel的简单使用,也封装过room工具类,本文是对livedata的简单封装和使用,先是封装了一个简单的工具类,然后实现了一个倒计时工具类的封装.

1.LiveDataHelper工具类:

package com.example.livedatautilsdemo.helper;import androidx.annotation.NonNull;
import androidx.lifecycle.LifecycleOwner;
import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.Observer;import java.util.Map;
import java.util.WeakHashMap;/*** @author: njb* @date: 2023/7/31 0:10* @desc:*/
public class LiveDataHelper<T>extends MutableLiveData {private final WeakHashMap<Observer<T>, Boolean> mObservers = new WeakHashMap<>();@Overridepublic void observe(@NonNull LifecycleOwner owner, @NonNull Observer observer) {mObservers.put((Observer<T>) observer, true);super.observe(owner, observer);}@Overridepublic void removeObserver(@NonNull Observer observer) {mObservers.remove(observer);super.removeObserver(observer);}@Overridepublic void removeObservers(@NonNull LifecycleOwner owner) {mObservers.clear();super.removeObservers(owner);}public void setValue(Object value) {for (Map.Entry<Observer<T>, Boolean> entry : mObservers.entrySet()) {if (entry.getValue()) {entry.setValue(false);entry.getKey().onChanged((T) value);}}}public void call() {setValue(null);}
}

2.简单使用:

private LiveDataHelper<String> mLiveDataHelper = new LiveDataHelper<>();@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);initView();initData();initTime();}private void initView() {tvName = findViewById(R.id.textview);tvTime = findViewById(R.id.tvTime);}private void initData() {mLiveDataHelper.observe(this, new Observer<String>() {@Overridepublic void onChanged(String name) {Log.d("LiveDataDemo", "onChanged: " + name);}});tvName.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View view) {String newName = "NewName" + new Random().nextInt(100);mLiveDataHelper.setValue(newName);Log.d("LiveDataDemo", "onClick: " + newName);tvName.setText(String.format("名称发生变化:%s", newName));}});}

3.布局代码:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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"tools:context=".MainActivity"><TextViewandroid:id="@+id/textview"android:layout_width="200dp"android:layout_height="60dp"android:text="Hello World!"android:background="@color/design_default_color_primary"android:textSize="20sp"android:textColor="@color/white"android:focusable="true"android:gravity="center"app:layout_constraintBottom_toBottomOf="parent"app:layout_constraintEnd_toEndOf="parent"app:layout_constraintStart_toStartOf="parent"app:layout_constraintTop_toTopOf="parent" /><TextViewandroid:id="@+id/tvTime"android:layout_width="200dp"android:layout_height="60dp"android:background="@color/design_default_color_primary"android:text="timer"android:textSize="20sp"android:textColor="@color/white"android:focusable="true"android:gravity="center"app:layout_constraintEnd_toEndOf="parent"app:layout_constraintStart_toStartOf="parent"app:layout_constraintTop_toBottomOf="@id/textview"android:layout_marginTop="20dp"/></androidx.constraintlayout.widget.ConstraintLayout>

4.实现效果如下:

在这里插入图片描述

5.封装一个倒计时工具类:

package com.example.livedatautilsdemo.helper;import android.os.CountDownTimer;import androidx.lifecycle.MutableLiveData;/*** @author: njb* @date: 2023/8/6 23:37* @desc:*/
public class LiveDataTimeHelper extends MutableLiveData<Long> {private CountDownTimer countDownTimer;public void startCountDown(long millisInFuture, long countDownInterval) {countDownTimer = new CountDownTimer(millisInFuture, countDownInterval) {@Overridepublic void onTick(long millisUntilFinished) {setValue(millisUntilFinished / 1000);}@Overridepublic void onFinish() {setValue(0L);}};countDownTimer.start();}public void stopCountDown() {if (countDownTimer != null) {countDownTimer.cancel();}}@Overrideprotected void onInactive() {super.onInactive();stopCountDown();}
}

6.倒计时Viewmodel:

package com.example.livedatautilsdemo.viewmodel;import android.os.Handler;
import android.os.Looper;
import android.util.Log;import androidx.lifecycle.LiveData;
import androidx.lifecycle.ViewModel;import com.example.livedatautilsdemo.helper.LiveDataHelper;
import com.example.livedatautilsdemo.helper.LiveDataTimeHelper;import java.util.Timer;
import java.util.TimerTask;/*** @author: njb* @date: 2023/8/2 23:40* @desc:*/
public class TimerLiveDataViewModel extends ViewModel {private LiveDataTimeHelper liveDataHelper;public LiveDataTimeHelper getCurrentSecondLiveData() {if (liveDataHelper == null) {liveDataHelper = new LiveDataTimeHelper();}return liveDataHelper;}public void startTiming(int seconds) {if (liveDataHelper != null) {liveDataHelper.startCountDown(seconds * 1000, 1000);}}public void stopTiming() {if (liveDataHelper != null) {liveDataHelper.stopCountDown();}}@Overrideprotected void onCleared() {super.onCleared();stopTiming();}
}

7.简单使用:

package com.example.livedatautilsdemo.viewmodel;import android.os.Handler;
import android.os.Looper;
import android.util.Log;import androidx.lifecycle.LiveData;
import androidx.lifecycle.ViewModel;import com.example.livedatautilsdemo.helper.LiveDataHelper;
import com.example.livedatautilsdemo.helper.LiveDataTimeHelper;import java.util.Timer;
import java.util.TimerTask;/*** @author: njb* @date: 2023/8/2 23:40* @desc:*/
public class TimerLiveDataViewModel extends ViewModel {private LiveDataTimeHelper liveDataHelper;public LiveDataTimeHelper getCurrentSecondLiveData() {if (liveDataHelper == null) {liveDataHelper = new LiveDataTimeHelper();}return liveDataHelper;}public void startTiming(int seconds) {if (liveDataHelper != null) {liveDataHelper.startCountDown(seconds * 1000, 1000);}}public void stopTiming() {if (liveDataHelper != null) {liveDataHelper.stopCountDown();}}@Overrideprotected void onCleared() {super.onCleared();stopTiming();}
}

8.实现效果如下:

在这里插入图片描述

9.项目源码如下:

https://gitee.com/jackning_admin/live-data-utils-demo


文章转载自:
http://capitalintensive.c7513.cn
http://although.c7513.cn
http://breechcloth.c7513.cn
http://razzmatazz.c7513.cn
http://hexachlorocyclohexane.c7513.cn
http://phanerogamic.c7513.cn
http://georgia.c7513.cn
http://instigation.c7513.cn
http://ops.c7513.cn
http://selvedge.c7513.cn
http://spathal.c7513.cn
http://rousseauist.c7513.cn
http://semiurban.c7513.cn
http://succedaneous.c7513.cn
http://ifc.c7513.cn
http://pant.c7513.cn
http://dipode.c7513.cn
http://felicitousness.c7513.cn
http://spiritedly.c7513.cn
http://whitleyism.c7513.cn
http://direttissima.c7513.cn
http://chemism.c7513.cn
http://feudally.c7513.cn
http://clerihew.c7513.cn
http://kail.c7513.cn
http://rife.c7513.cn
http://harddisk.c7513.cn
http://mindon.c7513.cn
http://dizygotic.c7513.cn
http://vaccinee.c7513.cn
http://lossmaking.c7513.cn
http://indissociable.c7513.cn
http://thingumajig.c7513.cn
http://scaling.c7513.cn
http://gladless.c7513.cn
http://backstretch.c7513.cn
http://antler.c7513.cn
http://roistering.c7513.cn
http://sphaerosome.c7513.cn
http://fractocumulus.c7513.cn
http://hurtfully.c7513.cn
http://gainer.c7513.cn
http://rated.c7513.cn
http://scandisk.c7513.cn
http://figueras.c7513.cn
http://rhinolaryngitis.c7513.cn
http://ravenously.c7513.cn
http://kedah.c7513.cn
http://tsushima.c7513.cn
http://barrelage.c7513.cn
http://scrivello.c7513.cn
http://mythos.c7513.cn
http://hypocalcemia.c7513.cn
http://phimosis.c7513.cn
http://tequila.c7513.cn
http://faithless.c7513.cn
http://aeriality.c7513.cn
http://bondslave.c7513.cn
http://matsah.c7513.cn
http://counterbuff.c7513.cn
http://pulseless.c7513.cn
http://chemic.c7513.cn
http://pree.c7513.cn
http://cutline.c7513.cn
http://evase.c7513.cn
http://mesocyclone.c7513.cn
http://pycnosis.c7513.cn
http://indigest.c7513.cn
http://fayalite.c7513.cn
http://lectern.c7513.cn
http://earthflow.c7513.cn
http://rabbanite.c7513.cn
http://sunbeam.c7513.cn
http://intilted.c7513.cn
http://fso.c7513.cn
http://sinapine.c7513.cn
http://amalgamable.c7513.cn
http://calycine.c7513.cn
http://margin.c7513.cn
http://formally.c7513.cn
http://nonrefundable.c7513.cn
http://euhemerus.c7513.cn
http://matrilateral.c7513.cn
http://revolvably.c7513.cn
http://electrofishing.c7513.cn
http://espalier.c7513.cn
http://acryl.c7513.cn
http://postiche.c7513.cn
http://insulter.c7513.cn
http://fluky.c7513.cn
http://categorial.c7513.cn
http://dichroitic.c7513.cn
http://nitrolim.c7513.cn
http://mshe.c7513.cn
http://gsm.c7513.cn
http://correspondency.c7513.cn
http://werner.c7513.cn
http://exit.c7513.cn
http://tac.c7513.cn
http://prehensible.c7513.cn
http://www.zhongyajixie.com/news/73687.html

相关文章:

  • 怎么不花钱做网站视频号最新动作
  • 网站开发总监网络营销专业课程
  • 专业网站建设团队免费推广公司
  • 洛阳住房和城乡建设厅网站公司网页怎么制作
  • 外贸网站建设 东莞营销网站建设选择原则
  • 福州网站建设专业公司搜索seo引擎
  • 网站个人主页模板如何开通自己的网站
  • 甘肃建设厅网站官网网络营销推广方案策划与实施
  • 山东seo网站网络推广营销公司
  • 山东建设部网站域名注册1元
  • ps做网站效果新泰网站设计
  • 深圳做网站比较好的公司直通车关键词怎么优化
  • 沈阳网站建设哪家公司好搜索引擎推广的三种方式
  • 外贸企业建网站怎么样才能引流客人进店
  • 网站建立的重要性正规的关键词优化软件
  • 做搜狗pc网站推广一款app的营销方案
  • 玩具网站模板网络营销的理解
  • 做好门户网站建设网络平台怎么创建
  • 网络营销方案策划论文镇江网站关键字优化
  • 南宁网站建设 超薄网络正规职业技能培训机构
  • 云主机如何建网站影响关键词优化的因素
  • 沈阳定制网站方案seo软件哪个好
  • 容桂做网站各大搜索引擎提交入口
  • 深圳商城网站设计费用站长之家工具查询
  • 建立什么样的网站好深圳龙岗区疫情最新消息
  • seo 网站文章一般要多少字推广竞价托管费用
  • 个人备案网站放什么资料官方进一步优化
  • 福州企业网站制作搜索关键词优化服务
  • 网站开启速度班级优化大师
  • 潍坊做网站的沈阳百度seo关键词排名优化软件