Java 将字符串值转换为按钮名称

Java 将字符串值转换为按钮名称,java,android,Java,Android,在VB中,使用CType将字符串转换为控件名 Dim btn1 as Button Dim Str as String ="btn1" CType((Str), Button).Text ="your text" 如何在java中实现同样的功能 如果您的意思是设置按钮显示的文本,则类似于:- Button btn1 = this.findViewById(R.id.done); // Gets the Button as per the id in the layout. bt

在VB中,使用CType将字符串转换为控件名

Dim btn1 as Button
Dim Str as String ="btn1"
CType((Str), Button).Text ="your text"

如何在java中实现同样的功能

如果您的意思是设置按钮显示的文本,则类似于:-

    Button btn1 = this.findViewById(R.id.done); // Gets the Button as per the id in the layout.
    btn1.setText("Your Text");
  • 在一些旧版本的Android Studio中,您必须专门从findViewById强制转换类型,因此上面使用
    Button btn1=(Button)this.findViewById(R.id.done)
补充意见:-
我有多达20个按钮btn1………btn20,然后我想使用循环 参考此按钮,这意味着我需要btn的字符串和 整数表示数字。将字符串和整数连接在一起将得到 请告诉我实际的按钮名称

我相信以下可能是你想要的。这将创建20个按钮,并将它们添加到布局中

每个将设置两个字段/成员。 -文本(按钮显示的文本)字段将设置为我的按钮#?(其中?将为1-20) -标签(可根据需要使用的字段)将设置为一个字符串,该字符串是ArrayList中各个按钮的偏移量(即0-19)

每个按钮都会添加一个onClickListener,它将发出一个Toast,详细说明文本和标记的按钮值

此外,通过根据按钮的文本值或标记值搜索按钮,可以实现对按钮的引用。给出了搜索0的两个示例,首先在标记值中(将找到0),然后在文本值中(将找不到)。结果将写入日志

public class OtherActivity extends AppCompatActivity {

    ArrayList<Button> mButtons = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_other);
        LinearLayout mMainLayout = this.findViewById(R.id.mainlayout);

        // Add the 20 buttons dynamically assigning Text and a Tag
        for (int i=0; i < 20; i++) {
            Button current_button = new Button(this);
            current_button.setText("My Button #" + String.valueOf(i + 1));
            current_button.setTag(String.valueOf(i));
            mButtons.add(current_button);
            mMainLayout.addView(current_button);
            current_button.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Toast.makeText(
                            v.getContext(),
                            "You CLicked on the Buttton with Text as :-" + ((Button) v).getText().toString() + " Tag as " + ((Button)v).getTag(),
                            Toast.LENGTH_SHORT
                    ).show();
                }
            });
        }

        // Search for the button according to some text    
        // Should work as Tag has been set to 0
        Button button_to_use = findButtonByTagOrText("0",true,mButtons);
        if (button_to_use != null) {
            Log.d("BUTTONFINDRESULT","Button 0 Found according to the Tag");
        } else {
            Log.d("BUTTONFINDRESULT","Button 0 NOT FOUND acording to Tag");
        }

        //Will not find button as text will not be o but will instead be My Button #1 (for the first button)
        button_to_use = findButtonByTagOrText("0", false,mButtons);
        if (button_to_use != null) {
            Log.d("BUTTONFINDRESULT","Button 0 Found according to the Text");
        } else {
            Log.d("BUTTONFINDRESULT","Button 0 NOT FOUND acording to Text");
        }
    }

    /**
     * Find the button according to it's TAG or Text
     * @param value_to_find     The String to find
     * @param look_for_tag      true if to look at the Tag, false if to look at the Text
     * @param button_list       The ArrayList<Button> aka the list of buttons
     * @return                  null if not found, else the Button object
     */
    private Button findButtonByTagOrText(String value_to_find, boolean look_for_tag, ArrayList<Button> button_list) {
        Button rv = null;
        String compare_string;
        for (Button b: button_list) {
            if (look_for_tag) {
                compare_string = (String) b.getTag();
            } else {
                compare_string = b.getText().toString();
            }
            if (value_to_find.equals(compare_string)) {
                return b;
            }
        }
        return rv; // Note returned button will be null if text not found.
    }
}

如果您的意思是设置按钮显示的文本,则类似于:-

    Button btn1 = this.findViewById(R.id.done); // Gets the Button as per the id in the layout.
    btn1.setText("Your Text");
  • 在一些旧版本的Android Studio中,您必须专门从findViewById强制转换类型,因此上面使用
    Button btn1=(Button)this.findViewById(R.id.done)
补充意见:-
我有多达20个按钮btn1………btn20,然后我想使用循环 参考此按钮,这意味着我需要btn的字符串和 整数表示数字。将字符串和整数连接在一起将得到 请告诉我实际的按钮名称

我相信以下可能是你想要的。这将创建20个按钮,并将它们添加到布局中

每个将设置两个字段/成员。 -文本(按钮显示的文本)字段将设置为我的按钮#?(其中?将为1-20) -标签(可根据需要使用的字段)将设置为一个字符串,该字符串是ArrayList中各个按钮的偏移量(即0-19)

每个按钮都会添加一个onClickListener,它将发出一个Toast,详细说明文本和标记的按钮值

此外,通过根据按钮的文本值或标记值搜索按钮,可以实现对按钮的引用。给出了搜索0的两个示例,首先在标记值中(将找到0),然后在文本值中(将找不到)。结果将写入日志

public class OtherActivity extends AppCompatActivity {

    ArrayList<Button> mButtons = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_other);
        LinearLayout mMainLayout = this.findViewById(R.id.mainlayout);

        // Add the 20 buttons dynamically assigning Text and a Tag
        for (int i=0; i < 20; i++) {
            Button current_button = new Button(this);
            current_button.setText("My Button #" + String.valueOf(i + 1));
            current_button.setTag(String.valueOf(i));
            mButtons.add(current_button);
            mMainLayout.addView(current_button);
            current_button.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Toast.makeText(
                            v.getContext(),
                            "You CLicked on the Buttton with Text as :-" + ((Button) v).getText().toString() + " Tag as " + ((Button)v).getTag(),
                            Toast.LENGTH_SHORT
                    ).show();
                }
            });
        }

        // Search for the button according to some text    
        // Should work as Tag has been set to 0
        Button button_to_use = findButtonByTagOrText("0",true,mButtons);
        if (button_to_use != null) {
            Log.d("BUTTONFINDRESULT","Button 0 Found according to the Tag");
        } else {
            Log.d("BUTTONFINDRESULT","Button 0 NOT FOUND acording to Tag");
        }

        //Will not find button as text will not be o but will instead be My Button #1 (for the first button)
        button_to_use = findButtonByTagOrText("0", false,mButtons);
        if (button_to_use != null) {
            Log.d("BUTTONFINDRESULT","Button 0 Found according to the Text");
        } else {
            Log.d("BUTTONFINDRESULT","Button 0 NOT FOUND acording to Text");
        }
    }

    /**
     * Find the button according to it's TAG or Text
     * @param value_to_find     The String to find
     * @param look_for_tag      true if to look at the Tag, false if to look at the Text
     * @param button_list       The ArrayList<Button> aka the list of buttons
     * @return                  null if not found, else the Button object
     */
    private Button findButtonByTagOrText(String value_to_find, boolean look_for_tag, ArrayList<Button> button_list) {
        Button rv = null;
        String compare_string;
        for (Button b: button_list) {
            if (look_for_tag) {
                compare_string = (String) b.getTag();
            } else {
                compare_string = b.getText().toString();
            }
            if (value_to_find.equals(compare_string)) {
                return b;
            }
        }
        return rv; // Note returned button will be null if text not found.
    }
}

如果您有ID(名称)为的按钮,如
btn1
btn2
,…,
btn20
,并且您希望通过for循环设置它们的文本,您可以这样使用:

for (int i = 1; i <= 20; i++) {
    int id = getResources().getIdentifier("btn" + i, "id", getPackageName());
    Button button = findViewById(id);
    button.setText("something" + i);
}
int id = context.getResources().getIdentifier("btn" + i, "id", context.getPackageName());

如果您有ID(名称)为的按钮,如
btn1
btn2
,…,
btn20
,并且您希望通过for循环设置它们的文本,您可以这样使用:

for (int i = 1; i <= 20; i++) {
    int id = getResources().getIdentifier("btn" + i, "id", getPackageName());
    Button button = findViewById(id);
    button.setText("something" + i);
}
int id = context.getResources().getIdentifier("btn" + i, "id", context.getPackageName());

你好,罗伯托,谢谢你的邀请,欢迎来到SO。你的问题很笼统,很难回答。你能提供一个你试过的Java代码的例子吗?哪些代码不起作用?我甚至不知道该试哪一个,因为在谷歌看到的和我想要的都不一样。我的目标是从一个字符串中引用一个按钮,因为它的位置太宽,可能会被标记和关闭。另外,请进行编辑,将“归档”一词改为“实现”。你让我一时搞不明白“用java存档”是什么意思。你好,Roberto,谢谢你的提问,欢迎来到SO。你的问题很笼统,很难回答。你能提供一个你试过的Java代码的例子吗?哪些代码不起作用?我甚至不知道该试哪一个,因为在谷歌看到的和我想要的都不一样。我的目标是从一个字符串中引用一个按钮,因为它的位置太宽,可能会被标记和关闭。另外,请进行编辑,将“归档”一词改为“实现”。你让我一时糊涂了“用java存档”是什么意思。我有多达20个按钮btn1………btn20,然后我想使用循环引用这些按钮,这意味着我需要一个字符串作为btn,一个整数作为数字。连接字符串和整数将给出实际的按钮名称。@Roberto这不是你问的问题,这是另一个不同的问题。我最多有20个按钮btn1………btn20,然后我想使用循环引用此按钮,这意味着我需要一个字符串作为btn,一个整数作为数字。连接字符串和整数将得到实际的按钮名。@Roberto这不是你问的问题,这是另一个不同的问题。这正是我要找的。这是迄今为止解决我问题的最好办法。非常感谢你的帮助。这正是我想要的。这是迄今为止解决我问题的最好办法。非常感谢你帮助我。