我正在设计一个我正在使用共享偏好的应用程序。
我已经拍摄了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();
}
}
});
}
答案 0 :(得分:0)
这种情况正在发生,因为你已经添加/实现了" onTextChangedListener"。因此,当您在edittext中键入内容时,textview的值会发生变化。
虽然直到您点击按钮才会存储在共享首选项中。因此,更改文本视图文本的最佳方法是删除" onTextChangedListener"。
OR
根本不要将值设置为" onTextChangedListener"中的textview。这肯定会解决你的问题。