使用标准或支持ActionMode,下面链接的问题中的过程对我来说似乎不起作用。 ACTION_UP永远不会作为参数出现,虽然ACTION_DOWN可以,但无论如何,ActionMode仍然被取消。
Prevent to cancel Action Mode by press back button
有人建议如何在按下后退键时阻止动作模式关闭吗?
我的代码
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class test extends AppCompatActivity {
Toolbar toolbar;
Button standard;
Button support;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.test);
toolbar= (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
standard= (Button) findViewById(R.id.standard);
standard.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActionMode(standardCallback);
standard.setVisibility(View.GONE);
support.setVisibility(View.GONE);
}
});
support= (Button) findViewById(R.id.support);
support.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startSupportActionMode(supportCallback);
standard.setVisibility(View.GONE);
support.setVisibility(View.GONE);
}
});
}
private android.view.ActionMode.Callback standardCallback=new android.view.ActionMode.Callback() {
@Override
public boolean onCreateActionMode(android.view.ActionMode mode, Menu menu) {
mode.setTitle("Standard Action Mode Enabled");
return true;
}
@Override
public boolean onPrepareActionMode(android.view.ActionMode mode, Menu menu) {
return false;
}
@Override
public boolean onActionItemClicked(android.view.ActionMode mode, MenuItem item) {
return false;
}
@Override
public void onDestroyActionMode(android.view.ActionMode mode) {
standard.setVisibility(View.VISIBLE);
support.setVisibility(View.VISIBLE);
}
};
//requires theme entry: <item name="windowActionModeOverlay">true</item>
private android.support.v7.view.ActionMode.Callback supportCallback=new android.support.v7.view.ActionMode.Callback() {
@Override
public boolean onCreateActionMode(android.support.v7.view.ActionMode mode, Menu menu) {
mode.setTitle("Support Action Mode Enabled");
return true;
}
@Override
public boolean onPrepareActionMode(android.support.v7.view.ActionMode mode, Menu menu) {
return false;
}
@Override
public boolean onActionItemClicked(android.support.v7.view.ActionMode mode, MenuItem item) {
return false;
}
@Override
public void onDestroyActionMode(android.support.v7.view.ActionMode mode) {
standard.setVisibility(View.VISIBLE);
support.setVisibility(View.VISIBLE);
}
};
@Override
public boolean dispatchKeyEvent(KeyEvent event) {
if (event.getKeyCode() == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_UP) {
// handle your back button code here
Toast.makeText(this,"Back ACTION_UP",Toast.LENGTH_LONG).show();
return true; // consumes the back key event - ActionMode is not finished
}
if (event.getKeyCode() == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN) {
// handle your back button code here
Toast.makeText(this,"Back ACTION_DOWN",Toast.LENGTH_LONG).show();
return true; // consumes the back key event - ActionMode is not finished
}
return super.dispatchKeyEvent(event);
}
}
布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/colorPrimary"
android:minHeight="?android:attr/actionBarSize"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"/>
<Button
android:id="@+id/standard"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="30dp"
android:text="Standard"/>
<Button
android:id="@+id/support"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="30dp"
android:text="Support"/>
</LinearLayout>
答案 0 :(得分:0)
我通过使用支持库中的工具栏来模拟ActionMode栏来解决这个问题。
答案 1 :(得分:-1)
我遇到了这个问题,并通过将AppCompatActivity
更改为Activity
来解决了这个问题。我不知道为什么,但是在AppCompatActivity
中,当actionMode工作时,它不会读取KeyEvent.ACTION_UP
。但是在Activity
中确实如此。