与registerEditText的getSharedPreferences

时间:2014-08-04 08:35:51

标签: android

我正在为我的应用程序开发自定义键盘。我从http://www.fampennings.nl/maarten/android/09keyboard/index.htm

获得了我正在使用的参考代码

所有学分归他所有。

如果我们使用正常的初始化,MainActivity不会单独注册EditText,我们总是这样做:

editText1 = (EditText) findViewById(R.id.editText1)[EXAMPLE]

而不是这样,它正在使用:

mCustomKeyboard= new CustomKeyboard(this, R.id.keyboardview, R.xml.hexkbd );

    sPrefs = getSharedPreferences("storeData", MODE_PRIVATE);
    Editor editor = sPrefs.edit();
    mCustomKeyboard.registerEditText(R.id.edittext0);
    editor.putString("TEST1",);
    mCustomKeyboard.registerEditText(R.id.edittext3);
    mCustomKeyboard.registerEditText(R.id.edittext4);`

多个EditText的注册在另一个Activity中完成:

public void registerEditText(int resid) {
        // Find the EditText 'resid'
        EditText edittext= (EditText)mHostActivity.findViewById(resid);
        // Make the custom keyboard appear
        edittext.setOnFocusChangeListener(new OnFocusChangeListener() {
            @Override public void onFocusChange(View v, boolean hasFocus) {
                if( hasFocus ) showCustomKeyboard(v); else hideCustomKeyboard();
            }
        });

        edittext.setOnClickListener(new OnClickListener() {
            // NOTE By setting the on click listener, we can show the custom keyboard again, by tapping on an edit box that already had focus (but that had the keyboard hidden).
            @Override public void onClick(View v) {
                showCustomKeyboard(v);
            }
        });

        edittext.setOnTouchListener(new OnTouchListener() {
            @Override public boolean onTouch(View v, MotionEvent event) {
                EditText edittext = (EditText) v;
                int inType = edittext.getInputType();       // Backup the input type
                edittext.setInputType(InputType.TYPE_NULL); // Disable standard keyboard
                edittext.onTouchEvent(event);               // Call native handler
                edittext.setInputType(inType);              // Restore input type
                return true; // Consume touch event
            }
        });
        // Disable spell check (hex strings look like words to Android)
        edittext.setInputType(edittext.getInputType() | InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS);
    }

}

我是否将SharedPreferences放在我的MainActivity或其他实现EditText注册的类中?

解决方案:

edit1 = (EditText) findViewById(R.id.edittext0);

        saveButton = (Button) findViewById(R.id.button1);

        saveButton.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                sPrefs = getSharedPreferences("storeData", MODE_PRIVATE);
                Editor editor = sPrefs.edit();
                editor.putString("TEST1", edit1.getText().toString());
                editor.commit();
                Intent intent = new Intent(getApplicationContext(), testPref.class);
                startActivity(intent);

            }
        });



mCustomKeyboard= new CustomKeyboard(this, R.id.keyboardview, R.xml.hexkbd );

        sPrefs = getSharedPreferences("storeData", MODE_PRIVATE);
        Editor editor = sPrefs.edit();
        mCustomKeyboard.registerEditText(R.id.edittext0);
        editor.putString("TEST1", edit1.getText().toString());

1 个答案:

答案 0 :(得分:0)

现在查看这个简单的演示,尝试理解并在代码中使用它

activity_main.xml中的代码

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity" >


    <EditText
        android:id="@+id/ed1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"

        android:hint="Enter Text"
         />

    <Button
        android:id="@+id/btnsave"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Save"       
         />

    <Button
        android:id="@+id/btnget"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Get Saved Data"       
         />

    <TextView
        android:id="@+id/txt1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />


</LinearLayout>

活动类中的代码

public class MainActivity extends Activity implements OnClickListener{

    EditText editText;
    Button save, get;
    TextView print;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        editText = (EditText)findViewById(R.id.ed1);
        print = (TextView) findViewById(R.id.txt1);
        save = (Button) findViewById(R.id.btnsave);
        save.setOnClickListener(this);
        get = (Button) findViewById(R.id.btnget);
        get.setOnClickListener(this);
    }


    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        switch(v.getId()){
        case R.id.btnsave:
            SharedPreferences shpref = getSharedPreferences("save_value", MODE_PRIVATE);
            SharedPreferences.Editor editor = shpref.edit();
            editor.putString("item1", editText.getText().toString());
            editor.commit();
            break;
        case R.id.btnget:
            String value = getSharedPreferences("save_value", MODE_PRIVATE)
            .getString("item1", "hello");
            print.setText(value);
        }
    }

}