Brief Analysis on Properties of Android Mobile Guard Custom Control

  • 2021-06-29 12:02:58
  • OfStack

Recommended reading: Brief analysis of Android mobile guard turning off automatic updates

The custom combination control completed in the last section is not flexible enough, control display information, imitate the system properties, customize their own properties

In the previous section, there are three controls in the composite control SettingItemView, namely the TextView header, the TextView description, and the CheckBox check box.

Custom attributes tsh:title="Headline" and tsh:desc_on="Subtitle on", tsh:desc_off="Subtitle off"

Add a namespace, xmlns:tsh="http://schemas.android.com/apk/res/Package Name"

Create the attrs.xml file in the res/values/directory

Add Node < declare-styleable name="TextView" >

Add Node Under Node < attr name="title"format="string"/ > , add a node with two other attributes

When a layout file is used, a construction method with two parameters is invoked

In this construct, an AttributeSet object is passed

Call the getAttributeValue() method of the AttributeSet object to get the property value, parameter: index position, not recommended

Call the getAttributeValue (namespace, name) method of the AttributeSet object, parameter: namespace, property name

Call the setText() method of the TextView object to set it directly

Description section, in the setChecked () method, judgment, and setup

SettingItemView.java


package com.qingguow.mobilesafe.ui;
import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.CheckBox;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.qingguow.mobilesafe.R;
public class SettingItemView extends RelativeLayout {
private TextView tv_title;
private TextView tv_desc;
private CheckBox cb_status;
private String desc_on;
private String desc_off;
/**
*  Initialization View object 
* @param context
*/
private void initView(Context context) {
View.inflate(context, R.layout.setting_item_view, this);
cb_status=(CheckBox) this.findViewById(R.id.cb_status);
tv_desc=(TextView) this.findViewById(R.id.tv_desc);
tv_title=(TextView) this.findViewById(R.id.tv_title);
}
/**
*  Determine if selected 
* @return
*/
public boolean isChecked(){
return cb_status.isChecked();
}
/**
*  Set whether selected 
* @param status
*/
public void setChecked(boolean status){
if(status){
tv_desc.setText(desc_on);
}else{
tv_desc.setText(desc_off);
}
cb_status.setChecked(status);
}
/**
*  Set Display Text 
* @param text
*/
public void setDesc(String text){
tv_desc.setText(text);
}
public SettingItemView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
initView(context);
}
public SettingItemView(Context context, AttributeSet attrs) {
super(context, attrs);
initView(context); * // Get properties passed 
String title=attrs.getAttributeValue("http://schemas.android.com/apk/res/com.qingguow.mobilesafe", "title");
tv_title.setText(title);
desc_on=attrs.getAttributeValue("http://schemas.android.com/apk/res/com.qingguow.mobilesafe", "desc_on");
desc_off=attrs.getAttributeValue("http://schemas.android.com/apk/res/com.qingguow.mobilesafe", "desc_off");
}
public SettingItemView(Context context) {
super(context);
initView(context);
}
}

activity_setting.xml


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tsh="http://schemas.android.com/apk/res/com.qingguow.mobilesafe"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:layout_width="match_parent"
android:layout_height="40dp"
android:background="#ccc"
android:gravity="center"
android:text=" set focus "
android:textSize="20sp" />
<com.qingguow.mobilesafe.ui.SettingItemView
tsh:title=" Set up automatic updates "
tsh:desc_on=" Set automatic update on "
tsh:desc_off=" Set Automatic Update Off "
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/siv_item">
</com.qingguow.mobilesafe.ui.SettingItemView>
</LinearLayout> 

attrs.xml


<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="TextView">
<attr name="title" format="string" />
<attr name="desc_on" format="string" />
<attr name="desc_off" format="string" />
</declare-styleable>
</resources>

This is an introduction about the properties of the Android mobile guard custom control, and I hope it will be helpful for you!


Related articles: