简体   繁体   English

当按钮具有特定的当前文本时,如何更改按钮上的文本(Android Studio / Java和XML)

[英]How to change text on button when the button has a particular current text (Android Studio/Java&XML)

So I'm trying to do a very simple thing, and for some reason I can't figure out how to do it. 所以我想做一件非常简单的事情,由于某种原因,我无法弄清楚如何去做。 I've tried searching online but keep coming to resources that don't quite answer my question. 我尝试过在网上搜索,但继续寻找那些不能完全回答我的问题的资源。 How do you change the text on a button, not if that button is pressed, but if any button is pressed and the text on the pressed button equals some value? 如何更改按钮上的文本,而不是按下该按钮,但是如果按下任何按钮并且按下的按钮上的文本等于某个值? All my strings are saved in a strings.xml file and I'm just referencing them in my fragment_main.xml (using, for example, android:text="@string/firstoption" , but I can hardcode the strings if that's easier. I'm also using fragments, so should I put the onClick method in MainActivityFragment.java or MainActivity.java? 我的所有字符串都保存在strings.xml文件中,我只是在我的fragment_main.xml中引用它们(例如,使用android:text =“@ string / firstoption”,但如果更容易,我可以对字符串进行硬编码。我也在使用片段,所以我应该将onClick方法放在MainActivityFragment.java或MainActivity.java中吗?

Try to create 2 div for your button. 尝试为您的按钮创建2个div。 Show and hide the div on button click event. 在按钮单击事件上显示和隐藏div。 Use #divid to get the div. 使用#divid来获取div。 alternatively button text should be a dynamic value which should change on any button click on page ur on. 或者按钮文本应该是一个动态值,应该在页面上的任何按钮点击时更改。

With the below code, you will make a generic OnClickListener "buttonClickListener". 使用下面的代码,您将创建一个通用的OnClickListener“buttonClickListener”。 You can then add the same click listener to all of the buttons that you want to listen too. 然后,您可以将相同的单击侦听器添加到您想要侦听的所有按钮。 In this case, button1,2, and 3. When you click on either of those buttons, the buttonClickListener will be invoked. 在这种情况下,按钮1,2和3.当您单击其中任何一个按钮时,将调用buttonClickListener。 The View v paramater will be the reference of the view that triggered this event, in our case, whichever button was pressed. View v paramater将是触发此事件的视图的参考,在我们的示例中,无论按下哪个按钮。 You can then cast v to type Button and check the text of the button that was clicked. 然后,您可以将v转换为键入Button并检查单击的按钮的文本。 If it is a certain value, you can set the text of the desired button, in this case, buttonToSetText button. 如果是某个值,则可以设置所需按钮的文本,在本例中为buttonToSetText按钮。

    final OnClickListener buttonClickListener = new OnClickListener() {
            public void onClick(final View v) {
                Button b = (Button)v;
                if(b.getText().toString().equals("something")){
                   buttonToSetText.setText("value to set");
                }               
            }
        };
        Button buttonToSetText = (Button) findViewById(R.id.buttonToSetText);

        Button button1 = (Button) findViewById(R.id.button1);
        Button button2 = (Button) findViewById(R.id.button2);
        Button button3 = (Button) findViewById(R.id.button3);

        button1.setOnClickListener(buttonClickListener);
        button2.setOnClickListener(buttonClickListener);
        button3.setOnClickListener(buttonClickListener);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM