1

У меня есть набор кнопок, определенных в макете таблицы.Событие нажатия кнопки не запускается

custom_layout.xml:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:orientation="vertical" android:layout_width="match_parent" 
android:layout_height="match_parent"> 

<TableLayout 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:paddingBottom="10dp" 
    android:id="@+id/tableLayout"> 

    <TableRow 
     android:layout_weight="1"> 
     <Button 
      style="?android:attr/buttonStyleSmall" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:id="@+id/firstBtn" 
      android:text="F" 
      android:layout_weight="0.3"> 
     </Button> 

     <Button 
      style="?android:attr/buttonStyleSmall" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:id="@+id/secondBtn" 
      android:text="S" 
      android:layout_weight="0.3"> 
     </Button> 

     <Button 
      style="?android:attr/buttonStyleSmall" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:id="@+id/thirdBtn" 
      android:text="T" 
      android:layout_weight="0.3"> 
     </Button> 

    </TableRow> 

    </TableLayout> 

</RelativeLayout> 

Этот XML устанавливается в качестве макета в пользовательских предупреждений диалога.

Код:

public class CustomDialogPreference extends DialogPreference { 


private Button firstBtn = null; 
private Button secondBtn = null; 
private Button thirdBtn = null; 


public CustomDialogPreference(Context context, AttributeSet attrs) { 
    super(context, attrs); 
    setPersistent(false); 
    this.context = context; 
    setDialogLayoutResource(R.layout.custom_layout); 



} 

@Override 
protected void onBindDialogView(View view) { 

    firstBtn = (Button)view.findViewById(R.id.firstBtn); 
    secondBtn = (Button)view.findViewById(R.id.secondBtn); 
    thirdBtn = (Button)view.findViewById(R.id.thirdBtn); 

    firstBtn.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 

      Log.d("Check","First button is clicked")); 

     } 
    }); 

    secondBtn.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 

      Log.d("Check","Second button is clicked")); 

     } 
    }); 

    thirdBtn.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 

      Log.d("Check","Third button is clicked")); 

     } 
    }); 


    super.onBindDialogView(view); 
} 

@Override 
protected void onDialogClosed(boolean positiveResult) { 
    super.onDialogClosed(positiveResult); 
    persistBoolean(positiveResult); 

    Log.d("Blockout","Dialog close detected"); 
    if(positiveResult) 
    { 

     Log.d("Blockout","Save button had been clicked"); 

    } 

    } 

} 

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

+0

как вы открыли диалог? поделиться своим кодом –

+0

Я устанавливаю это диалоговое окно на экране предпочтений xml (не через код) .. и открывается диалоговое окно, а некоторые другие элементы в настраиваемом диалоге доступны (я не включал это здесь, поскольку он не имеет отношения к вопрос) – SoulRayder

+0

сделать тост, заменяющий журнал. –

ответ

1

Если вам нужен пользовательский диалог, я бы предложил класс DialogFragment. Там есть руководство по документации - Dialogs.

Я думаю, вы хотите этот способ - onCreateDialogView.

Например,

public class CustomDialogPreference extends DialogPreference 
    implements View.OnClickListener { 

    private Context context; 

    public CustomDialogPreference(Context context, AttributeSet attrs) { 
     super(context, attrs); 
     setPersistent(false); 
     this.context = context; 
    } 

    @Override 
    protected View onCreateDialogView() { 
     super.onCreateDialogView(); 
     LayoutInflater inflater = LayoutInflater.from(this.context); 
     View rootView = inflater.inflate(R.layout.custom_layout,null); 

     rootView.findViewById(R.id.firstBtn).setOnClickListener(this); 
     rootView.findViewById(R.id.secondBtn).setOnClickListener(this); 

     return rootView; 
    } 

    @Override 
    public void onClick(View v) { 
     switch (v.getId()) { 
      case R.id.firstBtn: 
       Log.d("Check","First button is clicked")); 
       break; 
      case R.id.secondBtn: 
       break; 
     } 
    } 
+0

спасибо за ответ, одна незначительная коррекция: inflater.inflate() не принимает ни одного параметра int. Отредактировано сообщение с исправлением. – SoulRayder