单击Android后退按钮时清除变量

时间:2016-03-27 01:52:01

标签: android string textview clear back-button

我有一个应用程序,我希望能够点击按钮A,并显示一组特定的信息。然后单击后退按钮并单击按钮B并显示一组不同的信息。我已经将测试TextView编码到Drinks.java文件中,以便通过确认传递的内容来开始该过程。目前我推送的任何按钮都会卡在变量中。因此,例如,如果我按下按钮A,然后按下后退箭头并按下按钮B,按钮A仍然显示在textView中。我尝试在点击监听器中使字符串为空,以“清除它们”,但这不起作用。有没有办法消除变量中的内容并重新分配其他内容?或者问题出在其他地方?

Bar.java

from django.db.models import Count

groups = GroupPlayer.objects.annotate(num_players=Count('player'))
groups[0].num_players

Drinks.java

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

public class Bar extends Activity{
    String setBarTest = MainActivity.setBar;
    String barNameHolder, picHolder, barContactHolder, barPhoneHolder; 
    int imageInt, textInt1,textInt2, textInt3;
    TextView setBarName, setBarContact,setBarPhone;
    ImageView barPic;
    Button viewAll, beer, wine, mixedDrinks, other, getTaxi;
    static String setDrinkType = "";

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_bar);

        Button viewAll = (Button)findViewById(R.id.btnviewAll);
        Button beer = (Button)findViewById(R.id.btnBeer);
        Button wine = (Button)findViewById(R.id.btnWine);
        Button mixedDrinks = (Button)findViewById(R.id.btnMixedDrinks);
        Button other = (Button)findViewById(R.id.btnOther);
        Button getTaxi = (Button)findViewById(R.id.btnTaxi);

        barPic = (ImageView) findViewById(R.id.barPic);
        String picHolder = "drawable/"+setBarTest;      
        int imageInt = getResources().getIdentifier(picHolder, null, getPackageName());
        barPic.setImageResource(imageInt);

        setBarName = (TextView)findViewById(R.id.barName);
        String barNameHolder = "@string/"+setBarTest;
        int textInt1 = getResources().getIdentifier(barNameHolder, null, getPackageName());
        setBarName.setText(textInt1);

        setBarContact = (TextView)findViewById(R.id.barContact);
        String barContactHolder = "@string/"+setBarTest+"Contact";
        int textInt2 = getResources().getIdentifier(barContactHolder, null, getPackageName());
        setBarContact.setText(textInt2);

        setBarPhone = (TextView)findViewById(R.id.barPhone);
        String barPhoneHolder = "@string/"+setBarTest+"Phone";
        int textInt3 = getResources().getIdentifier(barPhoneHolder, null, getPackageName());
        setBarPhone.setText(textInt3);

        viewAll.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent i = (new Intent(Bar.this, Drinks.class));
                startActivity(i);           
            }

        });
        beer.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                setDrinkType = "";
                setDrinkType = "Beer";
                Intent i = (new Intent(Bar.this, Drinks.class));
                startActivity(i); 

            }

        });
        wine.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                setDrinkType = "";
                setDrinkType = "Wine";
                Intent i = (new Intent(Bar.this, Drinks.class));
                startActivity(i);   

            }

        });
        mixedDrinks.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                setDrinkType = "";
                setDrinkType = "Mixed Drink";
                Intent i = (new Intent(Bar.this, Drinks.class));
                startActivity(i); 

            }

        });

        other.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                setDrinkType = "";
                setDrinkType = "Other";
                Intent i = (new Intent(Bar.this, Drinks.class));
                startActivity(i); 

            }

        });
        getTaxi.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent i = (new Intent(Bar.this, Taxi.class));
                startActivity(i);           
            }

        });


    }
}

1 个答案:

答案 0 :(得分:0)

请使用例如发送到启动活动的意图:

How do I get extra data from intent on Android?