我有一个包含按钮的自定义导航栏,我会调度点击事件,以便包含导航栏的活动可以响应点击
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
}
任何人都可以帮忙吗?
答案 0 :(得分:3)
我相信你要找的是button.performClick()
如果你不想听到咔嗒声,你可以这样做:
button.setSoundEffectsEnabled(false)
答案 1 :(得分:0)
您可以在此处使用EventBus:http://greenrobot.org/eventbus/
加载EventBus
依赖{ 编译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' 编译&#g; org.greenrobot:eventbus:3.0.0' }
创建新的事件类型类。我们称之为BarrePersonnaliseeEvent。
公共类BarrePersonnaliseeEvent {private boolean _touchDown; public BarrePersonnaliseeEvent(boolean touchDown) { _touchDown = touchDown; } public boolean isTouchDown(){ return _touchDown; } }
public class BarrePersonnalisee扩展LinearLayout实现OnClickListener { 上下文mycontext; 查看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
EventBus.getDefault().post(new BarrePersonnaliseeEvent(true));
break;
}
}
....
}
公共类TaxiMapActivity扩展了MapActivity {
BarrePersonnalisee barre;
...
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
barre=(BarrePersonnalisee)this.findViewById(R.id.barre1);
}
@Subscribe(threadMode = ThreadMode.MAIN) public void onBarrePersonnaliseeEvent(BarrePersonnaliseeEvent event){ Toast.makeText(getActivity(),"按钮触及:" + event.isTouchDown(),Toast.LENGTH_SHORT)。show(); }
}
确保在您的活动中注册和取消注册EventBus
@覆盖 public void onStart(Context context){ super.onStart(上下文); 。EventBus.getDefault()寄存器(本); }
@覆盖 public void onStop(){ super.onStop(); 。EventBus.getDefault()取消注册(这); }
答案 2 :(得分:-1)
使用此代码
Button b=new Button(this);
b.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
//what you want to do
}
})
答案 3 :(得分:-1)
假设您需要将点击事件附加到导航栏内的按钮,您可以使用setOnClickListener()
方法,并实施onClick()
方法,例如:
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
//define what you want to do here
Toast.makeText(getApplicationContext(), "Works", Toast.LENGTH_SHORT).show();
}
});
单击按钮时会显示“工作”的祝词。