我尝试解决有关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>
答案 0 :(得分:2)
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()
答案 1 :(得分:1)
通过查看位于的AOSP项目,您可以验证getButton()
是AlertDialog
类的方法
因此,正确的代码应为以下代码:
AlertDialog dialog = builder2.show();
final Button positiveButton = dialog.getButton(DialogInterface.BUTTON_POSITIVE);