Android 自定义preference组件

上篇博文代码实现了Android自带的preference组件,本文将通过实例讲解自定义preference组件。

主要通过以下几步来实现:

1.定义需要的layout布局res->layout->xml文件;

2.通过继承Preference类,来实现自定义preference组件类;

3.通过xml文件引用自定义preference组件,res->xml->xml文件;


下面通过实例来实现:

1.需求:本实例主要实现一个LinearLayout包含ImageView,Button,TextView,通过点击LinearLayout,ImageView,Button,TextView中显示相关的内容;

2.效果图:


3.xml文件

a,layout->custom_preference_activity.xml

<span style="font-size:14px;"><?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="match_parent"
    android:orientation="horizontal"
    android:gravity="center"
    android:id="@+id/my_ll"
     >
    <ImageView
        android:id="@+id/my_iv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:src="@drawable/ic_launcher" >
    </ImageView>

    <Button
        android:id="@+id/my_btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="自定义" >
    </Button>

    <TextView
        android:id="@+id/my_tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="自定义preference" >
    </TextView>

</LinearLayout></span>

b.xml->custom_preference.xml

<span style="font-size:14px;"><?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >
    <PreferenceCategory android:title="自定义项">
        <com.example.customsettingdemo.MyPreference
            android:key="my_preference_key"
            >
        </com.example.customsettingdemo.MyPreference>
    </PreferenceCategory>

</PreferenceScreen>
</span>

4.java类文件

a. 自定义Preference组件 MyPreference.java

<span style="font-size:14px;">package com.example.customsettingdemo;

import android.content.Context;
import android.preference.Preference;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class MyPreference extends Preference{

	private Context mContext;
	private OnClickListener mOnClickListener;
	
	private LinearLayout myLl;
	private ImageView myIv;
	private Button myBtn;
	private TextView myTv;

	public MyPreference(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		this.mContext = context;
		// TODO Auto-generated constructor stub
	}

	public MyPreference(Context context, AttributeSet attrs) {
		this(context, attrs, 0);
		// TODO Auto-generated constructor stub
	}

	public MyPreference(Context context) {
		this(context, null, 0);
		// TODO Auto-generated constructor stub
	}
	
	public void setmOnClickListener(OnClickListener mOnClickListener){
		this.mOnClickListener = mOnClickListener;
	}

	
	@Override
	public View onCreateView(ViewGroup parent){

		LayoutInflater inflater =(LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		View myView = inflater.inflate(R.layout.custom_preference_activity, null);
		
		return myView;
	
	}
	
	@Override
	public void onBindView(View view){
		myLl = (LinearLayout) view.findViewById(R.id.my_ll);
		myIv = (ImageView) view.findViewById(R.id.my_iv);
		myBtn = (Button) view.findViewById(R.id.my_btn);
		myTv = (TextView) view.findViewById(R.id.my_tv);
		
		myLl.setOnClickListener(mOnClickListener);
		myIv.setOnClickListener(mOnClickListener);
		myBtn.setOnClickListener(mOnClickListener);
	}

	public LinearLayout getMyLl() {
		return myLl;
	}

	public ImageView getMyIv() {
		return myIv;
	}


	public Button getMyBtn() {
		return myBtn;
	}


	public TextView getMyTv() {
		return myTv;
	}

}
</span>

b. setting主界面CustomSettingDemo

<span style="font-size:14px;">package com.example.customsettingdemo;

import android.os.Bundle;
import android.preference.PreferenceActivity;
import android.view.View;
import android.view.View.OnClickListener;

public class CustomSettingDemo extends PreferenceActivity implements OnClickListener{
	
	private MyPreference mMyPreference;
	@SuppressWarnings("deprecation")
	@Override
	public void onCreate(Bundle savedInstanceState){
		super.onCreate(savedInstanceState);
		this.addPreferencesFromResource(R.xml.custom_preference);
		
		initPreference();
	}
	
	@SuppressWarnings("deprecation")
	private void initPreference(){
		mMyPreference = (MyPreference)this.findPreference("my_preference_key");
		mMyPreference.setmOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch(v.getId()){
		case R.id.my_ll:
			showInfo("this is LinearLayout");
			break;
		case R.id.my_iv:
			showInfo("this is imageview");
			break;
		case R.id.my_btn:
			showInfo("this is button");
			break;
		}
	}
	
	private void showInfo(String info){
		if(mMyPreference != null){
			mMyPreference.getMyTv().setText(info);
		}
	}
}
</span>

5.AndroidManifest.xml清单文件

<span style="font-size:14px;"><?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.customsettingdemo"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="19" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.customsettingdemo.CustomSettingDemo"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>
</span>

至此,自定义Preference组件代码实现完成。

源代码下载:http://download.csdn.net/detail/a123demi/7569163


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值