2016-04-26 16 views
0

アクティビティ内に小さなポップアップオプションメニューを作成します。私はすでにメニューを示し、このコード状態変更時にスイッチボタン(AlertDialogにあります)を何かします

public void ShowOptionsDialog() 
    { 
     Android.Support.V7.App.AlertDialog.Builder optionDialog = new Android.Support.V7.App.AlertDialog.Builder(this); 
     optionDialog.SetTitle("Optionen");    
     optionDialog.SetView(Resource.Layout.Options); 
     optionDialog.Show();    
    } 

Resource.Layout.Optionsは、次のものが含まれています:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:gravity="center"> 
    <android.support.v7.widget.SwitchCompat 
     android:id="@+id/previewSwitch" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:checked="true" 
     android:hint="Preview der Dokumente anzeigen"/> 
</LinearLayout> 

どのように正確に私はスイッチが押されたときに何かをするアプリを教えてください(オンオフ)?

答えて

2

あなたはユーザーが行うすべての変更にSwitchCompatや行動にイベントハンドラを設定することができます。

注:v7のスイッチはAndroid.Resource.Id.Customが含まれていないため、FindViewByIdからnullを返すので、ここで私達はFrameLayout私たち自身が作成されます:

protected void ShowOptionsDialog() 
{ 
    Android.Support.V7.App.AlertDialog.Builder optionDialog = new Android.Support.V7.App.AlertDialog.Builder(this); 
    optionDialog.SetTitle("Optionen"); 
    // Android.Resource.Id.Custom does not exist within v7 alertdialog 
    var frameLayout = new FrameLayout (optionDialog.Context); 
    optionDialog.SetView(frameLayout); 
    Android.Support.V7.App.AlertDialog alert = optionDialog.Create(); 
    var myView = alert.LayoutInflater.Inflate (Resource.Layout.Options, frameLayout); 
    var mySwitch = myView.FindViewById<Android.Support.V7.Widget.SwitchCompat> (Resource.Id.previewSwitch); 
    mySwitch.CheckedChange += (object sender, CompoundButton.CheckedChangeEventArgs e) => { 
     System.Diagnostics.Debug.WriteLine(e.IsChecked); 
    }; 
    alert.Show(); 
} 

enter image description here

注: follを行うにはAndroidのドキュメントの状態スイッチにカスタムビューを追加したため、問題はSwitchCompatであり、そのビューは存在しません。

FrameLayout fl = (FrameLayout) findViewById(android.R.id.custom); 
fl.addView(myView, new LayoutParams(MATCH_PARENT, WRAP_CONTENT)); 

http://developer.android.com/reference/android/app/AlertDialog.html

+0

あなたは高速です。 ;-) –

+0

既に提案したものに似たものを試しましたが、コードを使用するときに同じエラーが発生します: "System.NullReferenceException:オブジェクト参照がオブジェクトのインスタンスに設定されていません" しかし、 .. – fbueckle

+0

このエラーが発生しました: Java.Lang.NullPointerException:ヌルオブジェクトリファレンスでインターフェイスメソッド 'int java.lang.CharSequence.length()'を呼び出そうとしました :/ – fbueckle

-1

あなたのスイッチへの参照を取得する必要があります。簡単な方法は、コードでスイッチビューを作成することです。これにより、コードが次のように変更されます。

public void ShowOptionsDialog() 
{ 
    var switchView = new Android.Support.V7.Widget.SwitchCompat(this); 
    switchView.CheckedChange += (object sender, CompoundButton.CheckedChangeEventArgs e) => { //Do stuff depending on state }; 

    Android.Support.V7.App.AlertDialog.Builder optionDialog = new Android.Support.V7.App.AlertDialog.Builder(this); 
    optionDialog.SetTitle("Optionen");    
    optionDialog.SetView(switchCompat); 
    optionDialog.Show();    
} 
関連する問題