长按文本框或者webview界面,会弹出复制粘贴剪切等对话框,但是目前好多厂商都是自己写的控件,先说一下对系统的对话框进行监听的情况。
这里是为了监听对话框来对自定义view进行高度的处理,对华为三星等手机做个适配,自定义了一个view,在onLayout方法中根据自定义的view在屏幕中的位置变化来进行判断复制粘贴对话框的弹出和隐藏。代码:
@Override
protected void onLayout(boolean changed, int left, int top, int right,
int bottom) {
super.onLayout(changed, left, top, right, bottom);
int[] a = new int[2];
getLocationInWindow(a);
int currentY = a[1];
if (android.os.Build.VERSION.SDK_INT > 18) {
int height = getHeight();
// TODO 大于0可能会有问题
if (currentY > 0 && height != 0) {
post(new Runnable() {
@Override
public void run() {
ViewGroup.LayoutParams params = getLayoutParams();
params.height = 0;
setLayoutParams(params);
}
});
} else if(currentY == 0 && height == 0){
post(new Runnable() {
@Override
public void run() {
ViewGroup.LayoutParams params = getLayoutParams();
params.height = AppEnv.STATUS_BAR_HEIGHT;
setLayoutParams(params);
}
});
}
}
}
public void onActionModeStarted(ActionMode mode) {
System.out.println("----------onActionModeStarted----------");
};
@Override
public void onActionModeFinished(ActionMode mode) {
System.out.println("----------onActionModeFinished----------");
super.onActionModeFinished(mode);
}
然后就是自定义的实现了,自己写了个小demo,以及文本框和webview自由选择复制这一块有点复杂,还未实现,贴一下自己的demo。
xml就是一个EditText,不贴了,
弹框的xml布局为:
下面是代码:
package soft.sxlc.com.showhidesoftkey;
import android.content.Context;
import android.content.ClipboardManager;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.text.Editable;
import android.text.TextUtils;
import android.util.TimeUtils;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.Toast;
import java.util.Collections;
public class MainActivity extends AppCompatActivity {
private EditText et;
private PopupWindow popupWindow;
private View view;
private TextView copy;
private TextView selectAll;
private TextView cut;
private TextView paste;
private TextView select;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
et = (EditText) findViewById(R.id.et);
initListener();
}
//TODO 初始化长按点击监听
private void initListener() {
et.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
if(!TextUtils.isEmpty(et.getText().toString())){
Toast.makeText(MainActivity.this, "long click", Toast.LENGTH_SHORT).show();
showPopWindow();
}
return true;
}
});
}
private void showPopWindow() {
if (popupWindow == null) {
LayoutInflater layoutInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = layoutInflater.inflate(R.layout.view_clipboard_popu, null);
final ClipboardManager clip = (ClipboardManager)getSystemService(Context.CLIPBOARD_SERVICE);
//TODO 设置点击监听实现相应的处理。
copy = (TextView) view.findViewById(R.id.copy);
selectAll = (TextView) view.findViewById(R.id.select_all);
cut = (TextView) view.findViewById(R.id.cut);
paste = (TextView) view.findViewById(R.id.paste);
select = (TextView) view.findViewById(R.id.select);
//TODO 复制点击事件
copy.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, "已复制到粘贴板", Toast.LENGTH_SHORT).show();
clip.setText(et.getText().toString());
popupWindow.dismiss();
}
});
//TODO 粘贴点击事件
paste.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, "粘贴好了", Toast.LENGTH_SHORT).show();
int index = et.getSelectionStart();
Editable editable = et.getText();
editable.insert(index, clip.getText().toString());
//et.setText(clip.getText());
popupWindow.dismiss();
}
});
//TODO 剪切点击事件
cut.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, "我剪", Toast.LENGTH_SHORT).show();
clip.setText(et.getText().toString());
et.setText("");
popupWindow.dismiss();
}
});
//TODO 全选点击事件
selectAll.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, "全部选中", Toast.LENGTH_SHORT).show();
et.selectAll();
}
});
//TODO 自由选择的点击事件
select.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, "选择", Toast.LENGTH_SHORT).show();
}
});
popupWindow = new PopupWindow(view, 500, 80);
}
//TODO 弹框的一些设置 位置 点击事件等
popupWindow.setFocusable(true);
popupWindow.setOutsideTouchable(true);
popupWindow.showAtLocation(et,0,20,300);
}
}