gpt4 book ai didi

Android:在动态 onclicklistener 中创建自定义警报对话框

转载 作者:行者123 更新时间:2023-11-29 15:23:34 24 4
gpt4 key购买 nike

不确定这个标题描述我希望做的事情的程度如何,但这里是。基本上我有一个应用程序,它实用地创建了一个按钮列表,当点击这些按钮时会返回一个描述。

我创建了以下类

public class DynamicOnClickListener implements OnClickListener
{

String description;
public DynamicOnClickListener(String adesc) {
//sets the description attribute at instantiation
this.description = adesc;
}

public void onClick(View v) {
//on button click returns dialog box with description in it
Log.v("DynamicOnClickListener","1");
AlertDialog.Builder builder = new AlertDialog.Builder(v.getContext());
builder.setMessage(description);
builder.setCancelable(false);
builder.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id)
{
dialog.cancel();
}
});
AlertDialog alert = builder.create();
alert.show();
}

}

这可以完美地工作,但是我非常想使对话框更加生动一些。我一直在网上查看一些示例,根据 android 文档,他们建议定义自定义 xml 布局并使用 LayourInflator 将自定义 xml 设置为对话框的 View 。 (好吧,我就是这么理解的,可能是错误的)

虽然文档示例与我的示例略有不同,但根据他们的示例我应该添加以下行

// Get the layout inflater
LayoutInflater inflater = getActivity().getLayoutInflater();

// Inflate and set the layout for the dialog
// Pass null as the parent view because its going in the dialog layout
builder.setView(inflater.inflate(R.layout.dialog_signin, null))

然而,当我将它添加到我的类时,导致以下结果,我在 getActivity() 上收到错误

public class DynamicOnClickListener implements OnClickListener
{

String description;
public DynamicOnClickListener(String adesc) {
//sets the description attribute at instantiation
this.description = adesc;
}

public void onClick(View v) {
//on button click returns dialog box with program description in it
Log.v("DynamicOnClickListener","1");
AlertDialog.Builder builder = new AlertDialog.Builder(v.getContext());

// Get the layout inflater
LayoutInflater inflater = getActivity().getLayoutInflater();

// Inflate and set the layout for the dialog
// Pass null as the parent view because its going in the dialog layout
builder.setView(inflater.inflate(R.layout.description_dialog, null));


builder.setMessage(description);
builder.setCancelable(false);
builder.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id)
{
dialog.cancel();
}
});
AlertDialog alert = builder.create();
alert.show();
}

}

最佳答案

我用一个 EditText 来输入电子邮件和一个按钮来发送数据和取消它。通过使背景变暗来显示此对话框。并将其显示在当前 Activity 之上。

    final Dialog dialog = new Dialog(this);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
LinearLayout popUp = new LinearLayout(this);
popUp.setBackgroundColor(Color.LTGRAY);
popUp.setOrientation(1);

EditText t1 = new EditText(this);
t1.setHint("Enter Your Email ID ");
t1.setTextColor(Color.parseColor("#FFFFFF"));
t1.setInputType(InputType.TYPE_CLASS_TEXT|InputType.TYPE_TEXT_VARIATION_EMAIL_ADDRESS);

LinearLayout btnLayout = new LinearLayout(this);
btnLayout.setBackgroundColor(Color.LTGRAY);
btnLayout.setOrientation(0);
btnLayout.setGravity(Gravity.CENTER);

Button send = new Button(this);
send.setText("Send");
send.setTextColor(Color.WHITE);
Button cancel = new Button(this);
cancel.setText("Cancel");
cancel.setTextColor(Color.WHITE);

btnLayout.addView(send);
btnLayout.addView(cancel);
popUp.addView(t1);
popUp.addView(btnLayout);

dialog.setContentView(popUp);
cancel.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
dialog.dismiss();
}
});
dialog.show();

关于Android:在动态 onclicklistener 中创建自定义警报对话框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15636006/

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