Mus*_*rer 20 android listview android-contextmenu
我想在单击按钮时打开上下文菜单,但是当我单击按钮时,我还必须知道哪个列表项是聚焦的.你知道怎么做吗?onclick
方法中的代码应该是什么?
dik*_*ill 59
我一直在寻找相同的,并发现不应该使用上下文菜单,而应使用Dialogs
final CharSequence[] items = {"Red", "Green", "Blue"};
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Pick a color");
builder.setItems(items, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int item) {
Toast.makeText(getApplicationContext(), items[item], Toast.LENGTH_SHORT).show();
}
});
AlertDialog alert = builder.create();
alert.show();
Run Code Online (Sandbox Code Playgroud)
http://developer.android.com/guide/topics/ui/dialogs.html#AlertDialog
Yen*_*chi 25
如果你真的想以任何理由这样做......(在我的情况下,出于懒惰)
在onCreate
您的活动期间或用户可以触摸按钮之前的某个位置,请执行registerForContextMenu
该按钮.然后在实际按钮onClick处理程序中调用openContextMenu(View)
.
例如,我有一个在xml中声明的按钮
<Button
android:id="@+id/btn_help"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onHelp"
android:text="@string/help_btn_text" />
Run Code Online (Sandbox Code Playgroud)
在我的onCreate
registerForContextMenu(findViewById(R.id.btn_help));
Run Code Online (Sandbox Code Playgroud)
并在onHelp函数中
public void onHelp(View v) {
openContextMenu(v);
}
Run Code Online (Sandbox Code Playgroud)
这是有效的,因为View v与为上下文菜单注册的视图相同.
归档时间: |
|
查看次数: |
35192 次 |
最近记录: |