我试图解决有关stackoverflow的问题,并且试图更好地理解直接从android studio网站读取getButton
方法的用法。
在直接来自developer.android.com的描述下方(粗体是我的):
公共按钮getButton(int whichButton)
获取使用的按钮之一
在对话框中。如果指定的按钮不存在,则返回null
该对话框尚未完全创建(例如,通过
Dialog.show()或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()
的警报对话框