android - 单击不同的按钮时如何更改按钮文本并使其功能相同?安卓

标签 android android-studio button replace textbox

前几天我问了这个问题并得到了一些答案,但我无法实现它们。

我已经设置了一个按钮来将语言从英语更改为威尔士语,只有大约 5 行英语,所以只需使用 .setText 手动更改每个框中的文本(也不支持威尔士语)。

我还需要更改按钮上的文本,这可行,但是一旦我更改了语言,其他按钮就无法正常工作。

一旦按下英语或威尔士语按钮(更改按钮的语言),calc、reset、calc2 和 reset2 按钮将停止执行它们设置的功能...但在按下两个按钮之前它们会工作.

这仍然是我的第一个应用程序,如果它很困惑,我深表歉意,我将包含所有代码,希望有人能看到我哪里出错了。

有问题的代码就在底部,英语和威尔士语按钮。

谢谢

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");

        }
    });






}
}

这是我最后一次问到的答案在下面,看起来不错,我在代码的不同地方尝试过,但总是出现某种形式或错误,原因可能是我如何列出了我的其余代码?

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);
    }

最佳答案

试试这个代码,我认为问题是重新初始化和 setOnclickListener() 再次

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.

关于android - 单击不同的按钮时如何更改按钮文本并使其功能相同?安卓,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54533243/

相关文章:

java - 声明处理程序错误

android-studio - 如何使用Kotlin安全地强制转换readLine()的结果以防止类型不匹配

Android:更改后退按钮的操作

android - 将旧的 gradle 项目导入新的 android studio 版本

android - 导入并添加为 Android 项目中的模块时的 AAR 文件更新

javascript - 如何使用相同的弹出按钮在新选项卡中打开另一个链接

javascript - 永久更改具有功能的按钮(正常+按下)的图像

android - 如何在 ListView 中单击特定项目时从数据库中获取特定列表

java - Android 中的时间控件

android - gradle build:任务 ':app:validateSigningAutomationRelease'的执行失败