如何将 Android 中的按钮文本更改为已编码的字符串?
How do you change the text of a button in Android to a String that you have coded?
我创建了一个程序,它生成操作并为您提供 select 的选项,其中之一是所提供操作的正确答案。我正在尝试弄清楚如何将程序创建的字符串应用于实际应用程序中的按钮和文本。
这是代码示例:
public static double value, falsevalue1, falsevalue2, falsevalue3;
public static int xy;
public static int randomchoicea, randomchoiceb, randomchoicec;
public static String optionA, optionB, optionC, optionD, operation;
public static void randomOperation() {
double x = (int)(Math.random() * (4 - 1 + 1)) + 1;
if (x == 1) {
int a = (int) getInt100to5000(); int b = (int) getInt100to5000();
operation = a + " + " + b;
value = a + b;
我将如何将选项字符串和操作字符串应用于它们各自的按钮和 TextView?我希望这些信息足以提供帮助。谢谢
您需要将 onCLick 侦听器添加到以这种方式将文本设置为 TextView 的按钮 textView.setText()。
button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
textView.setText(text);
}
});
您需要通过 findViewById() 获取 Button 和 Textviews 视图,然后为其应用函数,例如使用 Button.setOnClickListener() 进行点击操作,使用 TextView.setText() 更改文本该文本视图或应用您的代码结果。有 reference 供您实施。
我想我知道您想要实现什么,为了实现它,您需要创建 4 个要将文本更改为的按钮,以及您要修改的文本视图。
之后有必要实例化这些元素,这样您就可以访问视图并对其进行修改,这是我要解释的一个示例,想象一下 optionA 按钮是具有正确答案的按钮,这是您获取值的方式没关系,你可以保持原样:
//This are declarations
String optionAString;
double value;
Button optionAButton;
TextView example;
那些不是声明,而是实例化:
//After calculating the values you can do this in onCreate() or OnCreateView, depending of where are you using it, activity, Fragment, etc:
optionAButton = findViewById(R.id.option_a_button);
example = findViewById(R.id.text_view_example);
//this if for converting the doubles to Strings, them show the values
optionAButton.setText(String.valueOf(value));
example.setText(String.valueOf(a));
完成所有这些之后,您所要做的就是在您的按钮中设置监听器并使用它做一些事情
我创建了一个程序,它生成操作并为您提供 select 的选项,其中之一是所提供操作的正确答案。我正在尝试弄清楚如何将程序创建的字符串应用于实际应用程序中的按钮和文本。
这是代码示例:
public static double value, falsevalue1, falsevalue2, falsevalue3;
public static int xy;
public static int randomchoicea, randomchoiceb, randomchoicec;
public static String optionA, optionB, optionC, optionD, operation;
public static void randomOperation() {
double x = (int)(Math.random() * (4 - 1 + 1)) + 1;
if (x == 1) {
int a = (int) getInt100to5000(); int b = (int) getInt100to5000();
operation = a + " + " + b;
value = a + b;
我将如何将选项字符串和操作字符串应用于它们各自的按钮和 TextView?我希望这些信息足以提供帮助。谢谢
您需要将 onCLick 侦听器添加到以这种方式将文本设置为 TextView 的按钮 textView.setText()。
button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
textView.setText(text);
}
});
您需要通过 findViewById() 获取 Button 和 Textviews 视图,然后为其应用函数,例如使用 Button.setOnClickListener() 进行点击操作,使用 TextView.setText() 更改文本该文本视图或应用您的代码结果。有 reference 供您实施。
我想我知道您想要实现什么,为了实现它,您需要创建 4 个要将文本更改为的按钮,以及您要修改的文本视图。 之后有必要实例化这些元素,这样您就可以访问视图并对其进行修改,这是我要解释的一个示例,想象一下 optionA 按钮是具有正确答案的按钮,这是您获取值的方式没关系,你可以保持原样:
//This are declarations
String optionAString;
double value;
Button optionAButton;
TextView example;
那些不是声明,而是实例化:
//After calculating the values you can do this in onCreate() or OnCreateView, depending of where are you using it, activity, Fragment, etc:
optionAButton = findViewById(R.id.option_a_button);
example = findViewById(R.id.text_view_example);
//this if for converting the doubles to Strings, them show the values
optionAButton.setText(String.valueOf(value));
example.setText(String.valueOf(a));
完成所有这些之后,您所要做的就是在您的按钮中设置监听器并使用它做一些事情