简体   繁体   English

android切换按钮状态设置texview文本

[英]android toggle button state set texview text

i have a toggle button which when set to the on position sets the hint to one of my textview to 我有一个切换按钮,当设置为打开位置时,将提示设置为我的textview之一

"kg" “公斤”

. the initial hint of the text view is 文本视图的初始提示是

"st" “ st”

which should be shown if the toggle is in the off position. 如果切换开关处于关闭位置,则应显示该信息。

when i first start the app the textview dispalys 当我第一次启动该应用程序时,textview消失

"st" “ st”

(which at first is ok as the toggle is in the off position) now when i press the toggle it turns to the on position and displays (首先,因为拨动开关处于关闭位置,这是可以的)现在当我按拨动开关时,它会转到打开位置并显示

"kg" “公斤”

in the textView (this is also ok.) 在textView中(也可以)。

now comes the problem. 现在出现了问题。 if i click on the toggle again (off position) the textView stays as 如果我再次单击切换(关闭位置),则textView保持为

"kg" “公斤”

does anyone know how i could set it to always display "st in the off state and "kg" in the on state. 有谁知道我如何将其设置为始终在关闭状态下显示“ st”而在打开状态下始终显示“ kg”。

many thanks in advance 提前谢谢了

addListenerOnButton();

      }

      public void addListenerOnButton() {

        unitToggle = (Button) findViewById(R.id.unitToggle);
       final TextView tw1 = (TextView)findViewById(R.id.tw1);


        unitToggle.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {

               StringBuffer result = new StringBuffer();
               tw1.setHint("kg");
 unitToggle.setOnClickListener(new OnClickListener() {

        public void onClick(View v) {

           StringBuffer result = new StringBuffer();
           if(tw1.getHint().toString().equals("kg"))
                tw1.setHint("st");             
           else
                tw1.setHint("kg");

The main reason for the said problem is the logic which has not yet been implemented. 出现上述问题的主要原因是尚未实施的逻辑。

When you click the button for the first time it sets the text to "kg" which it will set always on any number of click. 首次单击该按钮时,它将文本设置为“ kg”,它将始终设置为任意点击次数。 since you have written the statement 因为您已经写了声明

tw1.setHint("kg"); tw1.setHint(“ kg”);

inside your onClick() method without keeping the state of the button. 在您的onClick()方法中而不保留按钮的状态。 emphasized text . 强调文字

In order to make it correct use a boolean flag and change its state on each click and set the text based on the flag value. 为了使其正确,请使用布尔标志,并在每次单击时更改其状态,并根据标志值设置文本。

The best way to do it is to use ToggleButton which has the inbuilt on/off states so you don't need to have your on boolean flag and set the hint based on the button state. 最好的方法是使用ToggleButton,它具有内置的开/关状态,因此您无需具有on boolean标志并根据按钮状态设置提示。

Try 尝试

private boolean on=false;

 unitToggle.setOnClickListener(new OnClickListener() {

        public void onClick(View v) {

           StringBuffer result = new StringBuffer();
           if(on){
                tw1.setHint("kg");
                on = true;
           }else{
               tw1.setHint("st");
               on = false;
           }

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

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