【问题标题】:save values of edit text using shared preference on text view in android在android中的文本视图上使用共享首选项保存编辑文本的值
【发布时间】:2014-09-20 19:04:38
【问题描述】:

我正在设计一个使用共享偏好的应用程序。

我采用了 8 个编辑文本、一个文本视图和一个按钮。我正在使用共享首选项来存储这些编辑文本的值并将其保存在单个文本视图中,然后单击按钮将这些值发送到下一个屏幕。

当我回到第一个屏幕时,我得到了保存在编辑文本中的值。

但是...当我尝试编辑任何编辑文本时,文本视图中的文本会完全改变。

我希望文本视图中的值保持不变,除非它被编辑并且只有编辑的值在文本视图中被更改,其余保持不变。

这是第一个屏幕的代码-

    public static final String MyPREFERENCES = "MyPrefs";
    public static final String EventName = "eventname";
    public static final String Discount = "discount";
    public static final String ProductOffer = "productoffer";
    public static final String StartingTime = "startingtime";
    public static final String EndingTime = "endingtime";
    public static final String Address = "address";
    public static final String TimesRedeem = "timesredeem";
    public static final String ValidTill = "vslidtill";
    public static final String Output = "output";

    SharedPreferences sharedpreferences;

    eventName = (EditText) findViewById(R.id.event_name);
    discount = (EditText) findViewById(R.id.discount);
    productOffer = (EditText) findViewById(R.id.product_offer);
    address = (EditText) findViewById(R.id.address);
    output = (TextView) findViewById(R.id.textview4);
    nextoffer = (Button) findViewById(R.id.next);
    startingTime = (EditText) findViewById(R.id.start_time);
    endingTime = (EditText) findViewById(R.id.end_time);
    timesRedeem = (EditText) findViewById(R.id.no_of_time);
    etbirth = (EditText) findViewById(R.id.valid_till);

    sharedpreferences = getSharedPreferences(MyPREFERENCES,
            Context.MODE_PRIVATE);

    if (sharedpreferences.contains(EventName)) {
        eventName.setText(sharedpreferences.getString(EventName, ""));
    }
    if (sharedpreferences.contains(Discount)) {
        discount.setText(sharedpreferences.getString(Discount, ""));
    }
    if (sharedpreferences.contains(ProductOffer)) {
        productOffer.setText(sharedpreferences.getString(ProductOffer, ""));
    }
    if (sharedpreferences.contains(StartingTime)) {
        startingTime.setText(sharedpreferences.getString(StartingTime, ""));
    }
    if (sharedpreferences.contains(EndingTime)) {
        endingTime.setText(sharedpreferences.getString(EndingTime, ""));
    }
    if (sharedpreferences.contains(ValidTill)) {
        etbirth.setText(sharedpreferences.getString(ValidTill, ""));
    }
    if (sharedpreferences.contains(TimesRedeem)) {
        timesRedeem.setText(sharedpreferences.getString(TimesRedeem, ""));
    }
    if (sharedpreferences.contains(Address)) {
        address.setText(sharedpreferences.getString(Address, ""));
    }
    if (sharedpreferences.contains(Output)) {
        output.setText(sharedpreferences.getString(Output, ""));
    } else {
        name = "Birthday";
        dis = "10";
        po = "Pizza";
        validtill = "DD-MM-YYYY";
        starttime = "HH:MM";
        endtime = "HH:MM";
        add = "XYZ";

        output.setText(name + " with friends & family. Get " + dis
                + "% off on " + po + " till " + validtill + " from "
                + starttime + " to " + endtime + " at " + bus_name + ", "
                + add + ". T&C");
    }

    eventName.addTextChangedListener(new TextWatcher() {
        public void afterTextChanged(Editable s) {
        }

        public void beforeTextChanged(CharSequence s, int start, int count,
                int after) {
            dis = discount.getText().toString();
            po = productOffer.getText().toString();
            validtill = etbirth.getText().toString();
            starttime = startingTime.getText().toString();
            endtime = endingTime.getText().toString();
            timeredeem = timesRedeem.getText().toString();
            add = address.getText().toString();

        }

        public void onTextChanged(CharSequence s, int start, int before,
                int count) {
            name = eventName.getText().toString();

            if (name.length() == 0) {
                if(sharedpreferences.contains(Discount)){
                    sharedpreferences.getString(Discount, "");
                    output.setText(name + " with friends & family. Get " + dis
                            + "% off on " + po + " till " + validtill
                            + " from " + starttime + " to " + endtime + " at "
                            + bus_name + ", " + add + ". T&C");
                }
                name = "Birthday";
                output.setText(name + " with friends & family. Get " + dis
                        + "% off on " + po + " till " + validtill
                        + " from " + starttime + " to " + endtime + " at "
                        + bus_name + ", " + add + ". T&C");
            }  else {
                dis = "10";
                po = "Pizza";
                validtill = "DD-MM-YYYY";
                starttime = "HH:MM";
                endtime = "HH:MM";
                add = "XYZ";
                output.setText(name + " with friends & family. Get " + dis
                        + "% off on " + po + " till " + validtill
                        + " from " + starttime + " to " + endtime + " at "
                        + bus_name + ", " + add + ". T&C");
            }
        }
    });

    discount.addTextChangedListener(new TextWatcher() {
        public void afterTextChanged(Editable s) {
        }

        public void beforeTextChanged(CharSequence s, int start, int count,
                int after) {
            po = productOffer.getText().toString();
            validtill = etbirth.getText().toString();
            starttime = startingTime.getText().toString();
            endtime = endingTime.getText().toString();
            timeredeem = timesRedeem.getText().toString();
            add = address.getText().toString();
        }

        public void onTextChanged(CharSequence s, int start, int before,
                int count) {
            dis = discount.getText().toString();
            name = eventName.getText().toString();

            if (dis.length() == 0) {
                dis = "10";
                output.setText(name + " with friends & family. Get " + dis
                        + "% off on " + po + " till " + validtill
                        + " from " + starttime + " to " + endtime + " at "
                        + bus_name + ", " + add + ". T&C");
            } else {
                po = "Pizza";
                validtill = "DD-MM-YYYY";
                starttime = "HH:MM";
                endtime = "HH:MM";
                add = "XYZ";
                output.setText(name + " with friends & family. Get " + dis
                        + "% off on " + po + " till " + validtill
                        + " from " + starttime + " to " + endtime + " at "
                        + bus_name + ", " + add + ". T&C");
            }
        }
    });

    productOffer.addTextChangedListener(new TextWatcher() {
        public void afterTextChanged(Editable s) {
        }

        public void beforeTextChanged(CharSequence s, int start, int count,
                int after) {
            validtill = etbirth.getText().toString();
            starttime = startingTime.getText().toString();
            endtime = endingTime.getText().toString();
            timeredeem = timesRedeem.getText().toString();
            add = address.getText().toString();
        }

        public void onTextChanged(CharSequence s, int start, int before,
                int count) {
            po = productOffer.getText().toString();
            name = eventName.getText().toString();
            dis = discount.getText().toString();

            if (po.length() == 0) {
                po = "Pizza";
                output.setText(name + " with friends & family. Get " + dis
                        + "% off on " + po + " till " + validtill
                        + " from " + starttime + " to " + endtime + " at "
                        + bus_name + ", " + add + ". T&C");
            } else {
                validtill = "DD-MM-YYYY";
                starttime = "HH:MM";
                endtime = "HH:MM";
                add = "XYZ";
                output.setText(name + " with friends & family. Get " + dis
                        + "% off on " + po + " till " + validtill
                        + " from " + starttime + " to " + endtime + " at "
                        + bus_name + ", " + add + ". T&C");
            }
        }
    });

    etbirth.addTextChangedListener(new TextWatcher() {
        public void afterTextChanged(Editable s) {
        }

        public void beforeTextChanged(CharSequence s, int start, int count,
                int after) {
            starttime = startingTime.getText().toString();
            endtime = endingTime.getText().toString();
            timeredeem = timesRedeem.getText().toString();
            add = address.getText().toString();
        }

        public void onTextChanged(CharSequence s, int start, int before,
                int count) {
            name = eventName.getText().toString();
            dis = discount.getText().toString();
            po = productOffer.getText().toString();
            validtill = etbirth.getText().toString();

            if (validtill.length() == 0) {
                validtill = "DD-MM-YYYY";
                output.setText(name + " with friends & family. Get " + dis
                        + "% off on " + po + " till " + validtill
                        + " from " + starttime + " to " + endtime + " at "
                        + bus_name + ", " + add + ". T&C");
            } else {
                starttime = "HH:MM";
                endtime = "HH:MM";
                add = "XYZ";
                output.setText(name + " with friends & family. Get " + dis
                        + "% off on " + po + " till " + validtill
                        + " from " + starttime + " to " + endtime + " at "
                        + bus_name + ", " + add + ". T&C");
            }
        }
    });

    startingTime.addTextChangedListener(new TextWatcher() {
        public void afterTextChanged(Editable s) {
        }

        public void beforeTextChanged(CharSequence s, int start, int count,
                int after) {
            endtime = endingTime.getText().toString();
            timeredeem = timesRedeem.getText().toString();
            add = address.getText().toString();
        }

        public void onTextChanged(CharSequence s, int start, int before,
                int count) {
            name = eventName.getText().toString();
            dis = discount.getText().toString();
            po = productOffer.getText().toString();
            validtill = etbirth.getText().toString();
            starttime = startingTime.getText().toString();

            if (starttime.length() == 0) {
                starttime = "HH:MM";
                output.setText(name + " with friends & family. Get " + dis
                        + "% off on " + po + " till " + validtill
                        + " from " + starttime + " to " + endtime + " at "
                        + bus_name + ", " + add + ". T&C");
            } else {
                endtime = "HH:MM";
                add = "XYZ";
                output.setText(name + " with friends & family. Get " + dis
                        + "% off on " + po + " till " + validtill
                        + " from " + starttime + " to " + endtime + " at "
                        + bus_name + ", " + add + ". T&C");
            }
        }
    });

    endingTime.addTextChangedListener(new TextWatcher() {
        public void afterTextChanged(Editable s) {
        }

        public void beforeTextChanged(CharSequence s, int start, int count,
                int after) {
            timeredeem = timesRedeem.getText().toString();
            add = address.getText().toString();
        }

        public void onTextChanged(CharSequence s, int start, int before,
                int count) {
            name = eventName.getText().toString();
            dis = discount.getText().toString();
            po = productOffer.getText().toString();
            validtill = etbirth.getText().toString();
            starttime = startingTime.getText().toString();
            endtime = endingTime.getText().toString();

            if (endtime.length() == 0) {
                endtime = "HH:MM";
                output.setText(name + " with friends & family. Get " + dis
                        + "% off on " + po + " till " + validtill
                        + " from " + starttime + " to " + endtime + " at "
                        + bus_name + ", " + add + ". T&C");
            } else {
                add = "XYZ";
                output.setText(name + " with friends & family. Get " + dis
                        + "% off on " + po + " till " + validtill
                        + " from " + starttime + " to " + endtime + " at "
                        + bus_name + ", " + add + ". T&C");
            }
        }
    });

    timesRedeem.addTextChangedListener(new TextWatcher() {

        @Override
        public void onTextChanged(CharSequence s, int start, int before,
                int count) {
            // TODO Auto-generated method stub
            name = eventName.getText().toString();
            dis = discount.getText().toString();
            po = productOffer.getText().toString();
            validtill = etbirth.getText().toString();
            starttime = startingTime.getText().toString();
            endtime = endingTime.getText().toString();
            tr = timesRedeem.getText().toString();

            if (tr.length() == 0) {
                tr = "1";
                output.setText(name + " with friends & family. Get " + dis
                        + "% off on " + po + " till " + validtill
                        + " from " + starttime + " to " + endtime + " at "
                        + bus_name + ", " + add + ". T&C");

            } else {
                add = "XYZ";
                output.setText(name + " with friends & family. Get " + dis
                        + "% off on " + po + " till " + validtill
                        + " from " + starttime + " to " + endtime + " at "
                        + bus_name + ", " + add + ". T&C");
            }
        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count,
                int after) {
            // TODO Auto-generated method stub
            add = address.getText().toString();
        }

        @Override
        public void afterTextChanged(Editable s) {
            // TODO Auto-generated method stub

        }
    });

    address.addTextChangedListener(new TextWatcher() {
        public void afterTextChanged(Editable s) {
        }

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

        public void onTextChanged(CharSequence s, int start, int before,
                int count) {
            name = eventName.getText().toString();
            dis = discount.getText().toString();
            po = productOffer.getText().toString();
            validtill = etbirth.getText().toString();
            starttime = startingTime.getText().toString();
            endtime = endingTime.getText().toString();
            tr = timesRedeem.getText().toString();
            add = address.getText().toString();
            if (add.length() == 0) {
                add = "XYZ";
                output.setText(name + " with friends & family. Get " + dis
                        + "% off on " + po + " till " + validtill
                        + " from " + starttime + " to " + endtime + " at "
                        + bus_name + ", " + add + ". T&C");

            } else {
                output.setText(name + " with friends & family. Get " + dis
                        + "% off on " + po + " till " + validtill
                        + " from " + starttime + " to " + endtime + " at "
                        + bus_name + ", " + add + ". T&C");
            }
        }
    });

    nextoffer.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            name = eventName.getText().toString();
            dis = discount.getText().toString();
            po = productOffer.getText().toString();
            validtill = etbirth.getText().toString();
            starttime = startingTime.getText().toString();
            endtime = endingTime.getText().toString();
            timeredeem = timesRedeem.getText().toString();
            add = address.getText().toString();

            if (name.length() == 0) {
                eventName.setError("Enter Event Name");
            } else if (dis.length() == 0) {
                discount.setError("Enter Discount Value");
            } else if (dis.startsWith("0")) {
                discount.setError("Invalid Bill Amount");
            } else if (po.length() == 0) {
                productOffer.setError("Enter Product Offer");
            } else if (validtill.length() == 0) {
                Toast.makeText(getApplicationContext(),
                        "Please choose Date", 1).show();
            } else if (starttime.length() == 0) {
                Toast.makeText(getApplicationContext(),
                        "Please choose Starting Time", 1).show();
            } else if (endtime.length() == 0) {
                Toast.makeText(getApplicationContext(),
                        "Please choose Ending Time", 1).show();
            } else if (timeredeem.length() == 0) {
                timesRedeem
                        .setError("Enter number of times the offer can be redeemed");
            } else if (timeredeem.startsWith("0")) {
                timesRedeem.setError("Invalid Value");
            } else if (add.length() == 0) {
                address.setError("Enter Address");
            } else {

                SimpleDateFormat sdf = new SimpleDateFormat("hh:mm a");

                try {

                    Date d1 = sdf.parse(starttime);
                    Date d2 = sdf.parse(endtime);

                    if ((d2.getTime()) > (d1.getTime())) {

                        offer_name = output.getText().toString();

                        Intent intent = new Intent(Offers.this,
                                OfferCreate.class);
                        intent.putExtra("offername1", offer_name);
                        intent.putExtra("offer_discount", dis);
                        intent.putExtra("offer_valid_date", validtill);
                        intent.putExtra("offer_startingtime", starttime);
                        intent.putExtra("offer_endingtime", endtime);
                        intent.putExtra("offer_time_redeem", timeredeem);
                        intent.putExtra("offer_product", po);
                        intent.putExtra("offer_address", add);
                        intent.putExtra("offer_event_name", name);
                        startActivity(intent);

                    } else if ((d1.getTime()) >= (d2.getTime())) {
                        // System.out.println("deep less");
                        startingTime.setText("");
                        endingTime.setText("");
                    }
                } catch (ParseException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (java.text.ParseException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

                String Eventname = eventName.getText().toString();
                String Discoun = discount.getText().toString();
                String Productoffer = productOffer.getText().toString();
                String Startingtime = startingTime.getText().toString();
                String Endingtime = endingTime.getText().toString();
                String Validtill = etbirth.getText().toString();
                String Timesredeem = timesRedeem.getText().toString();
                String Addresss = address.getText().toString();
                String Output1 = output.getText().toString();

                Editor editor = sharedpreferences.edit();
                editor.putString(EventName, Eventname);
                editor.putString(Discount, Discoun);
                editor.putString(ProductOffer, Productoffer);
                editor.putString(StartingTime, Startingtime);
                editor.putString(EndingTime, Endingtime);
                editor.putString(ValidTill, Validtill);
                editor.putString(TimesRedeem, Timesredeem);
                editor.putString(Address, Addresss);
                editor.putString(Output, Output1);

                editor.commit();

            }

        }
    });
}

【问题讨论】:

  • 让我们看一些代码,...
  • @nobalG.. 检查我更新的代码

标签: android android-edittext sharedpreferences textview


【解决方案1】:

这是因为您添加/实现了“onTextChangedListener”。因此,当您在 edittext 中键入内容时,textview 的值就会改变。

尽管在您单击按钮之前它不会存储在 sharedpreference 中。因此,stpo 更改 textview 文本的最佳方法是删除“onTextChangedListener”。

或

只需不要在“onTextChangedListener”中为 textview 设置值。这肯定会解决你的问题。

【讨论】:

  • @RIT..那么我应该如何在不使用 onTextChangedListener 的情况下在文本视图上设置文本..
  • 当然可以,我认为在你的情况下这是唯一的方法。
  • 我已经在回答中这样告诉过你。不要在 On/addTextChanglistener() 接口中使用 setText。
  • 那么我应该如何在文本视图上设置文本以更改编辑文本上的文本?
  • 现在你正在做。我说不要那样做。仔细阅读我的回答。
猜你喜欢
  • 1970-01-01
  • 2020-08-19
  • 1970-01-01
  • 2014-03-21
  • 1970-01-01
  • 1970-01-01
  • 2012-09-10
  • 1970-01-01
  • 2012-03-29
相关资源
最近更新 更多