在 ActionMode 处于活动状态时阻止后退按钮
Prevent Back button whilst ActionMode is active
下面链接的问题中的过程似乎不适用于标准或支持 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>
我通过使用支持库中的工具栏模拟 ActionMode 栏解决了这个问题。
我遇到了这个问题,我通过将 AppCompatActivity
更改为 Activity
解决了这个问题。我不知道为什么,但在 AppCompatActivity
中,当 actionMode 工作时它不会读取 KeyEvent.ACTION_UP
。但在 Activity
中确实如此。
下面链接的问题中的过程似乎不适用于标准或支持 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>
我通过使用支持库中的工具栏模拟 ActionMode 栏解决了这个问题。
我遇到了这个问题,我通过将 AppCompatActivity
更改为 Activity
解决了这个问题。我不知道为什么,但在 AppCompatActivity
中,当 actionMode 工作时它不会读取 KeyEvent.ACTION_UP
。但在 Activity
中确实如此。