Android 中如何调度点击事件
我有一个包含按钮的自定义导航栏,我将调度点击事件,以便包含我的导航栏的活动可以响应点击
public class BarrePersonnalisee extends LinearLayout implements OnClickListener {
Context mycontext;
View convertview;
ImageButton searchadresse;
public BarrePersonnalisee(Context context, AttributeSet attrs) {
super(context, attrs);
mycontext=context;
convertview=LayoutInflater.from(mycontext).inflate(R.layout.barre, this);
searchadresse=(ImageButton)convertview.findViewById(R.id.searchadresse);
searchadresse.setOnClickListener(this);
}
public void onClick(View arg0) {
switch (arg0.getId()) {
case R.id.searchadresse:
//I want to dispatch this event
break;
}
}
....
}
public class TaxiMapActivity extends MapActivity{
BarrePersonnalisee barre;
...
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
barre=(BarrePersonnalisee)this.findViewById(R.id.barre1);
//task to do here
}
有人可以帮忙吗?
I have a custom navigation bar that contains a button, I would dispatch the click event so that the activity that contains my navigation bar can respond to click
public class BarrePersonnalisee extends LinearLayout implements OnClickListener {
Context mycontext;
View convertview;
ImageButton searchadresse;
public BarrePersonnalisee(Context context, AttributeSet attrs) {
super(context, attrs);
mycontext=context;
convertview=LayoutInflater.from(mycontext).inflate(R.layout.barre, this);
searchadresse=(ImageButton)convertview.findViewById(R.id.searchadresse);
searchadresse.setOnClickListener(this);
}
public void onClick(View arg0) {
switch (arg0.getId()) {
case R.id.searchadresse:
//I want to dispatch this event
break;
}
}
....
}
public class TaxiMapActivity extends MapActivity{
BarrePersonnalisee barre;
...
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
barre=(BarrePersonnalisee)this.findViewById(R.id.barre1);
//task to do here
}
can anyone help?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
使用此代码
Use this code
假设您需要将点击事件附加到导航栏中的按钮,您可以使用 setOnClickListener() 方法,并实现 onClick() 方法,例如
:当您单击按钮时,将显示一个提示“有效”的提示。
Assuming that you need to attach click event to a button inside your navigation bar, you can use
setOnClickListener()
method, and implements theonClick()
method, for example:that will show a toast saying "Works" when you click the button.
我相信您正在寻找的是
button.performClick()
如果您不想要点击声音,您可以这样做:
I believe what you are looking for is
button.performClick()
and if you don't want the click sound, you can do:
您实际上可以在这里使用EventBus: http://greenrobot.org/eventbus/
加载EventBus < /p>
依赖项{
编译 fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
编译'com.android.support:appcompat-v7:24.1.1'
编译'com.android.support:support-v4:24.1.1'
编译 'org.greenrobot:eventbus:3.0.0'
}
创建新的事件类型类。我们将其命名为 BarrePersonnaliseeEvent。
public class BarrePersonnalisee extends LinearLayout Implements OnClickListener {
上下文 mycontext;
查看转换视图;
图像按钮搜索地址;
public class TaxiMapActivity extends MapActivity{
@Subscribe(threadMode = ThreadMode.MAIN)
公共无效onBarrePersonnaliseeEvent(BarrePersonnaliseeEvent事件){
Toast.makeText(getActivity(), "触摸按钮:" + event.isTouchDown(), Toast.LENGTH_SHORT).show();
}
}
确保在您的 Activity 中注册和取消注册 EventBus
@覆盖
公共无效onStart(上下文上下文){
super.onStart(上下文);
EventBus.getDefault().register(this);
}
@覆盖
公共无效onStop(){
super.onStop();
EventBus.getDefault().unregister(this);
确保
You can actually use EventBus here: http://greenrobot.org/eventbus/
Load the EventBus
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:24.1.1'
compile 'com.android.support:support-v4:24.1.1'
compile 'org.greenrobot:eventbus:3.0.0'
}
Create new Event type Class. Let's call it BarrePersonnaliseeEvent.
public class BarrePersonnalisee extends LinearLayout implements OnClickListener {
Context mycontext;
View convertview;
ImageButton searchadresse;
public class TaxiMapActivity extends MapActivity{
@Subscribe(threadMode = ThreadMode.MAIN)
public void onBarrePersonnaliseeEvent(BarrePersonnaliseeEvent event) {
Toast.makeText(getActivity(), "Button touched: " + event.isTouchDown(), Toast.LENGTH_SHORT).show();
}
}
Make sure to register and unregister for EventBus in your Activity
@Override
public void onStart(Context context) {
super.onStart(context);
EventBus.getDefault().register(this);
}
@Override
public void onStop() {
super.onStop();
EventBus.getDefault().unregister(this);
}