ToggleButton示例

10

我正在开发一个应用程序,使用切换按钮,在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"
         />  


谢谢,但是当按钮第二次被点击时,它会改变文字,并且每次在点击按钮时都会切换按钮,而不需要在编辑文本中输入值。 - Durga
6个回答

17

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

您正在尝试将EditText的值存储在字符串变量中,但它始终保持不变,因为您在onCreate()中获取值,因此最好直接在您的onClick监听器中使用EditText来获取其值。

只需按照以下方式更改您的代码,它现在可以正常工作。

  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);

            }
        }
    });

5

您应遵循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
        }
    }
});

你可以在这里查看文档 意思是:在该链接中,您可以查阅相关文档,了解有关开关按钮的更多信息。

3

尝试使用切换按钮

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();
    }
}

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);

    }
        
    }
     }

Snaps

enter image description here

enter image description here


谢谢,但是当按钮第二次被点击时,它改变了单词,并且每次在点击按钮时都会更改切换按钮,而不需要在编辑文本中输入值。 - Durga
@user3081942,我不理解你的评论。 - Raghunandan
@user3081942,你的问题在于你改变了切换按钮的状态,无论是1还是0。因此它总是会改变状态。 - Raghunandan
@user3081942,请查看已编辑的帖子,其中包括开关和切换按钮。 - Raghunandan

0

0
@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();上添加注释。 - Harsh Goswami

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接