最近做盒子开发中,有需求要修改输入法的功能,查阅资料,终于解决了这个问题,现在把代码贴出来:

import android.app.Dialog;import android.content.Context;import android.content.pm.ApplicationInfo;import android.content.pm.PackageManager;import android.inputmethodservice.InputMethodService;import android.os.Bundle;import android.provider.Settings;import android.text.TextUtils;import android.util.Log;import android.view.View;import android.view.inputmethod.InputMethodInfo;import android.view.inputmethod.InputMethodManager;import android.widget.Button;import android.widget.RadioButton;import android.widget.RadioGroup;import android.widget.TextView;import com.xinrui.tvsetting.R;import com.xinrui.tvsetting.common.ResultCallbackListener;import java.util.HashMap;import java.util.List;import java.util.Map;public class InputMethodDialog extends Dialog implements View.OnClickListener{    private ResultCallbackListener callbackListener;    private RadioGroup inputmethod_type;    private RadioButton google_rdb,sogou_rdb;    private Context mContext;    private Button ok_btn,cancle_btn;    private String inputmethod="";    private List<InputMethodInfo> methodList;    private Map<String,String> inputmap = new HashMap<>();    private PackageManager pm;    private ApplicationInfo appinfo;    public InputMethodDialog(Context context, int theme) {        super(context, theme);        this.mContext = context;    }    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.inputmethod_layout);        initView();    }    public void setCallbackListener(ResultCallbackListener listener){        this.callbackListener = listener;    }    private void initView(){       inputmethod_type = (RadioGroup)findViewById(R.id.inputmethod_type);       google_rdb=(RadioButton)findViewById(R.id.google_type);       sogou_rdb=(RadioButton)findViewById(R.id.sougou_type);       ok_btn=(Button)findViewById(R.id.input_ok);       cancle_btn=(Button)findViewById(R.id.input_cancle);       ok_btn.setOnClickListener(this);       cancle_btn.setOnClickListener(this);       getInputMethodList();//        final InputMethodService service = new InputMethodService();       inputmethod_type.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {           @Override           public void onCheckedChanged(RadioGroup group, int checkedId) {               switch (checkedId){                   case R.id.google_type:                       inputmethod="谷歌拼音输入法";//                       service.switchInputMethod(inputmap.get(inputmethod));                       Settings.Secure.putString(mContext.getContentResolver(),                               Settings.Secure.DEFAULT_INPUT_METHOD,"com.android.inputmethod.pinyin/.PinyinIME");                       break;                   case R.id.sougou_type:                       inputmethod="搜狗输入法";                       Settings.Secure.putString(mContext.getContentResolver(),                               Settings.Secure.DEFAULT_INPUT_METHOD,"com.sohu.inputmethod.sogouoem/.SogouIME");                       break;               }           }       });       String curinputName = getcurinputname();       if(curinputName.equals("谷歌拼音输入法")){           inputmethod_type.check(R.id.google_type);       }else{           inputmethod_type.check(R.id.sougou_type);       }    }    private String getcurinputname() {        String package_name = getDefaultInputMethodPkgName(mContext);        pm = mContext.getPackageManager();        try {            appinfo = pm.getApplicationInfo(package_name, PackageManager.GET_META_DATA);        } catch (Exception e) {            e.printStackTrace();        }        return pm.getApplicationLabel(appinfo).toString();    }    //获取默认输入法包名:    private String getDefaultInputMethodPkgName(Context context) {        String mDefaultInputMethodPkg = null;        String mDefaultInputMethodCls = Settings.Secure.getString(                context.getContentResolver(),                Settings.Secure.DEFAULT_INPUT_METHOD);        //输入法类名信息        //Log.e(TAG, "mDefaultInputMethodCls=" + mDefaultInputMethodCls);        if (!TextUtils.isEmpty(mDefaultInputMethodCls)) {            //输入法包名            mDefaultInputMethodPkg = mDefaultInputMethodCls.split("/")[0];            //Log.e(TAG, "mDefaultInputMethodPkg=" + mDefaultInputMethodPkg);        }        return mDefaultInputMethodPkg;    }    public void getInputMethodList(){        InputMethodManager imm = (InputMethodManager)mContext.getSystemService(Context.INPUT_METHOD_SERVICE);        methodList = imm.getInputMethodList();        for(int i=0;i<methodList.size();i++) {            InputMethodInfo mi = methodList.get(i);            String name = mi.loadLabel(mContext.getPackageManager()).toString();            String id = mi.getId();//            Log.e("MainActivity","id:"+id+"--name:"+name);            if(id.equals("com.android.inputmethod.pinyin/.PinyinIME")){                if(i==0) google_rdb.setChecked(true);                google_rdb.setText(name);            } else if(id.equals("com.sohu.inputmethod.sogouoem/.SogouIME")){                if(i==0) sogou_rdb.setChecked(true);                sogou_rdb.setText(name);            }            inputmap.put(name,id);        }    }    @Override    public void onClick(View v) {        switch (v.getId()){            case R.id.input_ok:                if(callbackListener!=null&&!TextUtils.isEmpty(inputmethod)) {                    callbackListener.getValue(inputmethod);                }                dismiss();                break;            case R.id.input_cancle:                dismiss();                break;        }    }}

通过Settings.Secure.putString(mContext.getContentResolver(),
Settings.Secure.DEFAULT_INPUT_METHOD,“com.sohu.inputmethod.sogouoem/.SogouIME”);//第三个参数就是 输入法id了 不过需要系统权限
这样就可以切换输入法了

更多相关文章

  1. android 获取手机通讯录以及 6.0 授权
  2. Android(安卓)EditText 不弹出输入法总结
  3. Android(安卓)判断网络是否可用 & 获取IP地址 & 获取以太网口MAC
  4. android root权限相关bin程序rageagainsttheca
  5. Android(安卓)判断Root的方法
  6. android 权限配置和测试环境配置
  7. Android(安卓)安全和权限
  8. Android仿微信文章悬浮窗效果
  9. Android中模拟点击软件的实现原理探究

随机推荐

  1. Android 运行出错
  2. 【Android】Android Clean Architecture
  3. Android OpenGL例子学习
  4. Android使用百度地图SDK获得当前设备位置
  5. Android逆向分析常用网站
  6. Linux系统Android SDK 下载版本,报错Warni
  7. android(2.0以后版本) 中读取联系人和通
  8. 更新SDK后eclipse报错This Android SDK r
  9. Android中常用的Intent.Action整理
  10. android 常用JVM配置参数