[英]How to clear the intent text?
I have a reset
button
in Activity A and it works fine since it can clear all the text and display null when the save
button
in Activity B is clicked.我在活动 A 中有一个
reset
button
,它工作正常,因为当单击活动 B 中的save
button
时,它可以清除所有文本并显示空值。 But this only works if there are nothing in the textView before pass to B.但这仅在传递给 B 之前 textView 中没有任何内容时才有效。
It does not works in below cases.它在以下情况下不起作用。
In Activity A, type " Project 123', click next button
go to B. Then I back to Activity A again and click the reset button to clear "Project 123". After done go to Activity B and click submit button. It shows "Project 123" instead of "null"...在活动 A 中,输入“项目 123”,单击下一步
button
转到 B。然后我再次返回活动 A 并单击重置按钮以清除“项目 123”。完成后转到活动 B 并单击提交按钮。它显示“项目 123" 而不是 "null"...
Activity A活动A
private TextView c;
String result; // 123
String name; // project
reset=(Button)claims.findViewById(R.id.button14); // reset button
Button button = (Button) claims.findViewById(R.id.button8); //next button
reset.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
c.setText("");
d.setText("");
e.setText("");
f.setText("");
g.setText("");
h.setText("");
}
});
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
Intent intent = new Intent(getActivity().getApplicationContext(), B.class);
if(c!=null){
intent.putExtra("name", name);
intent.putExtra("result", result);
}
});
return A;
}
Activity B活动乙
Name=getIntent().getExtras().getString("name");
Result=getIntent().getExtras().getString("result");
save=(Button)findViewById(R.id.button8);
save.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
if((Name!=null)&&(Result!=null)){
Toast.makeText(getApplicationContext(), Name+Result, Toast.LENGTH_LONG).show();
}else{
Toast.makeText(getApplicationContext(),"null", Toast.LENGTH_LONG).show();
}
}
});
实际上,名称不为空,而是带有空字符串 "" 尝试在 B 活动中将 Name != null 替换为 !TextUtils.isEmpty()。
This is because you only clear the setText but not the string.这是因为您只清除 setText 而不是字符串。
reset.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
c.setText("");
name="";
result="";
}
});
You have to add name=""
and result=""
in your reset method.您必须在重置方法中添加
name=""
和result=""
。 It should works.它应该有效。
Kindly refer clear a value from a string android请参考从字符串 android 中清除一个值
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.