Я попытался решить эту проблему, читая о stackoverflow, а также попытался лучше понять использование метода getButton, считывающего непосредственно с веб-сайта студии Android.
Ниже описание прямо с сайта developer.android.com (выделено мной жирным шрифтом):
public Button getButton (int whichButton)
Gets one of the buttons used in the dialog. Returns null if the specified button does not exist or the dialog has not yet been fully created (for example, via Dialog.show() or Dialog.create()).
MapsActivity.java
Как вы можете видеть ниже, я использую метод getButton после Dialog.show(), но получаю error: cannot find symbol method getButton(int)
. Не могли бы вы сказать мне, в чем проблема? Спасибо
...
case R.id.settings:
AlertDialog.Builder builder2 = new AlertDialog.Builder(this, R.style.SettingDialogStyle);
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = inflater.inflate(R.layout.setting_alert_dialog, null);
SeekBar sb = (SeekBar) v.findViewById(R.id.seekBar2);
final TextView txtView = (TextView) v.findViewById(R.id.intervalValue);
sb.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
txtView.setText(String.valueOf(progress)+ " sec");
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
builder2.setView(v);
builder2.setTitle("Interval between updates");
builder2.setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// User clicked OK button
}
});
builder2.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// User cancelled the dialog
}
});
builder2.show();
final Button positiveButton = builder2.getButton(DialogInterface.BUTTON_POSITIVE);
LinearLayout.LayoutParams positiveButtonLL = (LinearLayout.LayoutParams) positiveButton.getLayoutParams();
positiveButtonLL.gravity = Gravity.CENTER;
positiveButton.setLayoutParams(positiveButtonLL);
return true;
...
setting_alert_dialog.xml
<RelativeLayout xmlns:android = "http://schemas.android.com/apk/res/android"
android:layout_width = "match_parent"
android:layout_height = "match_parent">
<TextView
android:id = "@+id/intervalValue"
android:layout_width = "wrap_content"
android:layout_height = "wrap_content"
android:layout_alignParentTop = "true"
android:layout_centerHorizontal = "true"
android:layout_marginTop = "16dp"
android:textColor = "#FFFFFF"
android:text = "TextView" />
<SeekBar
android:id = "@+id/seekBar2"
style = "@style/Widget.AppCompat.SeekBar.Discrete"
android:layout_width = "150dp"
android:layout_height = "wrap_content"
android:layout_alignParentTop = "true"
android:layout_centerHorizontal = "true"
android:layout_marginTop = "45dp"
android:max = "60"
android:progress = "3"
android:progressTint = "#FFFFFF"
android:thumbTint = "#FFFFFF" />
</RelativeLayout>




getButton() является членом класса AlertDialog, а не AlertDialog.Builder.
если вы измените на: AlertDialog alertDialog = new AlertDialog(this, R.style.SettingDialogStyle);
тогда у вас есть доступ к alertDialog.getButton()
Альтернатива, после этой строки:
AlertDialog.Builder builder2 = new AlertDialog.Builder(this, R.style.SettingDialogStyle);
по этому:
AlertDialog alertDialog = builder2.create();
у вас есть диалоговое окно с предупреждением, которое может получить доступ к getButton()
Вы можете убедиться, что getButton() является методом класса AlertDialog, взглянув на проект AOSP по адресу
Итак, правильный код должен быть следующим:
AlertDialog dialog = builder2.show();
final Button positiveButton = dialog.getButton(DialogInterface.BUTTON_POSITIVE);
какие-либо отзывы после ответов ниже?