2014-01-23 39 views
1

我正在嘗試構建我的第一個Android應用程序,並且我已經被卡住了。我有兩個切換開關,當它們打開時,會出現一個對話窗口。我想要「取消」按鈕來關閉開關。我試過toggleButton.setChecked(false),Switch.setChecked(false)等等,但是因爲這些開關是在XML文件中創建的,所以沒有對象在/上執行該方法。如何在程序中切換這些開關?我在我的主要活動中有我的onClick監聽器,並將對話框創建爲不同的類。這可能是錯誤的,但它到目前爲止都是有效的。在Android應用程序中切換基於關閉對話框按鈕選項的開關

MainActivity.java:

package com.example.arduinoautomation; 

import android.app.Activity; 
import android.os.Bundle; 
import android.view.Menu; 
import android.view.View; 
import android.widget.Switch; 

public class MainActivity extends Activity { 


    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
    } 


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

     if (on) { 
      lampOnDialog lamp_on = new lampOnDialog(); 
      lamp_on.message = "Lamp is on."; 
      lamp_on.show(this.getFragmentManager(),"switch"); 
     } else { 
      // Disable vibrate 
     } 
    } 

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

     if (on) { 
      lampOnDialog light_on = new lampOnDialog(); 
      light_on.message = "Light is on"; 
      light_on.show(this.getFragmentManager(), "switch"); 
     } else { 
      // Disable vibrate 
     } 
    } 


    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.main, menu); 
     return true; 
    } 

} 

lampOnDialog.java:

package com.example.arduinoautomation; 

import android.app.AlertDialog; 
import android.app.Dialog; 
import android.app.DialogFragment; 
import android.content.DialogInterface; 
import android.os.Bundle; 

public class lampOnDialog extends DialogFragment { 
    String message = ""; 
    @Override 
    public Dialog onCreateDialog(Bundle savedInstanceState) { 

     // Use the Builder class for convenient dialog construction 
     AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); 
     builder.setMessage(message) 
       .setPositiveButton("OK", new DialogInterface.OnClickListener() { 
        public void onClick(DialogInterface dialog, int id) { 
         // FIRE ZE MISSILES! 
        } 
       }) 
       .setNegativeButton("Cancel", new DialogInterface.OnClickListener() { 
        public void onClick(DialogInterface dialog, int id) { 
         // User cancelled the dialog 

        } 
       }); 
     // Create the AlertDialog object and return it 
     return builder.create(); 
    } 
} 

activity_main.xml中

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

    <Switch 
     android:id="@+id/switch1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentTop="true" 
     android:layout_marginTop="57dp" 
     android:onClick="lampToggle" 
     android:text="Lamp" /> 

    <Switch 
     android:id="@+id/switch2" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentLeft="true" 
     android:layout_below="@+id/switch1" 
     android:layout_marginTop="49dp" 
     android:onClick="lightToggle" 
     android:text="Lights" /> 

</RelativeLayout> 

回答

1

你必須定義切換按鈕爲您的活動視圖:

ToggleButton toggle; 

然後實例化,通常在你onCreate方法:

toggle = (ToggleButton) findViewById(R.id.switch1); 

然後,您可以使用setChecked方法對您的視圖在任何地方:

toggle.setChecked(false); 

編輯

您無法訪問視圖,因爲您的對話框是另一個類,切換視圖位於您的活動類中。嘗試創建活動類中的對話框:

public void showDialog(String message) { 
    AlertDialog.Builder builder = new AlertDialog.Builder(this); 
    builder.setMessage(message).setPositiveButton("Yes, you will", new DialogInterface.OnClickListener() { 
     @Override 
     public void onClick(DialogInterface dialog, int which) { 
      toggle.setChecked(true); 
     } 
    }).setNegativeButton("No, you won't", new DialogInterface.OnClickListener() { 

     @Override 
     public void onClick(DialogInterface dialog, int which) { 
      toggle.setChecked(false); 
     } 
    }).show(); 
} 

,然後致電showDialog()方法從您的活動的任何地方顯示一個對話框:

showDialog("Hi, I'll be your dialog today"); 
+0

我能夠做的第一兩條線沒有問題,但是當我嘗試在AlertDialog的「取消」按鈕的onClick方法中將其設置爲false時,它告訴我沒有變量「切換」。 –

+0

請看我更新的答案。我不確定它是否是最佳解決方案,但它應該可行。答案中的所有代碼應該在同一個Activity中,因此可以放棄LampOnDialog類。 – Plasma

相關問題