android判断按钮是否按下,android – 如何检测是否按下了向上按钮

http://developer.android.com/guide/topics/ui/actionbar.html#Handling

处理对操作项的点击

当用户按下某个动作时,系统将调用您的活动的onOptionsItemSelected()方法.使用传递给此方法的MenuItem,您可以通过调用getItemId()来识别该操作.这将返回标记的id属性提供的唯一ID,以便您可以执行相应的操作.例如:

@Override

public boolean onOptionsItemSelected(MenuItem item) {

// Handle presses on the action bar items

switch (item.getItemId()) {

case android.R.id.home:

onUpButtonPressed();

return true;

case R.id.action_search:

openSearch();

return true;

case R.id.action_compose:

composeMessage();

return true;

default:

return super.onOptionsItemSelected(item);

}

}

Note: If you inflate menu items from a fragment,via the Fragment

class’s onCreateOptionsMenu() callback,the system calls

onOptionsItemSelected() for that fragment when the user selects one of

those items. However,the activity gets a chance to handle the event

first,so the system first calls onOptionsItemSelected() on the

activity,before calling the same callback for the fragment. To ensure

that any fragments in the activity also have a chance to handle the

callback,always pass the call to the superclass as the default

behavior instead of returning false when you do not handle the item.

要将应用程序图标启用为向上按钮,请调用setDisplayHomeAsUpEnabled().例如:

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_details);

ActionBar actionBar = getSupportActionBar();

actionBar.setDisplayHomeAsUpEnabled(true);

...

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值