2014-02-20 4 views
0

Мне нужна ваша помощь. в моем диалоговом окне есть 2 радиообмена. Я хочу отключить или включить некоторые виды в этом диалоговом окне, в зависимости от того, какой радиатор проверен пользователем.Как включить отключить диалоговые окна динамически

мой макет для диалога: inc_dialog.xml

<?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="match_parent" 
    android:orientation="vertical" > 

    <EditText 
     android:id="@+id/inc_name" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_marginBottom="4dp" 
     android:layout_marginLeft="4dp" 
     android:layout_marginRight="4dp" 
     android:layout_marginTop="16dp" 
     android:hint="Название дохода" 
     /> 

    <EditText 
     android:id="@+id/inc_value" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_marginBottom="16dp" 
     android:layout_marginLeft="4dp" 
     android:layout_marginRight="4dp" 
     android:layout_marginTop="4dp" 
     android:hint="Сумма дохода" 
     /> 

    <TextView 
     android:id="@+id/tvChkBoxIncType" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_marginBottom="6dp" 
     android:layout_marginLeft="16dp" 
     android:layout_marginRight="4dp" 
     android:layout_marginTop="4dp" 
     android:text="Тип дохода:" /> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" > 

     <RadioGroup 
      android:id="@+id/RadioGroup1" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:orientation="horizontal" > 

      <RadioButton 
       android:id="@+id/inc_random" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_marginLeft="6dp" 
       android:onClick="onRadioButtonClicked" 
       android:text="Разовый" /> 

      <RadioButton 
       android:id="@+id/inc_const" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_marginLeft="9dp" 
       android:checked="true" 
       android:onClick="onRadioButtonClicked" 
       android:text="Постоянный" /> 

     </RadioGroup> 
    </LinearLayout> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" > 

     <TextView 
     android:id="@+id/DataPick1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="top" 
     android:layout_marginLeft="16dp" 
     android:text="Начало периода:" 
     android:enabled="false"/> 

    <EditText 
    android:id="@+id/inc_day" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="DD" 
    android:enabled="false" > 
    </EditText> 

    <EditText 
    android:id="@+id/inc_month" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="MM" 
    android:enabled="false"> 
    </EditText> 

    <EditText 
    android:id="@+id/inc_year" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:enabled="false" 
    android:text="YYYY" > 
    </EditText> 
    </LinearLayout> 

<EditText 
    android:id="@+id/inc_period" 
    android:layout_width="wrap_content" 
    android:enabled="false" 
    android:layout_height="wrap_content" 
    android:layout_marginBottom="4dp" 
    android:layout_marginLeft="4dp" 
    android:layout_marginRight="4dp" 
    android:text="Каждые Х дней" > 

     </EditText> 
</LinearLayout> 

Строительство этого диалога:

protected Dialog onCreateDialog(int id) { 
     AlertDialog.Builder adb = new AlertDialog.Builder(this); 
     adb.setTitle("Добавить доход"); 
     view = (LinearLayout) getLayoutInflater() 
      .inflate(R.layout.inc_dialog, null); 
     // устанавливаем ее, как содержимое тела диалога 
     adb.setView(view) .setPositiveButton("Cancel", new DialogInterface.OnClickListener() { 
      @Override 
      public void onClick(DialogInterface dialog, int id) { 
       dialog.cancel(); 
      } 
     }) .setNegativeButton("OK", new DialogInterface.OnClickListener() { 
      public void onClick(DialogInterface dialog, int id) { 
       Toast.makeText(getApplicationContext(),"Добавлено", Toast.LENGTH_SHORT).show(); 
      } 
     }); ; 

      return adb.create(); 
     } 

функции OnClick, что я называю в АКТИВНОСТИ

public void onRadioButtonClicked(){ 

      switch(view.getId()) { 
      case R.id.inc_const: 
       findViewById(R.id.DataPick1).setEnabled(true); 
       break; 
      case R.id.inc_random: 
       findViewById(R.id.DataPick1).setEnabled(false); 
       break; 
     } 
    }; 

Если это необходимо в целом Код операции. Income.java

package com.example.pocketbooker; 

import android.app.Activity; 
import android.app.AlertDialog; 
import android.app.Dialog; 
import android.content.DialogInterface; 
import android.os.Bundle; 
import android.view.Menu; 
import android.view.MenuInflater; 
import android.view.MenuItem; 
import android.view.View.OnClickListener; 
import android.widget.LinearLayout; 
import android.widget.TextView; 
import android.widget.Toast; 

public class Income extends Activity{ 

    LinearLayout view; 


    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setTitle("Доходы"); 
     setContentView(R.layout.income_const); 

    } 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu items for use in the action bar 
     MenuInflater inflater = getMenuInflater(); 
     inflater.inflate(R.menu.inoutgo, menu); 
     return super.onCreateOptionsMenu(menu); 


    } 
    protected Dialog onCreateDialog(int id) { 
     AlertDialog.Builder adb = new AlertDialog.Builder(this); 
     adb.setTitle("Добавить доход"); 
     view = (LinearLayout) getLayoutInflater() 
      .inflate(R.layout.inc_dialog, null); 
     // устанавливаем ее, как содержимое тела диалога 
     adb.setView(view) .setPositiveButton("Cancel", new DialogInterface.OnClickListener() { 
      @Override 
      public void onClick(DialogInterface dialog, int id) { 
       dialog.cancel(); 
      } 
     }) .setNegativeButton("OK", new DialogInterface.OnClickListener() { 
      public void onClick(DialogInterface dialog, int id) { 
       Toast.makeText(getApplicationContext(),"Добавлено", Toast.LENGTH_SHORT).show(); 
      } 
     }); ; 

      return adb.create(); 
     } 

    public boolean onOptionsItemSelected(MenuItem item) { 
     // Handle presses on the action bar items 
     switch (item.getItemId()) { 
      case R.id.plus: 
       showDialog(1); 
       return true; 
      default: 
       return super.onOptionsItemSelected(item); 
     } 
    } 

    public void onRadioButtonClicked(){ 

      switch(view.getId()) { 
      case R.id.inc_const: 
       findViewById(R.id.DataPick1).setEnabled(true); 
       break; 
      case R.id.inc_random: 
       findViewById(R.id.DataPick1).setEnabled(false); 
       break; 
     } 
    }; 

    } 

Так что я хочу, чтобы изменить свойство зрения в моем диалоге динамически. Как я могу это сделать? Как я могу вызвать диалоговые представления из моей функции Activity? И извините за мой английский, я из Казахстана. Пожалуйста, помогите.

+0

Кнопка Initialze Radio затем проверяет, не проверено или не включено/выключено согласно – Raghunandan

ответ

0

Итак, я сделал это.

Я использовал DialogFragment

Created отдельный класс Dialog_inc.java для моего диалога

package com.example.pocketbooker; 

import android.annotation.SuppressLint; 
import android.app.DialogFragment; 
import android.content.DialogInterface; 
import android.os.Bundle; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.view.ViewGroup; 
import android.widget.EditText; 

@SuppressLint("NewApi") 
public class Dialog_inc extends DialogFragment implements OnClickListener { 
    EditText POLE; 

    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
     Bundle savedInstanceState) { 
    getDialog().setTitle("Добавить Доход"); 

    View v = inflater.inflate(R.layout.inc_dialog, null); 
    v.findViewById(R.id.btnYes).setOnClickListener(this); 
    v.findViewById(R.id.btnNo).setOnClickListener(this); 
    v.findViewById(R.id.inc_const).setOnClickListener(this); 
    v.findViewById(R.id.inc_random).setOnClickListener(this); 
    POLE=(EditText) v.findViewById(R.id.inc_year); 
    return v; 

    } 

    public void onClick(View v) { 

     switch(v.getId()) 
     {case R.id.inc_const: 
      POLE.setEnabled(true); 
      break; 
     case R.id.inc_random: 
      POLE.setEnabled(false); 

      break; 
     default: 
      dismiss();} 

    } 

    public void onDismiss(DialogInterface dialog) { 
    super.onDismiss(dialog); 

    } 

    public void onCancel(DialogInterface dialog) { 
    super.onCancel(dialog); 

    } 

} 

Немного изменил мою макет для этого, добавлены 2 кнопки. inc_dialog.xml

<?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="match_parent" 
    android:orientation="vertical" > 

    <EditText 
     android:id="@+id/inc_name" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_marginBottom="4dp" 
     android:layout_marginLeft="4dp" 
     android:layout_marginRight="4dp" 
     android:layout_marginTop="16dp" 
     android:hint="Название дохода" 
     android:inputType="textEmailAddress" /> 

    <EditText 
     android:id="@+id/inc_value" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_marginBottom="16dp" 
     android:layout_marginLeft="4dp" 
     android:layout_marginRight="4dp" 
     android:layout_marginTop="4dp" 
     android:hint="Сумма дохода" 
     android:inputType="text" /> 

    <TextView 
     android:id="@+id/tvChkBoxIncType" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_marginBottom="6dp" 
     android:layout_marginLeft="16dp" 
     android:layout_marginRight="4dp" 
     android:layout_marginTop="4dp" 
     android:text="Тип дохода:" /> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" > 

     <RadioGroup 
      android:id="@+id/RadioGroup1" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:orientation="horizontal" > 

      <RadioButton 
       android:id="@+id/inc_random" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_marginLeft="6dp" 
       android:onClick="OnRadio" 
       android:text="Разовый" /> 

      <RadioButton 
       android:id="@+id/inc_const" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_marginLeft="9dp" 
       android:onClick="OnRadio" 
       android:checked="true" 
       android:text="Постоянный" /> 

     </RadioGroup> 
    </LinearLayout> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" > 

     <TextView 
      android:id="@+id/DataPick1" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_gravity="top" 
      android:layout_marginLeft="16dp" 
      android:text="Начало периода:" 
      /> 
     <EditText 
    android:id="@+id/inc_day" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="DD" > 

     </EditText><EditText 
    android:id="@+id/inc_month" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="MM" 

    > 

     </EditText><EditText 
      android:id="@+id/inc_year" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="YYYY" > 

     </EditText> 
    </LinearLayout> 

<EditText 
    android:id="@+id/inc_period" 
    android:layout_width="wrap_content" 

    android:layout_height="wrap_content" 
    android:layout_marginBottom="4dp" 
    android:layout_marginLeft="4dp" 
    android:layout_marginRight="4dp" 
    android:onClick="onRadioButtonClicked" 
    android:text="Каждые Х дней" > 

     </EditText> 
     <LinearLayout 
android:layout_width="wrap_content" 
android:layout_height="wrap_content"> 
<Button 
android:id="@+id/btnYes" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:layout_margin="10dp" 
android:text="yes"> 
</Button> 
<Button 
android:id="@+id/btnNo" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:layout_margin="10dp" 
android:text="no"> 
</Button> 

</LinearLayout> 
</LinearLayout> 

И я вызываю этот диалог следующим образом. Income.class

package com.example.pocketbooker; 

import android.annotation.SuppressLint; 
import android.app.Activity; 
import android.app.DialogFragment; 
import android.os.Bundle; 
import android.view.Menu; 
import android.view.MenuInflater; 
import android.view.MenuItem; 
import android.widget.LinearLayout; 

public class Income extends Activity{ 

    LinearLayout view; 
    DialogFragment dlg1; 

    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setTitle("Доходы"); 
     setContentView(R.layout.income_const); 
     dlg1 = new Dialog_inc(); 
    } 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu items for use in the action bar 
     MenuInflater inflater = getMenuInflater(); 
     inflater.inflate(R.menu.inoutgo, menu); 
     return super.onCreateOptionsMenu(menu); 
      } 

    @SuppressLint("NewApi") 
    public boolean onOptionsItemSelected(MenuItem item) { 
     // Handle presses on the action bar items 
     switch (item.getItemId()) { 
      case R.id.plus: 
       dlg1.show(getFragmentManager(), "dlg1"); 
       return true; 
      default: 
       return super.onOptionsItemSelected(item); 
     } 
    } 

} 

Конечно, сейчас это не красивый код. Но я надеюсь, что это может помочь кому-то. Спасибо всем за то, что вы ответили

1

Поскольку DataPicker находится внутри диалогового окна, вам необходимо передать выбранный пользователем RadioButton id в onCreateDialog. попробуйте как:

protected Dialog onCreateDialog(int id) { 
     RadioGroup radioGroup = (RadioGroup) findViewById(R.id.RadioGroup1); 
      AlertDialog.Builder adb = new AlertDialog.Builder(this); 
      adb.setTitle("Добавить доход"); 
      view = (LinearLayout) getLayoutInflater() 
       .inflate(R.layout.inc_dialog, null); 
    int radioBtnid = radioGroup.getCheckedRadioButtonId(); 

    // get DatePicker 
    DatePicker datepick1=(DatePicker)view.findViewById(R.id.DataPick1); 
    if(radioBtnid==R.id.inc_const){ 
     //Enable DataPick1 
     datepick1.setEnabled(true); 
    }else if(radioBtnid==R.id.inc_random){ 
     // Disable DataPick1 
     datepick1.setEnabled(false); 
    } 

//your code ... 
} 
+0

Это не помогло. Выполняется только 1 раз ... и тогда ничего не происходит – anindis

1

Прежде всего передать в представлении в качестве аргумента вашего метода onRadioButtonClicked(), так как это необходимо для метода OnClick, указанного в макете XML. Определить вид как его единственный параметр (это будет просмотр, который был нажат)

Также проверьте, было ли переданное представление выбранным или нет, вызвав метод isChecked(). Ваш окончательный код может выглядеть следующим образом:

public void onRadioButtonClicked(View view) { 
// Is the button now checked? 
boolean checked = ((RadioButton) view).isChecked(); 

// Check which radio button was clicked 
switch(view.getId()) { 
    case R.id.inc_const: 
     if (checked) 
      // Do the necessary things here if inc_const is selected 
      findViewById(R.id.DataPick1).setEnabled(true); 
     break; 
    case R.id.inc_random: 
     if (checked) 
      // Put the code necessary if the random is checked 
      findViewById(R.id.DataPick1).setEnabled(false); 
     break; 
    } 
} 

Сообщите мне, если это поможет.

+0

Я здесь новый, поэтому я надеюсь, что вот как люди отвечают здесь. К сожалению, это не помогло. (( – anindis

Смежные вопросы