首页 > 解决方案 > How can I change a buttons text when a different button is clicked and still have it function the same? Android

问题描述

I asked this question the other day and got some answers however I'm unable to implement them.

I've set a button up to change the language from English to Welsh, only about 5 lines of English so just changing the text in each box manually with .setText (also Welsh isn't supported).

I also needed to change the text on the buttons, this works but once I change the language the other buttons fail to function.

Once the english or welsh button is pressed (changing the language of the buttons) the calc, reset, calc2, and reset2 buttons stop doing the function they are set up to do...but they work before neither button is pressed.

This is still my first app so apologies if it's messy, I'm going to include all the code in the hope that someone can see where I've gone wrong.

The code in question is right at the bottom, the english and welsh buttons.

Thanks

public class MainActivity extends AppCompatActivity {

private EditText num1;
private EditText num2;
private EditText num3;
private EditText num4;
private TextView total;
private EditText num5;
private EditText num6;
private TextView total2;


private TextView abv1;
private TextView ml1;
private TextView quantity1;
private TextView removed1;
private TextView total1;

private TextView abv2;
private TextView ml2;
private TextView total22;

private TextView multi_title;
private TextView single_title;


public static double aroundUp(double number, int canDecimal) {
    int cifras = (int) Math.pow(10, canDecimal);
    return Math.ceil(number * cifras) / cifras;
}




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

    multi_title = findViewById(R.id.multi_english);
    single_title = findViewById(R.id.single_english);

    num1 = findViewById(R.id.a1numberdec);
    num2 = findViewById(R.id.a2number);
    num3 = findViewById(R.id.a3number);
    num4 = findViewById(R.id.a4number);
    Button calc = findViewById(R.id.buttoncalc);
    Button reset = findViewById(R.id.buttonreset);

    total = findViewById(R.id.a5number);
    num5 = findViewById(R.id.a6numberdec);
    num6 = findViewById(R.id.a7number);
    total2 = findViewById(R.id.a8number);
    Button calc2 = findViewById(R.id.buttoncalc2);
    Button reset2 = findViewById(R.id.buttonreset2);

    abv1 = findViewById(R.id.abv);
    ml1 = findViewById(R.id.ml);
    quantity1 = findViewById(R.id.quantity);
    removed1 = findViewById(R.id.removed1);
    total1 = findViewById(R.id.total1);

    abv2 = findViewById(R.id.abv2);
    ml2 = findViewById(R.id.ml2);
    total22 = findViewById(R.id.total2);

    Button welsh = findViewById(R.id.welsh);
    Button english = findViewById(R.id.english);






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

            if (num1.getText().toString().length() == 0) {
                num1.setText("0");
            }
            if (num2.getText().toString().length() == 0) {
                num2.setText("0");
            }
            if (num3.getText().toString().length() == 0) {
                num3.setText("0");
            }
            if (num4.getText().toString().length() == 0) {
                num4.setText("0");
            }


            double number1 = Double.parseDouble(num1.getText().toString());
            double number2 = Double.parseDouble(num2.getText().toString());
            double number3 = Double.parseDouble(num3.getText().toString());
            double number4 = Double.parseDouble(num4.getText().toString());
            double sum = (((number1 * number2) / 1000) * 0.5) * (number3 - number4);
            total.setText(String.format("£%s", aroundUp(sum, 2)));

        }
    });


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

            num1.setText("");
            num2.setText("");
            num3.setText("");
            num4.setText("");
            total.setText("");
            num1.requestFocus();
        }
    });


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

            if (num5.getText().toString().length() == 0) {
                num5.setText("0");
            }
            if (num6.getText().toString().length() == 0) {
                num6.setText("0");
            }



            double number1 = Double.parseDouble(num5.getText().toString());
            double number2 = Double.parseDouble(num6.getText().toString());
            double sum = (((number1 * number2) / 1000) * 0.5);
            total2.setText(String.format("£%s", aroundUp(sum, 2)));

        }
    });

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

            num5.setText("");
            num6.setText("");
            total2.setText("");
            num5.requestFocus();
        }
    });

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

            abv1.setText("abv% fesul can/botel?");
            ml1.setText("ml fesul can / botel?");
            quantity1.setText("Nifer yn y pecyn llawn?");
            removed1.setText("Nifer wedi'i dynnu \n o becyn?");
            total1.setText("Gellir lleihau'r cyn \n lleied o gynnyrch i -");

            abv2.setText("abv% fesul can/botel?");
            ml2.setText("ml per can/bottle?");
            total22.setText("Gellir lleihau'r cyn \n lleied o gynnyrch i -");

            multi_title.setText("Cyfrifiannell Lluosog");
            single_title.setText("Cyfrifiannell Potel Sengl");




            Button calc = findViewById(R.id.buttoncalc);
            calc.setOnClickListener(this);
            calc.setText("Cyfrifo");

            Button reset = findViewById(R.id.buttonreset);
            reset.setOnClickListener(this);
            reset.setText("Ail gychwyn");

            Button calc2 = findViewById(R.id.buttoncalc2);
            calc2.setOnClickListener(this);
            calc2.setText("Cyfrifo");

            Button reset2 = findViewById(R.id.buttonreset2);
            reset2.setOnClickListener(this);
            reset2.setText("Ail gychwyn");




            }
        });

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

            abv1.setText("abv % per can/bottle?");
            ml1.setText("ml per can/bottle?");
            quantity1.setText("Quantity in full pack?");
            removed1.setText("Quantity removed \n from pack?");
            total1.setText("Minimum product can \n be reduced to - ");

            abv2.setText("abv % per can/bottle?");
            ml2.setText("ml per can/bottle?");
            total22.setText("Minimum product can \n be reduced to - ");

            Button calc = findViewById(R.id.buttoncalc);
            calc.setOnClickListener(this);
            calc.setText("Calculation");

            Button reset = findViewById(R.id.buttonreset);
            reset.setOnClickListener(this);
            reset.setText("Reset");

            Button calc2 = findViewById(R.id.buttoncalc2);
            calc2.setOnClickListener(this);
            calc2.setText("Calculation");

            Button reset2 = findViewById(R.id.buttonreset2);
            reset2.setOnClickListener(this);
            reset2.setText("Reset");

            multi_title.setText("Multipack Calculator");
            single_title.setText("Single Bottle Calculator");

        }
    });






}
}

This is the answer I got the last asked is below and it seems good, I've tried it in various different places in the code but always come out with some form or error, the reason being is probably how I've laid out the rest of my code?

Button calc = findViewById(R.id.buttoncalc);
Button reset = findViewById(R.id.buttonreset);
Button calc2 = findViewById(R.id.buttoncalc2);
Button reset2 = findViewById(R.id.buttonreset2);

setClicks();

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


    quantity1.setText("Quantity");
    total1.setText("Minimum");
    calc.setText("Calculation");
    reset.setText("Reset");
    calc2.setText("Calculation");
    reset2.setText("Reset");
    multi_title.setText("Multi Calculator");
    single_title.setText("Single Calculator");

    }
    });

private void setClicks() {

    calc.setOnClickListener(this);
    reset.setOnClickListener(this);
    calc2.setOnClickListener(this);
    reset2.setOnClickListener(this);
    }

标签: androidandroid-studiobuttonreplacetextbox

解决方案


Try this code, i think the issue is reinitialization and setOnclickListener() again

public class MainActivity extends AppCompatActivity {

private EditText num1;
private EditText num2;
private EditText num3;
private EditText num4;
private TextView total;
private EditText num5;
private EditText num6;
private TextView total2;


private TextView abv1;
private TextView ml1;
private TextView quantity1;
private TextView removed1;
private TextView total1;

private TextView abv2;
private TextView ml2;
private TextView total22;

private TextView multi_title;
private TextView single_title;


public static double aroundUp(double number, int canDecimal) {
    int cifras = (int) Math.pow(10, canDecimal);
    return Math.ceil(number * cifras) / cifras;
}




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

    multi_title = findViewById(R.id.multi_english);
    single_title = findViewById(R.id.single_english);

    num1 = findViewById(R.id.a1numberdec);
    num2 = findViewById(R.id.a2number);
    num3 = findViewById(R.id.a3number);
    num4 = findViewById(R.id.a4number);
    final Button calc = findViewById(R.id.buttoncalc);
    final Button reset = findViewById(R.id.buttonreset);

    total = findViewById(R.id.a5number);
    num5 = findViewById(R.id.a6numberdec);
    num6 = findViewById(R.id.a7number);
    total2 = findViewById(R.id.a8number);
    final Button calc2 = findViewById(R.id.buttoncalc2);
    final Button reset2 = findViewById(R.id.buttonreset2);

    abv1 = findViewById(R.id.abv);
    ml1 = findViewById(R.id.ml);
    quantity1 = findViewById(R.id.quantity);
    removed1 = findViewById(R.id.removed1);
    total1 = findViewById(R.id.total1);

    abv2 = findViewById(R.id.abv2);
    ml2 = findViewById(R.id.ml2);
    total22 = findViewById(R.id.total2);

    Button welsh = findViewById(R.id.welsh);
    Button english = findViewById(R.id.english);






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

            if (num1.getText().toString().length() == 0) {
                num1.setText("0");
            }
            if (num2.getText().toString().length() == 0) {
                num2.setText("0");
            }
            if (num3.getText().toString().length() == 0) {
                num3.setText("0");
            }
            if (num4.getText().toString().length() == 0) {
                num4.setText("0");
            }


            double number1 = Double.parseDouble(num1.getText().toString());
            double number2 = Double.parseDouble(num2.getText().toString());
            double number3 = Double.parseDouble(num3.getText().toString());
            double number4 = Double.parseDouble(num4.getText().toString());
            double sum = (((number1 * number2) / 1000) * 0.5) * (number3 - number4);
            total.setText(String.format("£%s", aroundUp(sum, 2)));

        }
    });


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

            num1.setText("");
            num2.setText("");
            num3.setText("");
            num4.setText("");
            total.setText("");
            num1.requestFocus();
        }
    });


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

            if (num5.getText().toString().length() == 0) {
                num5.setText("0");
            }
            if (num6.getText().toString().length() == 0) {
                num6.setText("0");
            }



            double number1 = Double.parseDouble(num5.getText().toString());
            double number2 = Double.parseDouble(num6.getText().toString());
            double sum = (((number1 * number2) / 1000) * 0.5);
            total2.setText(String.format("£%s", aroundUp(sum, 2)));

        }
    });

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

            num5.setText("");
            num6.setText("");
            total2.setText("");
            num5.requestFocus();
        }
    });

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

            abv1.setText("abv% fesul can/botel?");
            ml1.setText("ml fesul can / botel?");
            quantity1.setText("Nifer yn y pecyn llawn?");
            removed1.setText("Nifer wedi'i dynnu \n o becyn?");
            total1.setText("Gellir lleihau'r cyn \n lleied o gynnyrch i -");

            abv2.setText("abv% fesul can/botel?");
            ml2.setText("ml per can/bottle?");
            total22.setText("Gellir lleihau'r cyn \n lleied o gynnyrch i -");

            multi_title.setText("Cyfrifiannell Lluosog");
            single_title.setText("Cyfrifiannell Potel Sengl");

            calc.setText("Cyfrifo");
            reset.setText("Ail gychwyn");
            calc2.setText("Cyfrifo");
            reset2.setText("Ail gychwyn");
            }
        });

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

            abv1.setText("abv % per can/bottle?");
            ml1.setText("ml per can/bottle?");
            quantity1.setText("Quantity in full pack?");
            removed1.setText("Quantity removed \n from pack?");
            total1.setText("Minimum product can \n be reduced to - ");

            abv2.setText("abv % per can/bottle?");
            ml2.setText("ml per can/bottle?");
            total22.setText("Minimum product can \n be reduced to - ");

            calc.setText("Calculation");
            reset.setText("Reset");
            calc2.setText("Calculation");
            reset2.setText("Reset");
            multi_title.setText("Multipack Calculator");
            single_title.setText("Single Bottle Calculator");

        }
    });
}
}

Or You have to implement View.OnclickListener and assign check id of clicked View.


推荐阅读