Android自定义PreferenceScreen的Layout布局,并获取控件
生活随笔
收集整理的這篇文章主要介紹了
Android自定义PreferenceScreen的Layout布局,并获取控件
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
先說一下需求,要在<PreferenceScreen>里添加一個自定義的Layout,實現(xiàn)如下效果:
操作步驟:
1、在res/layout目錄創(chuàng)建一個xml文件,名為my_preference_layout.xml,代碼如下:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="vertical"><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="horizontal"><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginLeft="15dp"android:textColor="#000000"android:text="手機編號:" /><TextViewandroid:id="@+id/mobileid"android:layout_width="0dp"android:layout_height="wrap_content"android:inputType="textPersonName"android:cursorVisible="false"android:text=""android:layout_weight="1"/><Buttonandroid:id="@+id/btnCopyMobileid"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginLeft="0dp"android:layout_marginRight="0dp"android:layout_centerHorizontal="true"android:layout_marginTop="0dp"android:minHeight="28dp"android:text="點擊復(fù)制"android:textColor="#fff"android:background="#D41A5E"android:textSize="16sp" /></LinearLayout> </LinearLayout>2、創(chuàng)建一個自定義控件myPreference.java,繼承自android.preference.Preference,代碼如下:
package net.zy13.skhelper.view;import android.content.ClipData; import android.content.ClipboardManager; import android.content.Context; import android.preference.Preference; import android.util.AttributeSet; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.Button; import android.widget.TextView; import android.widget.Toast;import net.zy13.skhelper.MainApplication; import net.zy13.skhelper.R; import net.zy13.skhelper.utils.PreferenceUtil;public class MyPreference extends Preference {private Context mContext;private TextView mTextViewMobileid;private Button mButtonCopyMobileid;public MyPreference(Context context) {this(context, null);}public MyPreference(Context context, AttributeSet attrs) {//這里構(gòu)造方法也很重要,不加這個很多屬性不能再XML里面定義this(context, attrs, android.R.attr.editTextStyle);}public MyPreference(Context context, AttributeSet attrs, int defStyle) {super(context, attrs, defStyle);//獲取上下文mContext=context;}protected View onCreateView(ViewGroup parent) {// TODO Auto-generated method stubsuper.onCreateView(parent);LayoutInflater inflater=(LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);View view = inflater.inflate(R.layout.preference_mobileid_layout, null);//獲取控件mButtonCopyMobileid=view.findViewById(R.id.btnCopyMobileid);mTextViewMobileid = (TextView)view.findViewById(R.id.mobileid);//監(jiān)聽點擊事件(匿名類方式,或叫內(nèi)部類方式)mButtonCopyMobileid.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View arg0) {// TODO Auto-generated method stub}});return view;} }我們創(chuàng)建這個myPreference.java自定義控件的作用:
- 用來讀取我們添加layout布局文件my_preference_layout.xml,同時操作布局里的控件;
- 這個myPreference.java類最重要的就是重寫父類的onCreateView方法;
3、在PreferenceScreen的xml里使用我們自定義的控件,如下圖:
?具體代碼:
<?xml version="1.0" encoding="utf-8"?> <PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"><PreferenceCategoryandroid:key="settings"android:title="設(shè)置"><net.zy13.skhelper.view.MyPreferenceandroid:layout="@layout/my_preference_layout"/><SwitchPreferenceandroid:title="跳過手機編號(mobileid)的加密"android:defaultValue="true"android:summary="開啟后,選擇加密也不會加密mobileid參數(shù)"android:key="isnotencryptmobileid" /><SwitchPreferenceandroid:title="是否加密數(shù)據(jù)"android:key="isencrypt"android:defaultValue="true" /><ListPreferenceandroid:title="數(shù)據(jù)加密方式"android:key="encryptmethod"android:summary="接口數(shù)據(jù)加密方式,md5為sign簽名驗證,des為端對端對稱加密"android:defaultValue="md5"android:entries="@array/api_encrypt_method"android:entryValues="@array/api_encrypt_method_value" /></PreferenceCategory> </PreferenceScreen>?這樣就ok了,網(wǎng)上有很多類似的教程,但是都不詳細,所以我寫了一個完整的分享給大家。
總結(jié)
以上是生活随笔為你收集整理的Android自定义PreferenceScreen的Layout布局,并获取控件的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: OpenMP4.0: #pragma o
- 下一篇: hrbusrt 1900 权限问题