Android 시작 페이지 사용자 관련 정책 상자 구현 코드

현재 안 드 로 이 드 가 각 플랫폼 에 올 라 가면 앱 홈 페이지 에 탄 상 자 를 추가 하고 사용자 프로 토 콜 과 프라이버시 정책 을 표시 하 라 고 요구 합 니 다.그렇지 않 으 면 각 플랫폼 에 올 라 가 이 대화 상 자 를 간단하게 실현 하 겠 습 니 다.
대화 상자 인 이상 대화 상 자 를 간단하게 밀봉 하여 후속 적 인 수정 을 편리 하 게 합 니 다.
widget_user_dialog.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.appcompat.widget.LinearLayoutCompat
 xmlns:android="http://schemas.android.com/apk/res/android"
 android:orientation="vertical"
 android:background="@drawable/bg_sprint_border"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content">
 <androidx.appcompat.widget.LinearLayoutCompat
  android:background="@drawable/bg_sprint_border"
  android:orientation="vertical"
  android:layout_marginLeft="25dp"
  android:layout_marginRight="25dp"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content">
  <androidx.appcompat.widget.AppCompatTextView
   android:id="@+id/tv_sprint_title"
   android:text="Sprint"
   android:padding="12dp"
   android:layout_gravity="center_horizontal"
   android:textSize="18sp"
   android:textColor="@color/colorBlack"
   android:textStyle="bold"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"/>
  <androidx.appcompat.widget.AppCompatTextView
   android:id="@+id/tv_sprint_content"
   android:text="                                                                "
   android:layout_gravity="center_horizontal"
   android:padding="8dp"
   android:textSize="14sp"
   android:textColor="@color/colorBlack"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"/>
  <View
   android:layout_width="match_parent"
   android:layout_height="0.5dp"
   android:background="@color/colorLightGrey_1" />
  <LinearLayout
   android:layout_width="match_parent"
   android:layout_height="40dp"
   android:layout_marginBottom="0.1dp"
   android:layout_marginLeft="0.1dp"
   android:layout_marginRight="0.1dp"
   android:orientation="horizontal">
   <TextView
    android:id="@+id/tv_dialog_no"
    android:layout_width="0dp"
    android:layout_height="fill_parent"
    android:layout_weight="1"
    android:background="@drawable/bg_sprint_cancle"
    android:gravity="center_horizontal|center_vertical"
    android:text="  "
    android:textSize="15sp"
    android:textColor="@color/colorBlack"/>
   <View
    android:layout_width="0.5dp"
    android:layout_height="match_parent"
    android:background="@color/colorLightGrey_1" />
   <!--android:background="@drawable/message_dialog_bottom_right"-->
   <TextView
    android:id="@+id/tv_dialog_ok"
    android:layout_width="0dp"
    android:layout_height="fill_parent"
    android:layout_weight="1"
    android:gravity="center_horizontal|center_vertical"
    android:text="  "
    android:textSize="15sp"
    android:background="@drawable/bg_sprint_agreen_commit"
    android:textColor="@color/colorFont_0098FA" />
  </LinearLayout>
 </androidx.appcompat.widget.LinearLayoutCompat>
</androidx.appcompat.widget.LinearLayoutCompat>
drawable 파일 은 여기 서 놓 지 않 습 니 다.도 모 에 게 물 어 봐 도 될 지 모 르 겠 습 니 다.주로 원 각 을 설정 하고 색깔 도 있 습 니 다.
AgreementDialog.java
제목,확인,취소 등 일부 컨트롤 의 패 키 징 을 포함 한 패 키 징 대화 상자 입 니 다.주로 스 파 나 블 스 트 링 으로 콘 텐 츠 편집 을 실현 합 니 다.지정 한 콘 텐 츠 의 프 리 젠 테 이 션 색상,크기,스타일 등 을 설정 할 수 있 습 니 다.필요 하 시 면 직접 확장 하 실 수 있 습 니 다.

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.text.SpannableString;
import android.text.TextUtils;
import android.text.method.LinkMovementMethod;
import android.view.View;
import android.view.Window;
import android.widget.TextView;
import androidx.annotation.NonNull;
import com.huantek.module.sprint.R;
public class AgreementDialog extends Dialog {
 private Context context;
 private TextView tv_tittle;
 private TextView tv_content;
 private TextView tv_dialog_ok;
 private TextView tv_dialog_no;
 private String title;
 private SpannableString str;
 private View.OnClickListener mClickListener;
 private String btnName;
 private String no;
 private String strContent;
 public AgreementDialog(@NonNull Context context) {
  super(context);
 }
 //    
 public AgreementDialog(Context context, SpannableString content, String strContent, String title) {
  super(context, R.style.MyDialog);
  this.context = context;
  this.str = content;
  this.strContent = strContent;
  this.title = title;
 }
 public AgreementDialog setOnClickListener(View.OnClickListener onClick) {
  this.mClickListener = onClick;
  return this;
 }
 public AgreementDialog setBtName(String yes, String no) {
  this.btnName = yes;
  this.no = no;
  return this;
 }
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  requestWindowFeature(Window.FEATURE_NO_TITLE);
  setContentView(R.layout.widget_sprint_user_dialog);
  initView();
 }
 private void initView() {
  tv_dialog_ok = (TextView) findViewById(R.id.tv_dialog_ok);
  tv_tittle = (TextView) findViewById(R.id.tv_sprint_title);
  tv_content = (TextView) findViewById(R.id.tv_sprint_content);
  tv_dialog_no = (TextView) findViewById(R.id.tv_dialog_no);
  tv_content.setMovementMethod(LinkMovementMethod.getInstance());
  if (!TextUtils.isEmpty(btnName)) {
   tv_dialog_ok.setText(btnName);
  }
  if (!TextUtils.isEmpty(no)) {
   tv_dialog_no.setText(no);
  }
  //             
  this.setCanceledOnTouchOutside(false);
  this.setCancelable(true);
  tv_dialog_ok.setOnClickListener(new View.OnClickListener() {
   @Override
   public void onClick(View arg0) {
    AgreementDialog.this.dismiss();
    if (mClickListener != null) {
     mClickListener.onClick(tv_dialog_ok);
    }
   }
  });
  tv_dialog_no.setOnClickListener(new View.OnClickListener() {
   @Override
   public void onClick(View arg0) {
    AgreementDialog.this.dismiss();
    if (mClickListener != null) {
     mClickListener.onClick(tv_dialog_no);
    }
   }
  });
  if (TextUtils.isEmpty(strContent)) {
   tv_content.setText(str);
  } else {
   tv_content.setText(strContent);
  }
  tv_tittle.setText(title);
 }
}
이 대화 상 자 는 매번 튕 겨 나 와 야 하 는 것 이 아니 라 사용자 가 처음 설치 할 때 만 튕 겨 나 오고 뒤에서 시작 하면 튕 겨 나 올 필요 가 없 기 때문에 사용자 가 처음 사용 하 는 지 아 닌 지 판단 해 야 합 니 다.
먼저 boolean 의 값 을 정의 하여 사용자 가 처음 사용 하 는 지 아 닌 지 를 판단 하 는 데 사용 합 니 다.

//        
 private boolean isFirstUse;
이 값 이 false 인지 true 인지 Shared Preferences 로 저장 할 수 있 습 니 다.

SharedPreferences preferences = getSharedPreferences("isFirstUse", MODE_PRIVATE);
  //     true
  isFirstUse = preferences.getBoolean("isFirstUse", true);
처음 사용 하 는 것 이 라면 제목,내용 등 관련 값 을 설정 하고 그렇지 않 으 면 조작 하지 않 습 니 다.

 new AgreementDialog(context, generateSp("        " + AppUtils.getAppName(this)+"XXXXXX《"+ AppUtils.getAppName(this) + "    》" +
     " 《"+ AppUtils.getAppName(this) + "    》" +
     "XXXXX"),null,"    ").setBtName("  ", "   ")
     .setOnClickListener(new View.OnClickListener() {

      @Override
      public void onClick(View v) {
       switch (v.getId()){
        case R.id.tv_dialog_ok:
         //   Editor  
         SharedPreferences.Editor editor = preferences.edit();
         //    
         editor.putBoolean("isFirstUse", false);
         //    
         editor.commit();
         //           ,           
         requirePermission();

         break;
        case R.id.tv_dialog_no:
         finish();
         break;
       }

      }
     }).show();
  } else {
  }
꼭.show()를 기억 하 세 요.그렇지 않 으 면 대화 상자 가 튀 어 나 오지 않 습 니 다.이 안의 중점 부분 은 generateSp()방법 입 니 다.여 기 는'사용자 프로 토 콜'이라는 글꼴 의 색 을 설정 하기 위 한 것 입 니 다.

 private SpannableString generateSp(String text) {
 //         
  String high_light_1 = "《    》";
  String high_light_2 = "《    》";
  
  SpannableString spannableString = new SpannableString(text);
  //    
  int start = 0;
  //    
  int end;
  int index;
  //indexOf(String str, int fromIndex):     fromIndex                         ,              ,    -1。
  //    ,(index = text.indexOf(high_light_1, start)) > -1                    high_light_1      ,         
  while ((index = text.indexOf(high_light_1, start)) > -1) {
   //     
   end = index + high_light_1.length();
   spannableString.setSpan(new QMUITouchableSpan(this.getResources().getColor(R.color.colorFont_0098FA), this.getResources().getColor(R.color.colorFont_0098FA),
     this.getResources().getColor(R.color.colorWhite), this.getResources().getColor(R.color.colorWhite)) {
    @Override
    public void onSpanClick(View widget) {
     
     //            ,    WebView       
    }
   }, index, end, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
   start = end;
  }

  start = 0;
  while ((index = text.indexOf(high_light_2, start)) > -1) {
   end = index + high_light_2.length();
   spannableString.setSpan(new QMUITouchableSpan(this.getResources().getColor(R.color.colorFont_0098FA), this.getResources().getColor(R.color.colorFont_0098FA),
     this.getResources().getColor(R.color.colorWhite), this.getResources().getColor(R.color.colorWhite)) {
    @Override
    public void onSpanClick(View widget) {
      //            ,    WebView       

    }
   }, index, end, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
   start = end;
  }
  //    SpannableString
  return spannableString;
 }
마지막 으로 QMUITouchableSpan.자바.
사용자 클릭 시 관련 동작 을 터치 합 니 다.

/**
 * Created by Sammi on 2020/2/27.
 * /**
 *   Touch   Span,  {@link #setPressed(boolean)}       pressed       UI  
 * <p>
 *      span              ,       
 * </p>
 */

public abstract class QMUITouchableSpan extends ClickableSpan {
 private boolean mIsPressed;
 @ColorInt private int mNormalBackgroundColor;
 @ColorInt private int mPressedBackgroundColor;
 @ColorInt private int mNormalTextColor;
 @ColorInt private int mPressedTextColor;

 private boolean mIsNeedUnderline = false;

 public abstract void onSpanClick(View widget);

 @Override
 public final void onClick(View widget) {

  if (ViewCompat.isAttachedToWindow(widget)) {
   onSpanClick(widget);

  }
 }


 public QMUITouchableSpan(@ColorInt int normalTextColor,
        @ColorInt int pressedTextColor,
        @ColorInt int normalBackgroundColor,
        @ColorInt int pressedBackgroundColor) {
  mNormalTextColor = normalTextColor;
  mPressedTextColor = pressedTextColor;
  mNormalBackgroundColor = normalBackgroundColor;
  mPressedBackgroundColor = pressedBackgroundColor;
 }

 public int getNormalBackgroundColor() {
  return mNormalBackgroundColor;
 }

 public void setNormalTextColor(int normalTextColor) {
  mNormalTextColor = normalTextColor;
 }

 public void setPressedTextColor(int pressedTextColor) {
  mPressedTextColor = pressedTextColor;
 }

 public int getNormalTextColor() {
  return mNormalTextColor;
 }

 public int getPressedBackgroundColor() {
  return mPressedBackgroundColor;
 }

 public int getPressedTextColor() {
  return mPressedTextColor;
 }

 public void setPressed(boolean isSelected) {
  mIsPressed = isSelected;
 }

 public boolean isPressed() {
  return mIsPressed;
 }

 public void setIsNeedUnderline(boolean isNeedUnderline) {
  mIsNeedUnderline = isNeedUnderline;
 }

 @Override
 public void updateDrawState(TextPaint ds) {
  ds.setColor(mIsPressed ? mPressedTextColor : mNormalTextColor);
  ds.bgColor = mIsPressed ? mPressedBackgroundColor
    : mNormalBackgroundColor;
  ds.setUnderlineText(mIsNeedUnderline);
 }
}
효과 도 를 첨부 하 다
在这里插入图片描述
총결산
안 드 로 이 드 시작 페이지 사용자 관련 정책 탄 상자 의 실현 에 관 한 이 글 은 여기까지 소개 되 었 습 니 다.더 많은 안 드 로 이 드 시작 페이지 사용자 관련 정책 탄 상자 의 실현 내용 은 우리 의 이전 글 을 검색 하거나 아래 의 관련 글 을 계속 조회 하 시기 바 랍 니 다.앞으로 많은 지원 바 랍 니 다!

좋은 웹페이지 즐겨찾기