Dur*_*rga 6 android togglebutton
我正在使用切换按钮开发应用程序,我输入了1或0 EditText.单击按钮时,如果我输入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");
}
}
});
}
}
Run Code Online (Sandbox Code Playgroud)
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"
/>
Run Code Online (Sandbox Code Playgroud)
Jog*_*uda 16
<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"/>
Run Code Online (Sandbox Code Playgroud)
在承载此布局的Activity中,以下方法处理click事件:
public void onToggleClicked(View view) {
// Is the toggle on?
boolean on = ((ToggleButton) view).isChecked();
if (on) {
// Enable vibrate
} else {
// Disable vibrate
}
}
Run Code Online (Sandbox Code Playgroud)
GrI*_*sHu 14
只需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);
}
}
});
Run Code Online (Sandbox Code Playgroud)
你应该遵循谷歌指南;
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
}
}
});
Run Code Online (Sandbox Code Playgroud)
您可以在此处查看文档
| 归档时间: |
|
| 查看次数: |
90646 次 |
| 最近记录: |