主要活动
package com.example.dialog;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import android.content.DialogInterface;
import android.os.Bundle;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
AlertDialog alertDialog= new AlertDialog.Builder(this).create();
alertDialog.setTitle("Term and Conditons");
alertDialog.setIcon(R.drawable.baseline_info_24);
alertDialog.setMessage("have you read all term and condition");
alertDialog.setButton("yes, I've read",new DialogInterface.OnClickListener(){
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, "Tes, you can proceed now..", Toast.LENGTH_SHORT).show();
}
});
alertDialog.show();
}
}
错误
C:\Users\anonhake\AndroidStudioProjects\dialog\app\src\main\java\com\example\dialog\MainActivity.java:20: error: no suitable method found for setButton(String,<anonymous OnClickListener>)
alertDialog.setButton("yes, I've read",new DialogInterface.OnClickListener(){
^
method AlertDialog.setButton(int,CharSequence,Message) is not applicable
(actual and formal argument lists differ in length)
method AlertDialog.setButton(int,CharSequence,OnClickListener) is not applicable
(actual and formal argument lists differ in length)
method AlertDialog.setButton(int,CharSequence,Drawable,OnClickListener) is not applicable
(actual and formal argument lists differ in length)
2条答案
按热度按时间bvjxkvbb1#
不要在第一行建立对话框(
create()
调用),使用AlertDialog.Builder
示例,在其上设置“possitive”和“negative”按钮(还有标题、消息等),然后调用create()
或更简单的show()
检出样品HERE
当点击任何按钮时,对话框将自动关闭,“是”对话框将关闭整个
Activity
,“否”为空,您将停留在(看起来为空)Activity
jfgube3f2#
根据错误,看起来你正在使用的带参数类型的方法不存在。所以基本上你有三种选择