Issues with OnClick Listener for Custom Dialog Box
I am trying to write a custom dialog box that takes a name from the user. I am getting a "OnClickListener cannot be resolved to a ty开发者_如何学运维pe - The method setOnClickListener(View.OnClickListener) in the type View is not applicable for the arguments (new OnClickListener(){})" error in Eclipse. Anyone know what I am doing wrong?
Here is my Code:
public void getName(){
Dialog dialog = new Dialog(main.this);
dialog.setContentView(R.layout.customdialog);
dialog.setTitle("New Game");
dialog.setCancelable(true);
//there are a lot of settings, for dialog, check them all out!
final EditText inputBox = new EditText(this);
//set up text
final TextView text = (TextView) dialog.findViewById(R.id.TextView01);
text.setText("Enter Your Name...");
//set up button
final Button button = (Button) dialog.findViewById(R.id.namebutton);
button.setOnClickListener(new OnClickListener() {
public void onClick() {
String str = inputBox.getText().toString();
setName(str);
}
});
//now that the dialog is set up, it's time to show it
dialog.show();
}
You may just need to change this
button.setOnClickListener(new OnClickListener() {
to this
button.setOnClickListener(new View.OnClickListener() {
EDIT - To kind of combo our answers, also make sure that you're importing the correct class as Cristian has stated.
I guess you are importing the wrong OnClickListener
. Make sure you have:
import android.view.View.OnClickListener;
instead of
import android.content.DialogInterface.OnClickListener;
精彩评论