问题描述
我正在使用切换按钮开发应用程序,我在 EditText
中输入了 1 或 0.单击按钮时,切换按钮必须更改,如果我输入 1,切换按钮显示 TOGGLE ON
,如果我输入 0,切换按钮必须显示 TOGGLE OFF
.单击按钮时,我无法获得切换值.
I am developing an application using a toggle button, I entered 1 or 0 in EditText
. When button is clicked, the toggle button has to change if I enter 1 the toggle button shows TOGGLE ON
, if I enter 0 the toggle button has to show TOGGLE OFF
. I am unable to get toggle values when the button is clicked.
我的代码是:
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"
/>
推荐答案
只需从你的点击监听器中移除 toggle.toggle();
行 toggle()
方法将始终重置您的切换按钮值.
Just remove the line toggle.toggle();
from your click listener toggle()
method will always reset your toggle button value.
并且当您尝试在字符串变量中获取 EditText
的值时,该值始终与您在 onCreate()
中获取值相同,因此最好直接使用 EditText
以在您的 onClick
侦听器中获取它的值.
And as you are trying to take the value of EditText
in string variable which always remains same as you are getting value in onCreate()
so better directly use the EditText
to get the value of it in your onClick
listener.
只需更改您的代码,如下所示,现在可以正常工作了.
Just change your code as below its working fine now.
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);
}
}
});
这篇关于ToggleButton 示例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!