完成活动首先清除 EditTexts,然后才执行它应该执行的操作
这是我在这里发表的第一篇文章。 我在管理活动方面遇到问题。我有我的主要活动,它启动包含一些 EditText 的其他活动。这是启动代码:
Intent myIntent = new Intent(ActualClass.this, NextClass.class);
startActivity(myIntent);
我在新启动的活动和一些按钮上有几个 EditText。其中一个按钮让我回到主要活动。在其 OnTouchEvent 上,我运行简单的 finish(); 这一切看起来都很简单,应该工作得很好,但是当我单击应该让我返回主要活动的按钮时,它首先会清除我的 EditTexts。当我第二次单击它时,它终于让我回到了我的主要活动。我应该怎么做才能防止清除 EditTexts 并在第一次单击后返回到我的主要活动?我非常感谢你的帮助。
抱歉,大家,我正在添加更多代码。所以这里是启动第二个活动的侦听器,即带有 EditTexts 的活动:
public boolean onTouch(View v, MotionEvent me) {
switch(v.getId()){
case R.id.buttonForward:
Intent myIntent = new Intent(ActualClass.this, NextClass.class);
startActivity(myIntent);
break;
default:
break;
}
return false;
}
在第二个活动中,我的问题似乎发生了,按钮上的侦听器如下所示:
buttonBack.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View arg0, MotionEvent arg1) {
finish();
return false;
}
});
问题是:当我第一次单击第二个活动,它不会让我回到我的主要活动 - 它清除活动上的所有 EditText 并将焦点设置在第一个元素上。当我第二次单击该按钮时,它终于让我回到主要活动。
我提供 NextClass 类的完整代码,即带有 EditTexts 的类。
public class NextClass extends Activity implements OnTouchListener {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.nextclasslayout);
final EditText firstEditText = (EditText) findViewById(R.id.firstEditTextName);
final EditText secondEditText = (EditText) findViewById(R.id.secondEditTextName);
ImageButton buttonBack = (ImageButton) findViewById(R.id.imageButtonBack);
buttonBack.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View arg0, MotionEvent arg1) {
finish();
return true;
}
});
}
@Override
public boolean onTouch(View arg0, MotionEvent arg1) {
return false;
}
}
that's my first post here.
I have problem with managing activities. I have my main activity, which launches other activities containing some EditTexts. Here's the code for launching:
Intent myIntent = new Intent(ActualClass.this, NextClass.class);
startActivity(myIntent);
I have few EditTexts on the freshly launched activity and some buttons. One of the buttons gets me back to the main activity. On its OnTouchEvent I run simple finish(); It all seems simple and should work just fine, but when I click the button which should get me back to my main activity, it firstly clears my EditTexts. When I click it for the second time, It finally gets me back to my main activity. What should I do to prevent clearing the EditTexts and get back to my main activity after the first click? I'd really appreciate your help.
Sorry guys, I'm adding more code. So here's the listener which launches the second activity, the one with EditTexts:
public boolean onTouch(View v, MotionEvent me) {
switch(v.getId()){
case R.id.buttonForward:
Intent myIntent = new Intent(ActualClass.this, NextClass.class);
startActivity(myIntent);
break;
default:
break;
}
return false;
}
And on the second activity, where my problem seems to occur, the listener on button looks like this:
buttonBack.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View arg0, MotionEvent arg1) {
finish();
return false;
}
});
And the problem is: when I first click the back button on the second activity, it doesn't get me back to my main activity - it clears all the EditTexts on activity and sets the focus on first element. When I click the button for the second time, it finally gets me back to main activity.
I'm providing full code of the NextClass class, the one with EditTexts.
public class NextClass extends Activity implements OnTouchListener {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.nextclasslayout);
final EditText firstEditText = (EditText) findViewById(R.id.firstEditTextName);
final EditText secondEditText = (EditText) findViewById(R.id.secondEditTextName);
ImageButton buttonBack = (ImageButton) findViewById(R.id.imageButtonBack);
buttonBack.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View arg0, MotionEvent arg1) {
finish();
return true;
}
});
}
@Override
public boolean onTouch(View arg0, MotionEvent arg1) {
return false;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为您的
EditText
没有被清除。您只需启动两个NextClass
活动,因为onTouch()
方法被调用两次:一次用于ACTION_DOWN
,一次用于Action_UP
。当您完成最上面的一个时,下一个就会出现。如果您在ActualClass
活动中使用OnClickListener
来处理按钮的点击事件,那么一切都应该没问题。I think your
EditText
s aren't getting cleared. You just launch twoNextClass
activities becauseonTouch()
method is called twice: once forACTION_DOWN
and once forAction_UP
. And when you finish the top-most one, the next one appears. If you useOnClickListener
inActualClass
activity for handling click events from the button, everything should be fine.