如何设置Edittext是否为空。共享偏好不会存储?

时间:2017-05-15 12:21:40

标签: android android-edittext sharedpreferences

我有一个简单的笔记活动...从那里用户插入文本,可以通过两个按钮保存或加载它们......但我遇到的问题是...... example

我不知道如何防止不保存空的edittext,因为仍然可以保存空框以覆盖保存的文本。当试图加载旧文本时,用户将丢失文本..如果您需要任何代码请告诉我..



    public static final String FILENAME="ShareDATA";
    public static final String KEYNAME="key";
    private SharedPreferences sharedpreference;
    EditText edittext;
    Button save,load;
    TextView tv3, texf;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_shared_preference);

        texf = (TextView) findViewById(R.id.textView3);
        texf.setTypeface(Typeface.createFromAsset(getAssets(), "rrabar.ttf"));

        tv3 = (TextView) findViewById(R.id.textferk);
        tv3.setTypeface(Typeface.createFromAsset(getAssets(), "rrabar.ttf"));


        edittext=(EditText)findViewById(R.id.EditText01);
        save=(Button)findViewById(R.id.Button01);
        load=(Button)findViewById(R.id.Button02);


        try{
            save.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {

                    sharedpreference=getSharedPreferences(FILENAME, MODE_PRIVATE);
                    Editor editor=sharedpreference.edit();
                    editor.putString(KEYNAME, edittext.getText().toString());
                    editor.apply();
                    edittext.setText("");
                }
            });
        }
        catch (Throwable e) {
            Toast.makeText(getApplicationContext(), e.toString(), Toast.LENGTH_LONG);
        }

        try{

            load.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    sharedpreference=getSharedPreferences(FILENAME, MODE_PRIVATE);
                    edittext.setText(sharedpreference.getString(KEYNAME, "نووسینی نیشانه‌كراو بوونی نییه‌."));

                }
            });
        }
        catch (Exception f) {
            Toast.makeText(getApplicationContext(), f.toString(), Toast.LENGTH_LONG).show();

        }

    }




非常感谢您的帮助

4 个答案:

答案 0 :(得分:0)

只需在存储数据之前添加支票

...
@Override
public void onClick(View v) {
    if (editText.getText().toString().trim().length() > 0) {
        // store data
    }
    else {
        // show error message or something
    }
}

答案 1 :(得分:0)

您可以将Button设置为xml中默认禁用的android:enabled=false。之后,在TextChangeListener上加EditText,如果文字长度大于零,则启用Button,例如

editText.addTextChangedListener(new TextWatcher() {

   @Override
   public void afterTextChanged(Editable s) {}

   @Override    
   public void beforeTextChanged(CharSequence s, int start,
     int count, int after) {
   }

   @Override    
   public void onTextChanged(CharSequence s, int start,
     int before, int count) {
      if(count > 0) // enable button else disable;
   }
  });

答案 2 :(得分:0)

只检查简单条件

        if (!editText.getText().toString().trim().equals("")) {
        // store data
    } else {
        // show error message or something
    }

答案 3 :(得分:0)

试试这个:

    save.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                if (edittext.getText().toString().trim().length() > 0) {

                    sharedpreference = getSharedPreferences(FILENAME, MODE_PRIVATE);
                    Editor editor = sharedpreference.edit();
                    editor.putString(KEYNAME, edittext.getText().toString());
                    editor.apply();
                } else {
                    Toast.makeText(getApplicationContext(), "Please enter something...", Toast.LENGTH_SHORT).show();
                }

                // Clear
                edittext.setText("");
            }
        });
  

String.trim()方法可以消除leadingtrailing个空格   串。返回string,其值为此字符串,并删除任何前导和尾随whitespace