Android: Double Tap the Back Button?

I am new to android.

My script

I have screen A which has 2 buttons Button A Button B.

When I open the screen of my app A, open with the above two buttons, when I click on the B button, the text image and Edittext are displayed.

What I need?

When the back button is clicked the text and edittext should hide, and when I return again I should exit screen A.

What have I tried so far?

Is my code below correct for what I want?

Main Activity.xml

import android.support.v7.app.ActionBarActivity;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends Activity implements OnClickListener {

    TextView title;
    EditText userinput;
    Button buttonA,buttonB;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initialize();
    }

    private void initialize() {
        userinput = (EditText)findViewById(R.id.userinput);
        title = (TextView)findViewById(R.id.title);
        buttonA = (Button)findViewById(R.id.buttonA);
        buttonB = (Button)findViewById(R.id.buttonB);
        buttonA.setOnClickListener(this);
        buttonB.setOnClickListener(this);
    }



@Override
public void onBackPressed() {
  title.setVisibility(View.INVISIBLE);
 userinput.setVisibility(View.INVISIBLE); 


    }

    @Override
    public void onClick(View v) {

        switch(v.getId())
        {

        case R.id.buttonA:
            break;

        case R.id.buttonB:

            title.setVisibility(View.VISIBLE);
            userinput.setVisibility(View.VISIBLE);
            break;
        }


    }


}

      

I named this and this link but didn't get it. If anyone can help me reach me I want 1

+3


source to share


3 answers


When the back button is clicked the text and editor should hide and when I come back again,



@Override
public void onBackPressed() {
   if (title.getVisibility() != View.VISIBLE && 
             userInput.getVisibility() != View.VISIBLE) {
      super.onBackPressed();
      return;
   }
   title.setText(null);
   userinput.setText(null);
   title.setVisibility(View.INVISIBLE);
   userinput.setVisibility(View.INVISIBLE);        
 }

      

+2


source


Do it.

@Override
 public void onBackPressed() {
if(title.getVisibility()==View.VISIBLE)
 {
  title.setVisibility(View.INVISIBLE);
  userinput.setVisibility(View.INVISIBLE); 
 }
  else
  {
   finish();
  }
}

      



It will do what you want.

0


source


Change the code below

@Override
public void onBackPressed() {
if (title.getVisibility() != View.VISIBLE && 
             userInput.getVisibility() != View.VISIBLE){
title.setVisibility(View.INVISIBLE);
userinput.setVisibility(View.INVISIBLE); 
}
super.onBackPressed();
}

      

0


source







All Articles