上按一下按钮重新启动后会删除TextView的现有数据 [英] on button click deletes existing data from TextView after re-launching
本文介绍了上按一下按钮重新启动后会删除TextView的现有数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
每当我重新启动我的应用程序,让旧值到TextView的,如果一些存储由我,而是对着一个小问题,现在重新发动我的应用程序后,有一次我做水龙头的按钮,它的TextView删除先前存储的数据并显示新的。
我使用我的程序来存储数据TextView的,这对我来说工作只是正常的共享preferences。
可能我知道我在做什么错误?
见下面我的code:
公共类MainActivity扩展ActionBarActivity { TextView的textViewResult;
的EditText editTextInput;
串strInput =;
按钮btnInput; @覆盖
保护无效的onCreate(捆绑savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.activity_main); 最后一个共享preferences preFS = preferenceManager
.getDefaultShared preferences(本); textViewResult =(的TextView)findViewById(R.id.textResult);
editTextInput =(EditText上)findViewById(R.id.editInput); textViewResult.setText(prefs.getString(自动保存,)); textViewResult.addTextChangedListener(新TextWatcher(){ @覆盖
公共无效onTextChanged(CharSequence的为arg0,ARG1 INT,INT ARG2,诠释ARG3){
// TODO自动生成方法存根 } @覆盖
公共无效beforeTextChanged(CharSequence的为arg0,ARG1 INT,INT ARG2,
INT ARG3){
// TODO自动生成方法存根 } @覆盖
公共无效afterTextChanged(编辑S){
// TODO自动生成方法存根
。prefs.edit()putString(自动转存,s.toString())提交();
}
}); btnInput =(按钮)findViewById(R.id.button1);
btnInput.setOnClickListener(新OnClickListener(){ @覆盖
公共无效的onClick(查看为arg0){
// TODO自动生成方法存根
字符串检查= editTextInput.getText()的toString()。
如果(check.equalsIgnoreCase(ABC))
{
strInput = strInput +,+检查; Toast.makeText(getApplicationContext(),完成,Toast.LENGTH_LONG).show();
textViewResult.setText(strInput);
editTextInput.setText();
}
其他{
Toast.makeText(getApplicationContext(),错误,Toast.LENGTH_LONG).show();
}
}
});
}
}
解决方案
试试这个..
btnInput.setOnClickListener(新OnClickListener(){ @覆盖
公共无效的onClick(查看为arg0){
// TODO自动生成方法存根
字符串检查= editTextInput.getText()的toString()。
如果(check.equalsIgnoreCase(ABC))
{
strInput = textViewResult.getText()的toString()+,+检查。 Toast.makeText(getApplicationContext(),完成,Toast.LENGTH_LONG).show();
textViewResult.setText(strInput);
editTextInput.setText();
}
其他{
Toast.makeText(getApplicationContext(),错误,Toast.LENGTH_LONG).show();
}
}
});
修改
使用拆分()
的String [] = total_abc textViewResult.getText()的toString()分裂()。;
INT总= total_abc.length;
Whenever i relaunch my app, getting old values into TextView if some stored by me, but facing a small issue, now after relaunching my app once i do tap on button, it deletes earlier stored data from TextView and shows new one.
I am using SharedPreferences in my program to store textview data, which works just fine for me.
May i know where i am doing mistake ?
see my code below:
public class MainActivity extends ActionBarActivity {
TextView textViewResult;
EditText editTextInput;
String strInput = "";
Button btnInput;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final SharedPreferences prefs = PreferenceManager
.getDefaultSharedPreferences(this);
textViewResult = (TextView) findViewById(R.id.textResult);
editTextInput = (EditText) findViewById(R.id.editInput);
textViewResult.setText(prefs.getString("autoSave", ""));
textViewResult.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
// TODO Auto-generated method stub
}
@Override
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
int arg3) {
// TODO Auto-generated method stub
}
@Override
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
prefs.edit().putString("autoSave", s.toString()).commit();
}
});
btnInput = (Button) findViewById(R.id.button1);
btnInput.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
String check = editTextInput.getText().toString();
if(check.equalsIgnoreCase("ABC"))
{
strInput = strInput+","+check;
Toast.makeText(getApplicationContext(), "done", Toast.LENGTH_LONG).show();
textViewResult.setText(strInput);
editTextInput.setText("");
}
else {
Toast.makeText(getApplicationContext(), "error", Toast.LENGTH_LONG).show();
}
}
});
}
}
解决方案
Try this..
btnInput.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
String check = editTextInput.getText().toString();
if(check.equalsIgnoreCase("ABC"))
{
strInput = textViewResult.getText().toString()+","+check;
Toast.makeText(getApplicationContext(), "done", Toast.LENGTH_LONG).show();
textViewResult.setText(strInput);
editTextInput.setText("");
}
else {
Toast.makeText(getApplicationContext(), "error", Toast.LENGTH_LONG).show();
}
}
});
EDIT
Use split("")
String[] total_abc = textViewResult.getText().toString().split(",");
int total = total_abc.length;
这篇关于上按一下按钮重新启动后会删除TextView的现有数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文