Android(1)-自定义属性attrs.xml
此文是在上一遍Android(1)-自定义控件(输入框)extendsLinearLayout的基础上改写的,不同的是,在自定义控件中使用自定义属性。
自定义属性是将attrs.xml文件与自定义控件类文件(CustomEditText.java)关联起来,是文件类可以读到xml自定义属性的值。在自定义控件类里面获取到的值(自定义属性)与自定义控件类俩面的android基本控件的值结合。当然,也可以对获取到的值自由的处理。
文件结构:
src:
MainActivity.java
CustomEditText.java
res:
layout
activity_main.xml
custom_edittest.xml
values
attrs.xml
代码:
MainActivity.java
package com.example.testcustomwidget; import android.app.Activity; import android.os.Bundle; import android.view.Menu; import android.view.View; import android.view.View.OnClickListener; import android.widget.ImageButton; import android.widget.Toast; import com.example.widget.CustomEditText; public class MainActivity extends Activity { private CustomEditText cusEdt; private ImageButton ibTest; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); findViews(); attachEvent(); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } private void findViews() { // TODO Auto-generated method stub cusEdt = (CustomEditText)findViewById(R.id.custom_input); ibTest = (ImageButton)findViewById(R.id.btnTest); } private void attachEvent() { ibTest.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { String strGet = cusEdt.getEditText().getText().toString(); // TODO Auto-generated method stub Toast.makeText(MainActivity.this, "test:" + strGet, Toast.LENGTH_LONG).show(); } }); } }
activity_main.xml
xmlns:test="http://schemas.android.com/apk/res/com.example.testcustomwidget" 中的“com.example.testcustomwidget”为R文件目录
同时这句也可以改为:xmlns:ProgressWheel="http://schemas.android.com/apk/res-auto"
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:test="http://schemas.android.com/apk/res/com.example.testcustomwidget" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:background="@drawable/login_bg" tools:context=".MainActivity" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/hello_world" /> <com.example.widget.CustomEditText android:id="@+id/custom_input" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="100dp" android:layout_marginLeft="20dp" android:layout_marginRight="20dp" test:icon="@drawable/phone_num_icon" /> <ImageButton android:id="@+id/btnTest" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignLeft="@+id/custom_input" android:layout_below="@+id/custom_input" android:layout_marginTop="43dp" android:layout_marginRight="20dp" android:contentDescription="@string/hello_world" android:background="@drawable/bg_button" android:text="@string/hello_world" /> </RelativeLayout>
CustomEditText.java
package com.example.widget; import android.content.Context; import android.content.res.TypedArray; import android.graphics.drawable.Drawable; import android.util.AttributeSet; import android.util.Log; import android.view.LayoutInflater; import android.widget.EditText; import android.widget.ImageView; import android.widget.LinearLayout; import com.example.testcustomwidget.R; public class CustomEditText extends LinearLayout { private ImageView ivEdtIcon; private EditText edtEdtInput; public CustomEditText(Context context) { super(context); } public CustomEditText(Context context, AttributeSet attrs) { super(context, attrs); // TODO Auto-generated constructor stub init(context); initAttr(context, attrs); } private void init(Context context){ LayoutInflater.from(context).inflate(R.layout.custom_edittext, this); ivEdtIcon = (ImageView)findViewById(R.id.edt_icon); edtEdtInput = (EditText)findViewById(R.id.edt_input); }
//mark private void initAttr(Context context, AttributeSet attrs) { TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.CustomEditText); Drawable drawable = a.getDrawable(R.styleable.CustomEditText_icon); Log.v("msg", drawable + ""); if(drawable != null) ivEdtIcon.setImageDrawable(drawable); CharSequence hint = a.getString(R.styleable.CustomEditText_hint); if(hint != null) edtEdtInput.setHint(hint); CharSequence text = a.getString(R.styleable.CustomEditText_text); if(text != null) edtEdtInput.setText(text); //edtEdtInput.setInputType(R.styleable.); a.recycle(); } public EditText getEditText() { return edtEdtInput; } }
custom_edittest.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="match_parent" android:orientation="horizontal" android:background="@android:color/white" > <ImageView android:id="@+id/edt_icon" android:layout_width="wrap_content" android:layout_height="wrap_content" android:contentDescription="@string/hello_world" /> <EditText android:id="@+id/edt_input" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="@string/hello_world" /> </LinearLayout>
attrs.xml
<?xml version="1.0" encoding="utf-8"?> <resources> <!-- 自定义属性 --> <attr name="icon" format="integer" /> <attr name="text" format="string" /> <attr name="hint" format="string" /> <declare-styleable name="CustomEditText"> <attr name="icon" /> <attr name="text" /> <attr name="hint" /> </declare-styleable> </resources>
简单实现,适合学习!原创:http://www.cnblogs.com/TS-qrt/articles/android_attr.html
 
                    
                     
                    
                 
                    
                 
 
         
                
            
         浙公网安备 33010602011771号
浙公网安备 33010602011771号