简体   繁体   English

执行back和cancelbutton的区别?

[英]The difference between implementation of back and cancelbutton?

I ma newbie an trying to learn Java/Android-programming. 我是新手,试图学习Java / Android编程。

I m doing an app for Android in Eclipse and created some buttons. 我正在Eclipse中为Android开发一个应用程序,并创建了一些按钮。 I have a back and a cancel button. 我有一个后退和一个取消按钮。

Example: 例:

I have a EditText there you can write in your name. 我有一个EditText,您可以用自己的名字写。 If you write yourname and press the backbutton, then u will go back to the previous Activity, but if you go to the same Activity, then you will still see the name that you wrote in the EditText. 如果您输入您的姓名并按下后退按钮,则u将返回到上一个活动,但是如果您转到相同的活动,则仍会看到您在EditText中编写的名称。

But if you press the cancelbutton, you will go back to the previous Activity, but when you come back, yourname will be empty. 但是,如果您按“取消”按钮,您将返回到上一个活动,但是当您返回时,您的姓名将为空。 I will "kill" or "stop" the Activity. 我将“杀死”或“停止”该活动。

This is the code I use for the Backbutton, what would you use for the Cancel Button? 这是我用于“后退”按钮的代码,您将用于“取消”按钮吗? Thank YOU. 谢谢。

public void onClick(View v) {
    switch(v.getId()){
    case R.id.buttonBack:
        Intent intent = new Intent (AllActivity.this, MenuActivity.class);
        startActivity(intent);
        break;

For the cancel button you can use the below method, this will kill the activity. 对于取消按钮,您可以使用以下方法,这将终止活动。

 finish()

so in your code it will look something like this: 所以在您的代码中它将看起来像这样:

 public void onClick(View v) {
     switch(v.getId()){
     case R.id.cancel:
         finish();
         break;

There was little difference in this as per requirement of process or application flow. 根据流程或应用程序流程的要求,这几乎没有区别。 For cancel and back as work are same for example if you open any dialog and provide cancel button will close/dismiss your dialog same way the back button do this. 例如,如果您打开任何对话框,并且提供“取消”按钮将以与“后退”按钮相同的方式关闭/关闭对话框,则“取消”和“恢复工作”是相同的。 While for implementing with the Activity you if you implement for closing current activity you can just finish with both option by just calling finish() method. 在使用Activity进行实现时,如果您实现关闭当前活动,则只需调用finish()方法就可以同时完成这两个选项。 As back button was normally work for finish you current activity and back. 由于返回按钮通常可以正常工作,因此您可以完成当前活动并返回。

Another way to do this that you may be interested in is to wipe out the content of the EditText yourself. 您可能感兴趣的另一种方法是自己擦除EditText的内容。 You would need to have in your xml file an id defined for the EditText so that you could access it programatically. 您需要在xml文件中具有为EditText定义的ID,以便可以通过编程方式对其进行访问。

<EditText

layout stuff here: 布局的东西在这里:

    android:layout_width="fill_parent"

... and then the id attribute ...然后是id属性

    android:id="@+id/edit_text_id"
    >

then in your code you would put the following in your class (not inside any method): 然后在您的代码中将以下内容放入您的类中(而不是在任何方法内):

EditText anEditText;

then in your onCreate(), after the inflation of the layout (if it comes beforehand it will cause the app to crash): 然后在您的onCreate()中,在布局膨胀之后(如果事先出现,将导致应用崩溃):

anEditText = (EditText) findViewById(R.id.edit_text_id);

the name edit_text_id is not significant, but it is what we used in the layout file 名称edit_text_id并不重要,但这是我们在布局文件中使用的名称

next add to the onClick method for cancel (after the case statement): 接下来添加到onClick方法中以进行取消(在case语句之后):

//this wipes the text from the textbox
anEditText.setText("");
// add the rest of the back button code after this and your good!

Best of luck! 祝你好运! Remember that we were all newbies once. 请记住,我们都是新手。 If you want to be a good android programmer, I suggest that you get a strong background in Java first. 如果您想成为一名优秀的android程序员,我建议您首先在Java方面有扎实的背景。 This free book helped me very much! 这本免费书对我很有帮助!

Java Notes Java笔记

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Aggregation和Association在实现上的区别 - The difference between Aggregation and Association in implementation Python与Java中的模数实现之间的差异 - Difference Between Modulus Implementation in Python Vs Java 编译(实现)和包含项目之间的区别 - Difference between compile(implementation) and include Project JAR清单文件 - 规范和实现之间的差异 - JAR Manifest file - Difference between Specification and Implementation 这两种单例实现之间有什么区别? - What is the difference between these two implementation of singleton? Java中聚合和组合的实现区别 - Implementation difference between Aggregation and Composition in Java 工具栏后退按钮和onBackPressed方法之间的区别 - Difference between toolbar back button and onBackPressed method Spring Security和OAuth2之间Principal接口的两种不同实现 - Two difference implementation of Principal interface between Spring Security & OAuth2 这两种向BST插入操作的实现之间有什么区别? - What is the difference between these two implementation of insertion operation to BST? Java实现与UML规范wrt接口和抽象类之间的区别 - Difference between Java implementation and UML specification wrt interfaces and abstract classes
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM