Android为自定义控件添加事件
2013-08-15 14:58
295 查看
1)文章简介
创建自定义控件并为它添加一个自定义事件
当用户单击自定义控件中的“测试”按钮时触发按钮自定义事件
2)定义一个layout(activity_custom.xml)作为自定义控件的布局代码如下
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<EditText
android:id="@+id/editText1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="text" >
<requestFocus />
</EditText>
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
</LinearLayout>3)接下来写一个类(CustomUserControl.java)继承LinearLayout,导入刚刚的布局,添加自定义事件并通过单击“测试按钮”触发自定义事件
package com.example.androidtest2;
import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
public class CustomUserControl extends LinearLayout{
/*定义接口*/
public interface IMyClick{
public void onMyClick(String str);
}
/*初始化接口变量*/
IMyClick iMyClick=null;
/*自定义事件*/
public void setOnMyClickListener(IMyClick _iMyClick){
iMyClick=_iMyClick;
}
private Button button1=null;
private EditText editText1=null;
public CustomUserControl(Context context){
super(context);
}
public CustomUserControl(Context context, AttributeSet attr){
super(context, attr);
LayoutInflater layoutInflater=
(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
layoutInflater.inflate(R.layout.activity_custom, this);
editText1=(EditText)findViewById(R.id.editText1);
/*设置按钮的点击事件*/
button1=(Button)findViewById(R.id.button1);
button1.setOnClickListener(new Button.OnClickListener() {
@Override
public void onClick(View v) {
/*使用回调实现返回editText1内容*/
iMyClick.onMyClick(editText1.getText().toString());
}
});
}
/*为Button按钮添加内容*/
public void setButtonText(String text){
button1.setText(text);
}
}4)前台(activity_main.xml)调用自定义控件代码如下
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/linearLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<com.example.androidtest2.CustomUserControl
android:id="@+id/customUserControl1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
</com.example.androidtest2.CustomUserControl>
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
</LinearLayout>4)后台(MainActivity.java)触发自定义事件代码如下
package com.example.androidtest2;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.TextView;
public class MainActivity extends Activity {
private CustomUserControl customUserControl1=null;
private TextView textView1=null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView1=(TextView)findViewById(R.id.textView1);
customUserControl1=(CustomUserControl)findViewById(R.id.customUserControl1);
customUserControl1.setButtonText("测试");
customUserControl1.setOnMyClickListener(new CustomUserControl.IMyClick() {
@Override
public void onMyClick(String str) {
textView1.setText(str);
}
});
}
@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;
}
}
创建自定义控件并为它添加一个自定义事件
当用户单击自定义控件中的“测试”按钮时触发按钮自定义事件
2)定义一个layout(activity_custom.xml)作为自定义控件的布局代码如下
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<EditText
android:id="@+id/editText1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="text" >
<requestFocus />
</EditText>
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
</LinearLayout>3)接下来写一个类(CustomUserControl.java)继承LinearLayout,导入刚刚的布局,添加自定义事件并通过单击“测试按钮”触发自定义事件
package com.example.androidtest2;
import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
public class CustomUserControl extends LinearLayout{
/*定义接口*/
public interface IMyClick{
public void onMyClick(String str);
}
/*初始化接口变量*/
IMyClick iMyClick=null;
/*自定义事件*/
public void setOnMyClickListener(IMyClick _iMyClick){
iMyClick=_iMyClick;
}
private Button button1=null;
private EditText editText1=null;
public CustomUserControl(Context context){
super(context);
}
public CustomUserControl(Context context, AttributeSet attr){
super(context, attr);
LayoutInflater layoutInflater=
(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
layoutInflater.inflate(R.layout.activity_custom, this);
editText1=(EditText)findViewById(R.id.editText1);
/*设置按钮的点击事件*/
button1=(Button)findViewById(R.id.button1);
button1.setOnClickListener(new Button.OnClickListener() {
@Override
public void onClick(View v) {
/*使用回调实现返回editText1内容*/
iMyClick.onMyClick(editText1.getText().toString());
}
});
}
/*为Button按钮添加内容*/
public void setButtonText(String text){
button1.setText(text);
}
}4)前台(activity_main.xml)调用自定义控件代码如下
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/linearLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<com.example.androidtest2.CustomUserControl
android:id="@+id/customUserControl1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
</com.example.androidtest2.CustomUserControl>
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
</LinearLayout>4)后台(MainActivity.java)触发自定义事件代码如下
package com.example.androidtest2;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.TextView;
public class MainActivity extends Activity {
private CustomUserControl customUserControl1=null;
private TextView textView1=null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView1=(TextView)findViewById(R.id.textView1);
customUserControl1=(CustomUserControl)findViewById(R.id.customUserControl1);
customUserControl1.setButtonText("测试");
customUserControl1.setOnMyClickListener(new CustomUserControl.IMyClick() {
@Override
public void onMyClick(String str) {
textView1.setText(str);
}
});
}
@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;
}
}
相关文章推荐
- Android自定义控件系列 十:利用添加自定义布局来搞定触摸事件的分发,解决组合界面中特定控件响应特定方向的事件
- Android自定义控件系列 十:利用添加自定义布局来搞定触摸事件的分发,解决组合界面中特定控件响应特定方向的事件
- Android自定义控件系列 十:利用添加自定义布局来搞定触摸事件的分发,解决组合界面中特定控件响应特定方向的事件
- Android为自定义控件添加事件
- Android自定义控件系列 十:利用添加自定义布局来搞定触摸事件的分发,解决组合界面中特定控件响应特定方向的事件
- Android自定义控件系列 十:利用添加自定义布局来搞定触摸事件的分发,解决组合界面中特定控件响应特定方向的事件
- Android自定义控件系列 十:利用添加自定义布局来搞定触摸事件的分发,解决组合界面中特定控件响应特定方向的事件
- Android自定义控件系列 六:利用添加自定义布局来搞定触摸事件的分发,解决组合界面中特定控件响应特定方向的事件
- android自定义控件实现左右划出菜单并添加点击事件
- Android中Recyclerview使用6----添加条目得到点击事件和长按事件(另一种写法,较简单)
- Android RecyclerView给Item添加点击事件
- Android Databinding 从入门到转行(二)添加点击事件
- Android 给TextView添加点击事件
- android drawableRight 添加点击事件
- 为ASP.NET WEB自定义控件添加提交事件
- Android学习(44) -- 自定义控件(8) 下拉刷新(添加header)
- android监听事件添加动作的三种方式
- android WebView中图片适配屏幕大小并添加添加事件
- c# web 为自定义控件添加事件
- Android开发中添加View控件的点击事件