Android复合控件创建与使用Demo
2015-04-13 21:13
573 查看
1>创建复合控件
1.1>属性值类型声明
1.2>复合控件布局
效果为:
1.3>复合控件代码
2>使用该复合控件
2.1>布局
效果如下:
其中Strings.xml如下:
2.2>MainActivity代码
3>运行效果如下:
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>布局
效果如下:
<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>运行效果如下:
相关文章推荐
- android学习小结5-各种控件使用方式DEMO
- android学习小结3-各种控件使用方式DEMO
- Android中不使用布局文件,java代码动态创建布局及控件
- Android创建新的视图(二)之创建复合控件
- 如何使用LEADTOOLS Android库创建demo
- 【Android】创建自定义复合控件
- android使用java代码创建控件、布局
- android开发游记:meterial design 5.0 开源控件整套合集 及使用demo
- Android 基本控件的使用七(ListView+BaseAdapter)(demo)
- Android的创建复合控件
- 常量,字段,构造方法 调试 ms 源代码 一个C#二维码图片识别的Demo 近期ASP.NET问题汇总及对应的解决办法 c# chart控件柱状图,改变柱子宽度 使用C#创建Windows服务 C#服务端判断客户端socket是否已断开的方法 线程 线程池 Task .NET 单元测试的利剑——模拟框架Moq
- android自定义view(二)创建复合控件
- 《Android 群英传》读书笔记:自定义 View 之创建复合控件
- Android自定义控件——创建复合控件
- android开发游记:meterial design 5.0 开源控件整套合集 及使用demo
- Android创建自定义复合控件
- android使用HttpUrlCollection上传图片到服务器demo
- Android TextView使用SpannableString设置复合文本的方法
- Android 屏幕适配攻略(五)动态创建控件并设置控件的大小
- Android--TabLayout控件的使用