gpt4 book ai didi

java - 创建 alertDialog 后无法解析 getButton 方法

转载 作者:行者123 更新时间:2023-11-29 04:16:59 25 4
gpt4 key购买 nike

我试图通过在 stackoverflow 上阅读来解决这个问题,我也试图更好地理解 getButton 方法的使用,直接从 android studio 网站读取。在直接来自 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

正如您在下面看到的,我在 Dialog.show() 之后使用了 getButton 方法,但是我得到了 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() 的警报对话框

关于java - 创建 alertDialog 后无法解析 getButton 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51728664/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com