长按输入框跳出select all,cut,input method等菜单,点击input method跳出所有得到的输入法列表

TextView.java

private static final int ID_SWITCH_INPUT_METHOD = android.R.id.switchInputMethod;

 

protected void onCreateContextMenu (ContextMenu menu) {
        super.onCreateContextMenu(menu);

......

if (canCut()) {
            int name;
            if (selection) {
                name = com.android.internal.R.string.cut;
            } else {
                name = com.android.internal.R.string.cutAll;
            }

            menu.add(0, ID_CUT, 0, name).
                setOnMenuItemClickListener(handler).
                setAlphabeticShortcut('x');
            added = true;
        }

.......

//判断输入法是否被激活,如果激活的话,将 inputMethod menuitem添加到菜单里

 if (isInputMethodTarget ()) {
            menu.add(1, ID_SWITCH_INPUT_METHOD, 0, com.android.internal.R.string.inputMethod).
                    setOnMenuItemClickListener(handler);
            added = true;
        }

}

 

*************************************************************************************************************************

isInputMethodTarget()原型如下:

/**
     * Returns whether this text view is a current input method target.  The
     * default implementation just checks with {@link InputMethodManager}.
     */
    public boolean isInputMethodTarget () {
        InputMethodManager imm = InputMethodManager.peekInstance();
        return imm != null && imm.isActive(this);
    }

 

*************************************************************************************************************************

private class MenuHandler implements MenuItem.OnMenuItemClickListener {//menuitem点击的监听事件
        public boolean onMenuItemClick (MenuItem item) {
            return onTextContextMenuItem (item.getItemId());
        }
    }

 

*************************************************************************************************************************

/**
     * Called when a context menu option for the text view is selected.  Currently
     * this will be one of: {@link android.R.id#selectAll},
     * {@link android.R.id#startSelectingText}, {@link android.R.id#stopSelectingText},
     * {@link android.R.id#cut}, {@link android.R.id#copy},
     * {@link android.R.id#paste}, {@link android.R.id#copyUrl},
     * or {@link android.R.id#switchInputMethod}.
     */
    public boolean onTextContextMenuItem (int id) {
        int selStart = getSelectionStart();
        int selEnd = getSelectionEnd();

。。。。。。。。

 switch (id) {
            case ID_SELECT_ALL:
                Selection.setSelection((Spannable) mText, 0,
                        mText.length());
                return true;

。。。。。。。。。

 case ID_SWITCH_INPUT_METHOD:
                InputMethodManager imm = InputMethodManager.peekInstance();
                if (imm != null) {
                    imm.showInputMethodPicker();//显示所有可获取的输入法
                }
                return true;

}

*************************************************************************************************************************

p { margin-bottom: 0.21cm; }

InputMethodManager.java( 实现了 showInputMethodPicker ()函数 )


final IInputMethodManager mService;

public void showInputMethodPicker() {
        synchronized (mH) {
            try {
                mService .showInputMethodPickerFromClient (mClient);
            } catch (RemoteException e) {
                Log.w(TAG, "IME died: " + mCurId, e);
            }
        }
    }

p { margin-bottom: 0.21cm; }

InputMethodManager.java( 实现了 showInputMethodPicker ()函数 ) 实现了 InputMethodManager 类。此类调用 IInputMethodManager.aidl 接口功能,而 IInputMethodManager.aidl 接口功能由 InputMethodManagerService.java 实现 ( 实现了 IInputMethodManager.aidl 接口功能 函数 showInputMethodPickerFromClient () ) ,并运行在不同于客户端进程的 server 进程中。

 

 

*************************************************************************************************************************

InputMethodManagerService.java

 

 

 

 static final int MSG_SHOW_IM_PICKER = 1;

public void showInputMethodPickerFromClient (IInputMethodClient client) {
            .....................
            mHandler.sendEmptyMessage(MSG_SHOW_IM_PICKER );
    }

 

.......................................................................................................................................................

处理MSG_SHOW_IM_PICKER 消息

public boolean handleMessage(Message msg) {
        HandlerCaller.SomeArgs args;
        switch (msg.what) {
            case MSG_SHOW_IM_PICKER:
                showInputMethodMenu();

                return true;

}

*************************************************************************************************************************

InputMethodManagerService.java

 

 void showInputMethodMenu() {
        if (DEBUG) Slog.v(TAG, "Show switching menu");

        final Context context = mContext;


// Return PackageManager instance to find global package information.

       final PackageManager pm = context.getPackageManager ();p { margin-bottom: 0.21cm; }

 




        String lastInputMethodId = Settings.Secure.getString(context
                .getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD);//得到默认输入法字符串
        if (DEBUG) Slog.v(TAG, "Current IME: " + lastInputMethodId);

        final List<InputMethodInfo> immis = getEnabledInputMethodList();//获取已启用输入法列表

        if (immis == null) {
            return;
        }
       
        synchronized (mMethodMap) {
            hideInputMethodMenuLocked();

            int N = immis.size();//得到输入法列表的大小
   
            mItems = new CharSequence[N];
            mIms = new InputMethodInfo[N];
   
            int j = 0;
            for (int i = 0; i < N; ++i) {
                InputMethodInfo property = immis.get(i);
                if (property == null) {
                    continue;
                }
                mItems[j] = property.loadLabel(pm);
                mIms[j] = property;
                j++;
            }
   
            int checkedItem = 0;
            for (int i = 0; i < N; ++i) {
                if (mIms[i].getId().equals(lastInputMethodId)) {
                    checkedItem = i;
                    break;
                }
            }
   
            AlertDialog.OnClickListener adocl = new AlertDialog.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                    hideInputMethodMenu();
                }
            };
   
            TypedArray a = context.obtainStyledAttributes(null,
                    com.android.internal.R.styleable.DialogPreference,
                    com.android.internal.R.attr.alertDialogStyle, 0);
            mDialogBuilder = new AlertDialog.Builder(context)
                    .setTitle(com.android.internal.R.string.select_input_method)
                    .setOnCancelListener(new OnCancelListener() {
                        public void onCancel(DialogInterface dialog) {
                            hideInputMethodMenu();
                        }
                    })
                    .setIcon(a.getDrawable(
                            com.android.internal.R.styleable.DialogPreference_dialogTitle));
            a.recycle();
   
            mDialogBuilder.setSingleChoiceItems(mItems, checkedItem,
                    new AlertDialog.OnClickListener() {
                        public void onClick(DialogInterface dialog, int which) {
                            synchronized (mMethodMap) {
                                if (mIms == null || mIms.length <= which) {
                                    return;
                                }
                                InputMethodInfo im = mIms[which];
                                hideInputMethodMenu();
                                if (im != null) {
                                    setInputMethodLocked(im.getId());
                                }
                            }
                        }
                    });

            mSwitchingDialog = mDialogBuilder.create();
            mSwitchingDialog.getWindow().setType(
                    WindowManager.LayoutParams.TYPE_INPUT_METHOD_DIALOG);
            mSwitchingDialog.show();
        }
    }

 

 

 

删除之类操作需要全选功能,方便选择 public class MainActivity extends Activity { private ListView lv; private MyAdapter mAdapter; private ArrayList list; private Button bt_selectall; // private Button bt_cancel; // private Button bt_deselectall; private int checkNum; // 记录选中的条目数量 private TextView tv_show;// 用于显示选中的条目数量 /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); /* 实例化各个控件 */ lv = (ListView) findViewById(R.id.lv); bt_selectall = (Button) findViewById(R.id.bt_selectall); // bt_cancel = (Button) findViewById(R.id.bt_cancelselectall); // bt_deselectall = (Button) findViewById(R.id.bt_deselectall); tv_show = (TextView) findViewById(R.id.tv); list = new ArrayList(); // 为Adapter准备数据 initDate(); // 实例化自定义的MyAdapter mAdapter = new MyAdapter(list, this); // 绑定Adapter lv.setAdapter(mAdapter); // 全选按钮的回调接口 bt_selectall.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // 遍历list的度,将MyAdapter中的map值全部设为true for (int i = 0; i < list.size(); i++) { MyAdapter.getIsSelected().put(i, true); } // 数量设为list的度 checkNum = list.size(); // 刷新listview和TextView的显示 dataChanged(); } }); // 反选按钮的回调接口 // bt_cancel.setOnClickListener(new OnClickListener() { // @Override // public void onClick(View v) { // // 遍历list的度,将已选的设为未选,未选的设为已选 // for (int i = 0; i < list.siz
要实现输入框时,遮罩页面但不遮罩输入框,并在输入框下面显示菜单,您可以使用 PopupWindow 来实现。以下是一个示例代码: 首先,在您的布局文件中添加一个包含输入框菜单的布局: ```xml <RelativeLayout android:layout_width="match_parent" android:layout_height="match_parent"> <EditText android:id="@+id/editText" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Long press here" /> <LinearLayout android:id="@+id/menuLayout" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" android:background="@android:color/white" android:visibility="gone"> <!-- 在这里添加菜单项 --> </LinearLayout> </RelativeLayout> ``` 接下来,在您的活动或片段中添加以下代码: ```java // 获取输入框菜单布局的引用 EditText editText = findViewById(R.id.editText); LinearLayout menuLayout = findViewById(R.id.menuLayout); // 设置输入框按监听器 editText.setOnLongClickListener(new View.OnLongClickListener() { @Override public boolean onLongClick(View v) { // 创建并显示菜单PopupWindow PopupWindow popupWindow = new PopupWindow(menuLayout, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT, true); popupWindow.setOutsideTouchable(true); popupWindow.showAsDropDown(editText); // 在输入框下方显示菜单 return true; } }); ``` 在上述代码中,我们创建了一个 PopupWindow 对象,并将菜单布局设置为其内容。然后,我们通过调用 `showAsDropDown()` 方法将菜单显示在输入框的下方。此外,我们还设置了一些属性,使菜单可以在点击菜单以外的区域时自动隐藏。 请注意,您需要根据实际需求自定义菜单布局,并在其中添加相应的菜单项。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值