gpt4 book ai didi

android - 在 fragment 中添加警报对话框?

转载 作者:行者123 更新时间:2023-11-30 03:15:26 26 4
gpt4 key购买 nike

我正在尝试在 android fragment 中添加警报对话框。这就像 fragment 加载时它应该检查字符串的值,并根据它需要显示一个对话框。但它正在进来到 if 条件,但警告框不工作..

这是我的代码

    public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_planet, container, false);



if(heart.equals("No connection"))
{
Toast.makeText(getActivity(), "in alert", Toast.LENGTH_SHORT).show();
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity().getApplicationContext());
builder.setCancelable(true);
builder.setTitle("Connection Problem");
builder.setMessage("No device detected Do you want to restart the application?");
builder.setPositiveButton("OK", new DialogInterface.OnClickListener()
{
@Override
public void onClick(DialogInterface arg0, int arg1)
{

Intent restrat=new Intent(getActivity(),LandingPage.class);
startActivity(restrat);


}
});

builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener()
{
@Override
public void onClick(DialogInterface arg0, int arg1)
{




}
});

}
}

如何解决这个问题?

最佳答案

试试这个,

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState);
View rootView = inflater.inflate(R.layout.fragment_planet, container,
false);

if (heart.equals("No connection")) {
Toast.makeText(getActivity(), "in alert", Toast.LENGTH_SHORT)
.show();
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()
.getApplicationContext());
builder.setCancelable(true);
builder.setTitle("Connection Problem");
builder.setMessage("No device detected Do you want to restart the application?");
builder.setPositiveButton("OK",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface arg0, int arg1) {

Intent restrat = new Intent(getActivity(),
LandingPage.class);
startActivity(restrat);

}
});

builder.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface arg0, int arg1) {

}
});
AlertDialog dialog=builder.create();
dialog.show();
}
return rootView;
}

关于android - 在 fragment 中添加警报对话框?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20208274/

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