我有一个警报对话框,其中有一个EditText,您可以在其中输入文本,然后将其添加到ListView。然后在每个项目上都有一个Button我想让Button成为一个AlertDialog。下面的代码不起作用。
这是我的代码......
public class DeleteRenameList extends Activity {
Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
button = (Button)findViewById(R.id.button1);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
AlertDialog.Builder alert=new AlertDialog.Builder(DeleteRenameList.this);
alert.setMessage("What do you want to do?");
alert.setPositiveButton("Rename", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
alert.setNegativeButton("Delete", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
alert.setNeutralButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
AlertDialog ale=alert.create();
ale.show();
}
});
}}
请帮助。
答案 0 :(得分:0)
您的新onClickListener应该是新的View.onClickListener。 你应该有这样的东西开始:
Button button = (Button)findViewById(R.id.button1);
button.setOnClickListener(new View.onClickListener(){
@Override
public void onClick(View v){
AlertDialog.Builder builder = new Builder(this);
builder.setTitle("Are you sure you want to exit?")
.setIcon(R.drawable.ic_launcher)
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
dialog.dismiss();
finish();
}
}).setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
dialog.dismiss();
}
}).create().show();
}
});
答案 1 :(得分:0)
试试这个:
制作自定义对话框并展示它:
//Make your class named CUstomDialogClass
import android.app.Activity;
import android.app.Dialog;
import android.os.Bundle;
import android.widget.Button;
public class CustomDialogClass extends Dialog{
public Activity c;
public Dialog d;
public Button yes, no;
public CustomDialogClass(Activity a) {
super(a);
// TODO Auto-generated constructor stub
this.c = a;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.customdialog);
}
}
In your MainActivity, place this code in your onCreate() method:
Button button = (Button)findViewById(R.id.yourid);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
final CustomDialogClass dialog = new CustomDialogClass(MainActivity.this);
dialog.setTitle("Your Title");
dialog.show();
final Timer time = new Timer();
time.schedule(new TimerTask() {
@Override
public void run() {
dialog.dismiss();
}
}, 5000);
}
});
最后,您可以修改自定义对话框的布局。我举了一个例子:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Yout Title"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Your Message"
android:textAppearance="?android:attr/textAppearanceMedium" />
</LinearLayout>
然后你可以添加你的听众。
希望这会有所帮助.. :)