Android 切换按钮示例
我正在使用切换按钮开发应用程序,我在Android 切换按钮示例,android,togglebutton,Android,Togglebutton,我正在使用切换按钮开发应用程序,我在EditText中输入了1或0。单击按钮时,如果输入1,切换按钮必须更改。如果输入0,切换按钮必须显示切换打开。单击按钮时无法获取切换值 我的代码是: public class MainActivity extends Activity { String editString=""; Button btn; EditText ed; ToggleButton toggle; @Override protected void onCr
EditText
中输入了1或0。单击按钮时,如果输入1,切换按钮必须更改。如果输入0,切换按钮必须显示<代码>切换打开代码>。单击按钮时无法获取切换值
我的代码是:
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");
}
}
});
}
}
<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"
/>
xml文件:
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");
}
}
});
}
}
<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"
/>
更新:请查看此处的材料成分 这里有一个切换按钮的部分,应该使用它来代替下面给出的答案 旧答案(不再推荐) 移动这个
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
editString = ed.getText().toString();
内部onClick
您还可以更改toogle按钮的状态,无论是0
还是1
例如:
<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>
折断
我认为,当1是其中一个选项被选中,而0是另一个选项时,尝试的内容在语义上与单选按钮相同 我建议默认使用Android提供的单选按钮 下面是如何使用它- android文档在这里-
谢谢。只需删除行
toggle.toggle()代码>从您的单击侦听器toggle()
方法将始终重置您的切换按钮值
当您尝试在字符串变量中获取EditText
的值时,该变量始终保持与在onCreate()
中获取值相同,因此最好直接使用EditText
在onClick
侦听器中获取该变量的值
只需更改代码,如下所示,即可正常工作
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);
}
}
});
试试这个切换按钮
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"/>
你应该遵循谷歌指南
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
}
}
});
您可以查看文档谢谢,但当在Word上第二次单击按钮和每次更改切换按钮(单击按钮时在编辑文本中不输入值)时,文档都会更改谢谢,但当在Word上第二次单击按钮和每次更改切换按钮(单击时在编辑文本中不输入值)时,文档都会更改button@user3081942我不明白你的意思comment@user3081942您的问题是您更改了toogle buttom的状态,即1
或0
。所以它总是会改变世界state@user3081942检查编辑的文章中的开关和切换button@user3081942只需更改此代码。并将注释放在toggle.toggle()上;这是最好的答案。
<ToggleButton
android:id="@+id/togglebutton"
android:layout_width="100px"
android:layout_height="50px"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:onClick="toggleclick"/>
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();
}
}
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
}
}
});