【问题标题】:Example on ToggleButton切换按钮示例
【发布时间】:2014-02-12 04:33:27
【问题描述】:

我正在使用切换按钮开发应用程序,我在EditText 中输入了 1 或 0。单击按钮时,如果我输入 1,则切换按钮必须更改,切换按钮显示TOGGLE ON,如果我输入 0,切换按钮必须显示TOGGLE OFF。单击按钮时,我无法获取切换值。

我的代码是:

public class MainActivity extends Activity {

  String editString="";

  Button btn;
  EditText ed;
  ToggleButton toggle;
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    btn = (Button)findViewById(R.id.btn);
    ed  = (EditText)findViewById(R.id.ed);
    toggle = (ToggleButton)findViewById(R.id.toggBtn);

    editString = ed.getText().toString();

    btn.setOnClickListener(new OnClickListener() {

      @Override
      public void onClick(View v) {
        // TODO Auto-generated method stub
        toggle.toggle();
        if(editString.equals("1")){

          toggle.setTextOff("TOGGLE ON");

        }
        else if(editString.equals("0")){

          toggle.setTextOn("TOGGLE OFF");

        }
      }
    });
  }
}

xml 文件:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" 
android:orientation="vertical">

 <EditText
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/ed"/>
 <Button  android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:id="@+id/btn"
         android:text="Summit"/>
  <ToggleButton
        android:id="@+id/toggBtn"
        android:layout_below="@+id/text6"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"

        android:gravity="center"
         />  

【问题讨论】:

  • 谢谢,但是当按钮第二次点击单词时它发生了变化,每次点击按钮时更改切换按钮而不在编辑文本中输入值

标签: android togglebutton


【解决方案1】:

更新:请在此处查看材料组件https://material.io/components/buttons/android#toggle-button 有一个切换按钮部分应该用来代替下面过时的答案。

旧答案(不再推荐)

移动这个

 btn.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View v) {
     editString = ed.getText().toString();

onClick

您还可以更改工具按钮的状态,无论是0 还是1

http://developer.android.com/guide/topics/ui/controls/togglebutton.html

例子:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="20dp"
        android:text="Button" />

    <EditText
        android:id="@+id/editText1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="26dp"
        android:ems="10" >

        <requestFocus />
    </EditText>

    <Switch
        android:id="@+id/switch1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignRight="@+id/editText1"
        android:layout_below="@+id/editText1"
        android:layout_marginTop="51dp"
        android:text="Switch" />

    <ToggleButton
        android:id="@+id/togglebutton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/button1"
        android:layout_below="@+id/switch1"
        android:layout_marginTop="58dp"
        android:onClick="onToggleClicked"
        android:textOff="Vibrate off"
        android:textOn="Vibrate on" />

</RelativeLayout>

MainActivity.java

public class MainActivity extends Activity implements OnClickListener {

    EditText ed;
    Switch sb;
    ToggleButton tb;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ed = (EditText) findViewById(R.id.editText1);
        Button b = (Button) findViewById(R.id.button1);
        sb = (Switch)findViewById(R.id.switch1);
        tb = (ToggleButton)findViewById(R.id.togglebutton);
        b.setOnClickListener(this);
    }


    @Override
    public void onClick(View v) {
        String s = ed.getText().toString();
         if(s.equals("1")){

             tb.setText("TOGGLE ON");
             tb.setActivated(true);
             sb.setChecked(true);

         }
         else if(s.equals("0")){

             tb.setText("TOGGLE OFF");
             tb.setActivated(false);
             sb.setChecked(false);

    }
        
    }
     }

快照

【讨论】:

  • 谢谢,但是当按钮第二次点击单词时它发生了变化,每次点击按钮时更改切换按钮而不在编辑文本中输入值
  • @user3081942 我不明白你的评论
  • @user3081942 您的问题是您更改了工具按钮的状态,无论是1 还是0。所以它总是改变状态
  • @user3081942 检查编辑后的开关和切换按钮
【解决方案2】:

我认为当 1 是选择其中一个选项而 0 是另一个选项时,我认为在语义上与单选按钮相同。

我建议使用Android默认提供的单选按钮。

这里是如何使用它- http://www.mkyong.com/android/android-radio-buttons-example/

安卓文档在这里-

http://developer.android.com/guide/topics/ui/controls/radiobutton.html

谢谢。

【讨论】:

    【解决方案3】:

    只需从您的点击监听器中删除toggle.toggle();toggle() 方法将始终重置您的切换按钮值。

    当您尝试在字符串变量中获取EditText 的值时,该值始终与您在onCreate() 中获取的值相同,因此最好直接使用EditText 在您的@987654326 中获取它的值@听众。

    现在只需将您的代码更改为如下所示即可。

      btn.setOnClickListener(new OnClickListener() {
    
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                           //toggle.toggle();
                if ( ed.getText().toString().equalsIgnoreCase("1")) {
    
                    toggle.setTextOff("TOGGLE ON");
                    toggle.setChecked(true);
                } else if ( ed.getText().toString().equalsIgnoreCase("0")) {
    
                    toggle.setTextOn("TOGGLE OFF");
                    toggle.setChecked(false);
    
                }
            }
        });
    

    【讨论】:

      【解决方案4】:
      @Override
      public void onClick(View v) {
          // TODO Auto-generated method stub
          editString = ed.getText().toString();
          if(editString.equals("1")){
      
              toggle.setTextOff("TOGGLE ON");
              toggle.setChecked(true);
      
          }
          else if(editString.equals("0")){
      
              toggle.setTextOn("TOGGLE OFF");
              toggle.setChecked(false);
      
          }
      }
      

      【讨论】:

      • @user3081942 只需更改此代码。并在 toggle.toggle(); 上发表评论;
      【解决方案5】:
      <ToggleButton 
          android:id="@+id/togglebutton"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:textOn="Vibrate on"
          android:textOff="Vibrate off"
          android:onClick="onToggleClicked"/>
      

      在承载此布局的 Activity 中,以下方法处理点击事件:

      public void onToggleClicked(View view) {
          // Is the toggle on?
          boolean on = ((ToggleButton) view).isChecked();
      
          if (on) {
              // Enable vibrate
          } else {
              // Disable vibrate
          }
      }
      

      【讨论】:

        【解决方案6】:

        试试这个切换按钮

        test_activity.xml

        <ToggleButton 
        android:id="@+id/togglebutton" 
        android:layout_width="100px" 
        android:layout_height="50px" 
        android:layout_centerVertical="true" 
        android:layout_centerHorizontal="true"
        android:onClick="toggleclick"/>
        

        Test.java

        public class Test extends Activity {
        
        private ToggleButton togglebutton;
        
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
            togglebutton = (ToggleButton) findViewById(R.id.togglebutton);
        }
        
        public void toggleclick(View v){
            if(togglebutton.isChecked())
                Toast.makeText(TestActivity.this, "ON", Toast.LENGTH_SHORT).show();
            else
                Toast.makeText(TestActivity.this, "OFF", Toast.LENGTH_SHORT).show();
            }
        }
        

        【讨论】:

          【解决方案7】:

          您应该遵循 Google 指南;

          ToggleButton toggle = (ToggleButton) findViewById(R.id.togglebutton);
          toggle.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
              public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                  if (isChecked) {
                      // The toggle is enabled
                  } else {
                      // The toggle is disabled
                  }
              }
          });
          

          您可以查看文档here

          【讨论】:

          • 这是最好的答案。
          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2021-07-20
          • 2013-06-20
          • 2011-05-30
          • 2020-07-05
          • 2019-09-28
          • 2019-04-18
          相关资源
          最近更新 更多