[英]EditText returning same value when activity is created again after the back button is pressed
That's my first post obviously here...okay so my problem is as follows: @android: i have a TextEdit that a user enters in text, i get the text by getText() and it all works fine, but when i press the back button , and again come back to the same activity of the TextEdit and try to get the text again, it returns and empty string"" eventhough a different text is shown in the TextEdit view. 那显然是我的第一篇文章...好吧,所以我的问题如下:@android:我有一个TextEdit,用户在其中输入文本,我通过getText()得到了文本,并且一切正常,但是当我按后退按钮,然后再次返回到TextEdit的相同活动,并尝试再次获取文本,即使在TextEdit视图中显示了不同的文本,它也会返回并显示空字符串“”。 Could it be that the TextEdit is still "relating or corresponding" to the old closed activity?
是不是TextEdit仍与旧的已关闭活动“相关或相对应”? Any ideas ?
有任何想法吗 ?
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.messaging_screen2);
editText = (EditText) findViewById(R.id.msgText);
Button send = (Button)findViewById(R.id.Go);
send.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
textToSend=editText.getText().toString();
}
});
}
Then in an AsynckTask i send the textToSend, 然后在AsynckTask中,我发送textToSend,
@Override
protected String doInBackground(Void... arg0) {
while(!done)
{
if(isCancelled())
{
done=true;
break;
}
if(text.contains(Protocols.RESPONSE_OK))
{
System.out.println("getting message from edit text: "+textToSend);
publishProgress(textToSend);
}
}
return null;
}
Now the when i can send msgs normally but when i press the back button and then start the same activity again, the FIRST msg sent is exactly the old one( upon pressing the send button for the first time ). 现在,当我可以正常发送消息时,但是当我按下后退按钮然后再次开始相同的活动时,发送的第一个消息恰好是旧消息(第一次按下发送按钮时)。 when i press the send button again, the shown text in the EditText is sent properly.
当我再次按下发送按钮时,EditText中显示的文本将正确发送。 so any help ?
有什么帮助吗? Thanks.
谢谢。 After some debugging i noticed that the value of textToSend in send.Onclick() method is not the same as the one in the doInBackGround of the AsynckTask after creating the activity again.
经过一些调试后,我注意到send.Onclick()方法中textToSend的值与AsynckTask的doInBackGround中的值再次不同,再次创建了该活动。 This happens on the first click of send, after witch everything works fine.
在女巫一切正常之后,第一次单击发送时就会发生这种情况。
Its weird, Paste your code, 很奇怪,粘贴您的代码,
Try set edittext.setText("test") in button's onClick() method. 尝试在按钮的onClick()方法中设置edittext.setText(“ test”)。 And check what return edittext.getText() when u back.
并检查当您返回时返回return text.getText()的内容。
To change Activity use you this syntax? 要更改活动,请使用以下语法?
Intent direction = new Intent(Class.this, DirectionClass.class);
startActivity(direction);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.