2014-01-22 89 views
3

我正在使用切換按鈕開發應用程序,我在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" 
     /> 

+0

謝謝你,但它改變了當按鈕點擊的話,每次點擊無按鈕 – Durga

回答

9

只需從您的點擊監聽器中刪除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); 

      } 
     } 
    }); 
+0

感謝其工作正常 – Durga

0

移動這個

btn.setOnClickListener(new OnClickListener() { 
    @Override 
    public void onClick(View v) { 
    editString = ed.getText().toString(); 

內部onClick

另外更改的toogle按鈕的狀態是否其01

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

    } 

    } 
    } 

捕捉

enter image description here

enter image description here

+0

謝謝當編輯文本輸入值變化的切換按鈕,第二次,但它改變時,按鈕按下第二次在單詞和每次更改切換按鈕時沒有輸入值編輯文本時單擊按鈕 – Durga

+0

@ user3081942我不理解您的評論 – Raghunandan

+0

@ user3081942您的問題是您更改toogle託管的狀態,因爲它的'1'或'0'。所以它總是改變狀態 – Raghunandan

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

    } 
} 
+0

@ user3081942只需更改此代碼。並對toggle.toggle()發表評論; –

11
<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"/> 

在承載此佈局的活動,下面的方法處理點擊事件:

public void onToggleClicked(View view) { 
    // Is the toggle on? 
    boolean on = ((ToggleButton) view).isChecked(); 

    if (on) { 
     // Enable vibrate 
    } else { 
     // Disable vibrate 
    } 
} 
2

試試這個切換按鈕

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