Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
140 views
in Technique[技术] by (71.8m points)

android - Need help regarding textviews, dialogboxes, boolean statements, and switch cases

What I want is for the user to be able to click a TextView, after which a DialogBox pops up, and if the User clicks on the positive button, a Boolean value is changed from False to True.

It is for purchasing items in app items with in app currency.

Example: Inside the TextView, the item name is given, (Special Fruit), when the TextView is clicked a DialogBox pops up, asking, Are you sure you want to purchase this item? Yes/No. If user clicks on yes and had clicked on TExtView A, I want a Boolean A to change to true where default value was false.

Here is the code so far

public class ShopScreen extends AppCompatActivity {

private TextView TV1, TV2, TV3, TV4, TV5;
private boolean B1, B2, B3, B4, B5;

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

    B1 = false;
    B2 = false;
    B3 = false;
    B4 = false;
    B5 = false;

    TV1 = findViewById(R.id.TextOne);
    TV2 = findViewById(R.id.TextTwo);
    TV3 = findViewById(R.id.TextThree);
    TV4 = findViewById(R.id.TextFour);
    TV5 = findViewById(R.id.TextFive);

    TV1.setText("Special Fruit");
    TV1.setText("Apple Munch");
    TV1.setText("Watermelon Pop");
    TV1.setText("Passionfruit Blast");
    TV1.setText("Strawberry Explosion");

    TV1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            fruitPurchase();

        }
    });

    TV2.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            fruitPurchase();

        }
    });

    TV3.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            fruitPurchase();

        }
    });

    TV4.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            fruitPurchase();

        }
    });

    TV5.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            fruitPurchase();

        }
    });
}

public void fruitPurchase () {

    AlertDialog.Builder DBuild = new AlertDialog.Builder(ShopScreen.this);
    LayoutInflater DInflater = ShopScreen.this.getLayoutInflater();
    View DView = DInflater.inflate(R.layout.ao_dialog, null);
    DBuild.setView(DView)
            .setTitle("Confirm")
            .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {

                }
            })
            .setPositiveButton("Confirm", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {

               // Completely confused about what to do from here.

                    switch () {

                        case R.id.TV1;
                        
              // If TV1 was Selected, I want boolean B1 to change from false to true.
                            
                            B1 = true;
                            break;

                        case R.id.TV2;
                            B1 = true;
                            break;

                    }
                }
            });
    DBuild.show();
}

public void changeviews () {

    if (B1 = true) {
        TV1.setText("Sold Out");
        TV1.setClickable(false);
    }
}

I would really appreciate any help you could offer.

question from:https://stackoverflow.com/questions/66046932/need-help-regarding-textviews-dialogboxes-boolean-statements-and-switch-cases

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Change fruitPurchase so that it accepts a View:

public void fruitPurchase (View view) {
    //...
}

Then pass the view from inside the click listener:

  TV1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            fruitPurchase(v);

        }
    });

Do the above for all your textview click listeners.

Then check the view id inside your switch case as follows:

switch (view.getId()) {
    case R.id.TextOne;
        B1 = true;
        break;
    case R.id.TextTwo;
        B2 = true;
        break;
    // and so on for other text views
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...