Android复合控件创建与使用Demo

1>创建复合控件
1.1>属性值类型声明
<?xml version="1.0" encoding="utf-8"?>
<resources>

    <declare-styleable name="MyCompoundView">
        <attr name="et_hint" format="reference|string"></attr>
        <attr name="bt_text" format="reference|string"></attr>
    </declare-styleable>

</resources>

1.2>复合控件布局
<?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="horizontal">
    <EditText
        android:id="@+id/newItem_et"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"/>
    <Button
        android:id="@+id/done_bt"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</LinearLayout>

效果为:

1.3>复合控件代码
package com.demo.cxc.compoundview;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;

/**
 * Created by CXC on 15/4/12.
 */
public class MyCompoundView extends LinearLayout {
    private EditText newItem_et;
    private Button done_bt;

    OnButtonClickListener buttonClickListener;

    public MyCompoundView(Context context) {
        super(context);
        init(null,0);


    }

    public MyCompoundView(Context context, AttributeSet attrs) {
        /*
        ---注意调用的父类构造函数形式,如果使用super(context);
        在Activity中通过findFragmentById()or findFragmentByTag()时返回Null,
        进而使用该Fragment时会出现NullPointerException
        */
        super(context,attrs);
        init(attrs,0);

    }

    private void init(AttributeSet attrs, int defStyles) {
        //使用布局资源填充视图
        LayoutInflater layoutInflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        View view = layoutInflater.inflate(R.layout.compound_view_layout, this, true);

        //获得对子控件的引用
        newItem_et = (EditText) view.findViewById(R.id.newItem_et);
        done_bt = (Button) view.findViewById(R.id.done_bt);

        //获取设置的各个属性
        final TypedArray a = getContext().obtainStyledAttributes(attrs, R.styleable.MyCompoundView, defStyles, 0);
        String newItem_et_hint = a.getString(R.styleable.MyCompoundView_et_hint);
        String done_bt_text = a.getString(R.styleable.MyCompoundView_bt_text);
        //设置属性值
        newItem_et.setHint(newItem_et_hint);
        done_bt.setText(done_bt_text);

        done_bt.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if(buttonClickListener==null){
                    return ;
                }
                //调用listener
                buttonClickListener.onButtonClick(newItem_et.getText().toString());
                newItem_et.setText("");

            }
        });
    }

    public void setOnButtonClickListener(OnButtonClickListener listener) {
        this.buttonClickListener = listener;
    }

    public interface OnButtonClickListener {
        public void onButtonClick(String newItemString);
    }

}

2>使用该复合控件
2.1>布局
效果如下:
Android复合控件创建与使用Demo_第1张图片
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity">


    <!--使用该组合控件-->
    <!--xmlns:cv="http://schemas.android.com/apk/res/AndroidManifest文件中的package属性值"-->
    <com.demo.cxc.compoundview.MyCompoundView xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:cv="http://schemas.android.com/apk/res/com.demo.cxc.compoundview"
        android:id="@+id/new_item"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        cv:bt_text="@string/done_bt_text"
        cv:et_hint="@string/new_item_et_hint"/>
    <TextView
        android:id="@+id/show_tv"
        android:text="Show Text..."
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />




</LinearLayout>



其中Strings.xml如下:
<resources>
    <string name="app_name">CompoundView</string>

    <string name="action_settings">Settings</string>
    <string name="done_bt_text">Done</string>
    <string name="new_item_et_hint">Please input what you want to do ...</string>
</resources>

2.2>MainActivity代码
package com.demo.cxc.compoundview;

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.EditText;
import android.widget.TextView;

/*实现 MyCompoundView.OnButtonClickListener接口,以便实现相关操作*/
public class MainActivity extends ActionBarActivity implements MyCompoundView.OnButtonClickListener {
    private MyCompoundView new_item_cv;

    private TextView show_tv;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initViews();
    }
    private void initViews() {

        new_item_cv = (MyCompoundView) findViewById(R.id.new_item);
        if (new_item_cv == null) {
            Log.i("CXC", "***********Null---------");
        } else {
            new_item_cv.setOnButtonClickListener(this);
        }

        show_tv=(TextView)findViewById(R.id.show_tv);

    }

    @Override
    protected void onResume() {
        super.onResume();

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onButtonClick(String newItemString) {
        //在这里进行显示操作
        show_tv.setText(newItemString);
    }
}


3>运行效果如下:




你可能感兴趣的:(Fragment)