我正在使用切換按鈕開發應用程序,我在EditText
中輸入了1或0。當點擊按鈕時,如果我輸入1,切換按鈕必須改變,切換按鈕顯示TOGGLE ON
,如果我輸入0,切換按鈕必須顯示TOGGLE OFF
。點擊按鈕時,我無法獲得切換值。ToggleButton上的示例
我的代碼是:
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