package com.android.settings;import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.content.res.Configuration;
import android.os.Bundle;
import android.preference.CheckBoxPreference;
import android.preference.Preference;
import android.preference.PreferenceActivity;
import android.preference.PreferenceScreen;
import android.provider.Settings;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodInfo;
import android.view.inputmethod.InputMethodManager;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;/***
 * @author 付欢欢
 */
public class LanguageSettings extends Activity {
    
    private static final String KEY_PHONE_LANGUAGE = "phone_language";
    private boolean mHaveHardKeyboard;    private List<InputMethodInfo> mInputMethodProperties;
    private List<CheckBoxPreference> mCheckboxes;
    private Preference mLanguagePref;    final TextUtils.SimpleStringSplitter mStringColonSplitter= new TextUtils.SimpleStringSplitter(':');
    
    private String mLastInputMethodId;
    private String mLastTickedInputMethodId;
    
    private Button mButton;
    private ListView mList;
    List<Map<Object,String>> listMap=new ArrayList<Map<Object,String>>();
    List<Map<Object,String>> mListMapPackage=new ArrayList<Map<Object,String>>();
    final HashSet<String> enabled = new HashSet<String>();
    
    static public String getInputMethodIdFromKey(String key) {
        return key;
    }    @Override
    protected void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        setContentView(R.layout.language_settings);
//        addPreferencesFromResource(R.xml.language_settings);
//        if (getAssets().getLocales().length == 1) {
//            getPreferenceScreen().
//                removePreference(findPreference("language_category"));
//        } else {
//            mLanguagePref = findPreference(KEY_PHONE_LANGUAGE);
//        }        Configuration config = getResources().getConfiguration();
        if (config.keyboard != Configuration.KEYBOARD_QWERTY) {
//            getPreferenceScreen().removePreference(
//                    getPreferenceScreen().findPreference("hardkeyboard_category"));
        } else {
            mHaveHardKeyboard = true;
        }
        
        mCheckboxes = new ArrayList<CheckBoxPreference>();
        
        // 设置语言
        mButton=(Button)findViewById(R.id.mButton);
        mButton.setOnClickListener(new Button.OnClickListener(){
         public void onClick(View v){
         Intent intent=new Intent();
         intent.setClass(LanguageSettings.this,LocalePicker.class);
         startActivity(intent);
         }
        });
        
        mList=(ListView)findViewById(R.id.mList);
        onCreateIMM();
        DataSet dataSet=new DataSet(this, listMap);
        mList.setAdapter(dataSet);
        mList.setOnItemClickListener(new onItemOnclick());
    }  private boolean isSystemIme(InputMethodInfo property) {
        return (property.getServiceInfo().applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) != 0;
    }

解决方案 »

  1.   

     class DataSet extends BaseAdapter{
         LayoutInflater layoutInflater;
         String inflater = Context.LAYOUT_INFLATER_SERVICE;
         Context context;
         List<Map<Object,String>> listMap=new ArrayList<Map<Object,String>>();
         List<Map<Object,Boolean>> mListMapObject=new ArrayList<Map<Object,Boolean>>();
         Map map=null;
         public DataSet(Context context,List<Map<Object,String>> listMap) {
         this.context = context;
         this.listMap=listMap;
         layoutInflater = (LayoutInflater) context.getSystemService(inflater);
            String enabledStr = Settings.Secure.getString(getContentResolver(),Settings.Secure.ENABLED_INPUT_METHODS);
            if (enabledStr != null) {
                final TextUtils.SimpleStringSplitter splitter = mStringColonSplitter;
                splitter.setString(enabledStr);
                while (splitter.hasNext()) {
                    enabled.add(splitter.next());
                }
            }         for (int i = 0; i < listMap.size(); ++i) {
             map=new HashMap();
             String id=listMap.get(i).get("value");
                map.put("selected", enabled.contains(id));
                mListMapObject.add(map);
            }
    }
        
    public int getCount() {
    return listMap.size();
    } public Object getItem(int position) {
    return position;
    } public long getItemId(int position) {
    return position;
    } public View getView(final int position, View convertView, ViewGroup parent) {
     final ViewHolder viewHolder;
    if (convertView == null) {
    viewHolder= new ViewHolder();
    convertView = layoutInflater.inflate(R.layout.language_settings_data,null); viewHolder.mText = (TextView) convertView.findViewById(R.id.mText);
    viewHolder.mCheckBox = (ImageView) convertView.findViewById(R.id.mCheckBox);
    convertView.setTag(viewHolder);
    } else {
    viewHolder = (ViewHolder) convertView.getTag();
    } viewHolder.mText.setText(listMap.get(position).get("name"));
    if(mListMapObject.get(position).get("selected")){
    viewHolder.mCheckBox.setImageResource(R.drawable.checkbox_sel);
    }else{
    viewHolder.mCheckBox.setImageResource(R.drawable.checkbox_nor);
    } /*if(position%2==0 || position==0){
    viewHolder.mCheckBox.setVisibility(View.VISIBLE);
    }else {
    viewHolder.mCheckBox.setVisibility(View.GONE);
    }*/ viewHolder.mCheckBox.setOnClickListener(new ImageView.OnClickListener() {
    public void  onClick(View v) {
    System.out.println("onclick-------------->");
     final String id=listMap.get(position).get("value");
     System.out.println("id-------------->"+id);
     
                    InputMethodInfo selImi = null;
                    final int N = mInputMethodProperties.size();
                    for (int i=0; i<N; i++) {
                        InputMethodInfo imi = mInputMethodProperties.get(i);
                        if (id.equals(imi.getId())){
                            selImi = imi;
                            if (isSystemIme(imi)) {
                             System.out.println("jjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjj--->"+id);
                                mLastTickedInputMethodId = id;
                                //return super.onPreferenceTreeClick(preferenceScreen, preference);
                            }
                        }
                    }
    //                 chkPref.setChecked(false);
                    if (selImi == null) {
    //                     return super.onPreferenceTreeClick(preferenceScreen, preference);
                    }
                    System.out.println("mListMapObject.get(position)======>"+mListMapObject.get(position).get("selected"));
                    if(mListMapObject.get(position).get("selected")==false){
                     AlertDialog d = (new AlertDialog.Builder(LanguageSettings.this))
                            .setTitle(android.R.string.dialog_alert_title)
                            .setIcon(android.R.drawable.ic_dialog_alert)
                            .setMessage(getString(R.string.ime_security_warning, selImi.getServiceInfo().applicationInfo.loadLabel(getPackageManager())))
                            .setCancelable(true)
                            .setPositiveButton(android.R.string.ok,
                                    new DialogInterface.OnClickListener() {
                                        public void onClick(DialogInterface dialog, int which) {
    //                                         chkPref.setChecked(true);
                                         System.out.println("gou1----11------------>"+id);
                                         mLastTickedInputMethodId=id;
                                         viewHolder.mCheckBox.setImageResource(R.drawable.checkbox_sel);
                                        
                                         if(mLastTickedInputMethodId!=null || !"".equals(mLastTickedInputMethodId)){
                                         Settings.Secure.putString(getContentResolver(),
                                                    Settings.Secure.DEFAULT_INPUT_METHOD,
                                                    mLastTickedInputMethodId);
                                         }
                                        }
                            })
                            .setNegativeButton(android.R.string.cancel,
                                    new DialogInterface.OnClickListener() {
                                        public void onClick(DialogInterface dialog, int which) {
                                         System.out.println("Scanner----->");
                                         viewHolder.mCheckBox.setImageResource(R.drawable.checkbox_nor);
                                        }
                            }).create();
                     d.show();
                    }
                    
                    System.out.println("---------id-->"+id+"-------->"+mLastTickedInputMethodId);
                    if (id.equals(mLastTickedInputMethodId)) {
                 System.out.println("clear---------------->");
                    mLastTickedInputMethodId = null;
                    viewHolder.mCheckBox.setImageResource(R.drawable.checkbox_nor);
                    return;
                }
    }
    });
    return convertView;
    }

    class ViewHolder {
    TextView mText;
    ImageView mCheckBox;
    }
        }
      

  2.   

    private void onCreateIMM() {
         Map map = null;
         String saveValue="";
            InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
            mInputMethodProperties = imm.getInputMethodList();
            System.out.println("How many data article -------------------->"+mInputMethodProperties.size());
            
            mLastInputMethodId = Settings.Secure.getString(getContentResolver(),Settings.Secure.DEFAULT_INPUT_METHOD);
            
    //      PreferenceGroup textCategory = (PreferenceGroup) findPreference("text_category");
            
            int N = (mInputMethodProperties == null ? 0 : mInputMethodProperties.size());
            for (int i = 0; i < N; ++i) {
                InputMethodInfo property = mInputMethodProperties.get(i);
                String prefKey = property.getId();            CharSequence label = property.loadLabel(getPackageManager());
                System.out.println("进来就获取----------->"+label.toString()+"--->"+prefKey);
                
                map=new HashMap();
                map.put("name", label.toString());
                map.put("value", prefKey);
                listMap.add(map);
                
               /* if(saveValue=="" || saveValue!=label.toString()){
                 map=new HashMap();
                    map.put("name", label.toString()+ " " +getResources().getString(R.string.band_mode_set));
                    map.put("value", prefKey);
                    listMap.add(map);
                }*/            System.out.println("--------------------<>>>"+enabled.contains(prefKey));
                
                boolean systemIME = isSystemIme(property);
                // Add a check box.
                // Don't show the toggle if it's the only keyboard in the system, or it's a system IME.
                if (mHaveHardKeyboard || (N > 1 && !systemIME)) {
                    CheckBoxPreference chkbxPref = new CheckBoxPreference(this);
                    chkbxPref.setKey(prefKey);
                    chkbxPref.setTitle(label);
    //              System.out.println("add---------------->"+prefKey+"-------------->"+label);
                    mCheckboxes.add(chkbxPref);
                }            // If setting activity is available, add a setting screen entry.
                if (null != property.getSettingsActivity()) {
                    PreferenceScreen prefScreen = new PreferenceScreen(this, null);
                    String settingsActivity = property.getSettingsActivity();
                    if (settingsActivity.lastIndexOf("/") < 0) {
                        settingsActivity = property.getPackageName() + "/" + settingsActivity;
                    }
                    prefScreen.setKey(settingsActivity);
                    prefScreen.setTitle(label);                map=new HashMap();
                    map.put("name", settingsActivity);
                    map.put("value", label);
                    mListMapPackage.add(map);              /*  if(saveValue!="" || saveValue!=label.toString()){
                     map=new HashMap();
                        map.put("name", settingsActivity);
                        map.put("value", label);
                        mListMapPackage.add(map);
                    }*/                saveValue=label.toString();                System.out.println("打印------------->"+settingsActivity+"--->"+label);
                    if (N == 1) {
                        prefScreen.setSummary(getString(R.string.onscreen_keyboard_settings_summary));
                    } else {
                        CharSequence settingsLabel = getResources().getString(
                                R.string.input_methods_settings_label_format, label);
                        prefScreen.setSummary(settingsLabel);
                    }
                }
            }
        }    @Override
        protected void onResume() {
            super.onResume();        final HashSet<String> enabled = new HashSet<String>();
            String enabledStr = Settings.Secure.getString(getContentResolver(),
                    Settings.Secure.ENABLED_INPUT_METHODS);
            if (enabledStr != null) {
                final TextUtils.SimpleStringSplitter splitter = mStringColonSplitter;
                splitter.setString(enabledStr);
                while (splitter.hasNext()) {
                    enabled.add(splitter.next());
                }
            }
            System.out.println(" enabled.add(splitter.next());-------->"+enabled.size());
            
            // Update the statuses of the Check Boxes.
            int N = mInputMethodProperties.size();
            for (int i = 0; i < N; ++i) {
                final String id = mInputMethodProperties.get(i).getId();
    //            CheckBoxPreference pref = (CheckBoxPreference) findPreference(mInputMethodProperties
    //                    .get(i).getId());
    //            if (pref != null) {
    //                pref.setChecked(enabled.contains(id));
    //            }
            }
            mLastTickedInputMethodId = null;        if (mLanguagePref != null) {
                Configuration conf = getResources().getConfiguration();
                String locale = conf.locale.getDisplayName(conf.locale);
                if (locale != null && locale.length() > 1) {
                    locale = Character.toUpperCase(locale.charAt(0)) + locale.substring(1);
                    mLanguagePref.setSummary(locale);
                }
            }
        }    @Override
        protected void onPause() {
            super.onPause();        StringBuilder builder = new StringBuilder(256);
            StringBuilder disabledSysImes = new StringBuilder(256);        int firstEnabled = -1;
            int N = mInputMethodProperties.size();
            System.out.println("nnnnnn有多少个---------------->"+N);
            for (int i = 0; i < N; ++i) {
                final InputMethodInfo property = mInputMethodProperties.get(i);
                System.out.println("里面的信息是------------->"+property.getPackageName()+"-->"+property.getServiceName()+"--->"+property.getComponent());
                final String id = property.getId();
                System.out.println("勾选的是------------------->"+id);
                //CheckBoxPreference pref = (CheckBoxPreference) findPreference(id);
                boolean hasIt = id.equals(mLastInputMethodId);
                boolean systemIme = isSystemIme(property); 
    //            if (((N == 1 || systemIme) && !mHaveHardKeyboard) || (pref != null && pref.isChecked())) {
                if (((N == 1 || systemIme) && !mHaveHardKeyboard)) {
                    if (builder.length() > 0) builder.append(':');
                    builder.append(id);
                    if (firstEnabled < 0) {
                        firstEnabled = i;
                    }
                } 
                else if (hasIt) {
                    mLastInputMethodId = mLastTickedInputMethodId;
                }
               // If it's a disabled system ime, add it to the disabled list so that it
               // doesn't get enabled automatically on any changes to the package list
                
              /*  if (pref != null && !pref.isChecked() && systemIme && mHaveHardKeyboard) {
                if ( systemIme && mHaveHardKeyboard) {
                    if (disabledSysImes.length() > 0) disabledSysImes.append(":");
                    disabledSysImes.append(id);
                }
            }*/
                
            if (systemIme && mHaveHardKeyboard) {
                if (disabledSysImes.length() > 0) disabledSysImes.append(":");
                disabledSysImes.append(id);
            }
            }
            
            if (null == mLastInputMethodId || "".equals(mLastInputMethodId)) {
             System.out.println("mLastInputMethodId 1111111111111====>"+firstEnabled);
                if (firstEnabled >= 0) {
                    mLastInputMethodId = mInputMethodProperties.get(firstEnabled).getId();
                } else {
                    mLastInputMethodId = null;
                }
            }
            
            System.out.println("Setting over ------------->"+builder.toString()+"--->"+disabledSysImes.toString()+"---->"+mLastInputMethodId);        Settings.Secure.putString(getContentResolver(),
                Settings.Secure.ENABLED_INPUT_METHODS, builder.toString());
            Settings.Secure.putString(getContentResolver(),
                    Settings.Secure.DISABLED_SYSTEM_INPUT_METHODS, disabledSysImes.toString());
            Settings.Secure.putString(getContentResolver(),
                Settings.Secure.DEFAULT_INPUT_METHOD,
                mLastInputMethodId != null ? mLastInputMethodId : "");
        }
      

  3.   

    // 项点击事件
        class onItemOnclick implements OnItemClickListener{
         public void onItemClick(AdapterView<?> adaterView, View view, int position,long id) {
         // System.out.println("点击了那一个------------->"+listMap.get(position).get("name"));
         /*if(position%2==0 || position==0){
        
         }else{*/
         // PreferenceScreen pref = (PreferenceScreen) preference;
                     String activityName =mListMapPackage.get(position).get("name");
                     System.out.println("activityName----->"+activityName);
                     String packageName = activityName.substring(0, activityName
                             .lastIndexOf("."));
                     int slash = activityName.indexOf("/");
                     if (slash > 0) {
                         packageName = activityName.substring(0, slash);
                         activityName = activityName.substring(slash + 1);
                     }
                     System.out.println("-activityName------->"+packageName+"------------>"+activityName+"--------->"+slash);
                     if (activityName.length() > 0) {
                         Intent i = new Intent(Intent.ACTION_MAIN);
                         i.setClassName(packageName, activityName);
                         startActivity(i);
                     }
         /* }*/
         }
        }}
    //上面是我改过的整个代码,可以对比一下设置里面的源码!