2014-01-26 4 views
0

У меня есть настраиваемый диалог, у которого есть кнопка button.each, представляющая номер.something как приложение часов, когда вы хотите установить будильник. как я могу получить onClick каждой кнопки в настройках моего диалога? это часть моего диалога макета (time_picker):событие onclick в диалоговом окне

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:gravity="center"> 

<LinearLayout 
    android:id="@+id/showTimer" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:orientation="vertical" 
    > 

    <TextView 
     android:id="@+id/textView1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_marginLeft="10dp" 
     android:text="_ _ : _ _" 
     android:textSize="20dp" 
     android:textStyle="bold" /> 

</LinearLayout> 

<RelativeLayout 
    android:id="@+id/Buttons" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:gravity="center" 
    android:layout_below="@+id/showTimer" 
    android:layout_marginTop="10dp"> 

    <Button 
    android:id="@+id/button1" 
    android:layout_width="100dp" 
    android:layout_height="wrap_content" 
    android:layout_alignBaseline="@+id/button5" 
    android:layout_alignBottom="@+id/button5" 
    android:layout_alignLeft="@+id/button3" 
    android:layout_centerInParent="true" 
    android:text="6" 
    android:textStyle="bold" 
    android:textSize="20dp" 
    android:tag="1" 
    android:background="@android:color/transparent" /> 

<Button 
    android:id="@+id/button2" 
    android:layout_width="100dp" 
    ... 

это настройка макета, запуск диалога предпочтение:

<com.example.test.TimerForNoti 
    android:key = "pref_sync_noti_timer" 
    android:title = "@string/pref_sync_noti_timer" 
    android:summary = "@string/pref_sync_noti_timer_summ" 
    android:dialogMessage = "@string/pref_sync_noti_timer"> 
</com.example.test.TimerForNoti> 

и это класс диалог предпочтений (TimerForNoti):

public class TimerForNoti extends DialogPreference 
{ 

public TimerForNoti(Context context, AttributeSet attrs) { 
    super(context, attrs); 

    setDialogLayoutResource(R.layout.time_picker); 
    setPositiveButtonText(R.string.ok); 
    setNegativeButtonText(R.string.cancel); 

    Dialog di = new Dialog(context); 
    di.setContentView(R.layout.time_picker); 

    OnClickListener test = new OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      // TODO Auto-generated method stub 
      String tag = v.getTag().toString(); 
      if(tag == "1"){ 
       Log.v("onOne", "ok"); 
      } 
          // ..... 

     } 
    }; 

    Button btn1 = (Button) di.findViewById(R.id.button1); 
    btn1.setOnClickListener(test); 
} 

+0

Это вы хотите установить будильник или время по номерам нажатым? вы можете быть более ясными. – Umitk

+0

да, я хочу что-то подобное –

ответ

0

Если я правильно понимаю ваш вопрос, вы можете установить метки для каждой кнопки и установите один OnClickListener для всех из них:

<Button 
    android:id="@+id/button1" 
    android:layout_width="100dp" 
    android:layout_height="wrap_content" 
    android:layout_alignBaseline="@+id/button5" 
    android:layout_alignBottom="@+id/button5" 
    android:layout_alignLeft="@+id/button3" 
    android:layout_centerInParent="true" 
    android:text="6" 
    android:textStyle="bold" 
    android:textSize="20dp" 
    android:tag="6" 
    android:background="@android:color/transparent" /> 

Код:

 OnClickListener buttonsListener = new OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       String buttonText = v.getTag().toString(); 
       // your logic here 
      } 
     }; 

     Button btn1 = (Button)findViewById(R.id.button1); 
     btn1.setOnClickListener(buttonsListener); 

     Button btn2 = (Button)findViewById(R.id.button2); 
     btn2.setOnClickListener(buttonsListener); 

     Button btn3 = (Button)findViewById(R.id.button3); 
     btn3.setOnClickListener(buttonsListener); 

     // your buttons here 

Также вы можете использовать v.getId() в OnClickListener для определения, какая кнопка была нажата

+0

Я пробую ваш ответ, но это не работает: -? –

+0

Это крушение или другая проблема? – Dimmerg

+0

нет, это не сбой, так же, как вы не нажимаете кнопку –

0

Насколько я понял ваш вопрос. Вы хотите, чтобы один метод вызывался при каждой нажатой кнопке. Для этого Добавьте к каждой вашей кнопки элемента в XML:

<Button 
    android:onClick="OnClick" 
    ..      /> 

И этот метод в файл .java, где вы хотите, чтобы выполнить задачу:

public void OnClick(View v){ 
    // do something.... 
} 

Или вы можете напрямую позвонить setOnClickListener на кнопку объект

button.setOnClickListener(new OnClickListener() { 
     @Override 
     public void onClick(View arg0) { 
      // do something.... 
     } 
}); 
+0

Я пробую этот метод, но он не работает, и я не знаю, почему ... возможно потому, что это предпочтение диалога не активность –

+0

Возможно, но может не работать, потому что система будет искать метод OnClick в активности. Посмотрите на это: http://stackoverflow.com/questions/4243704/using-onclick-attribute-in-layout-xml-causes-a-nosuchmethodexception-in-android – Dimmerg

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