android-fragments 如何在片段上添加弹出气泡?[已关闭]

fjaof16o  于 2022-11-14  发布在  Android
关注(0)|答案(2)|浏览(132)

**已关闭。**此问题需要debugging details。当前不接受答案。

编辑问题以包含desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem。这将有助于其他人回答问题。
7个月前关闭。
Improve this question
你好我有一个问题我不能把一个片段在android studio弹出窗口
这是我尝试使用的代码,我尝试的事情是添加一个弹出窗口到我的片段,当我点击一个按钮弹出窗口显示,并显示一些文本在它,我试图添加一个工作代码在fargemnt它只是不工作

public class fragment2 extends Fragment {
   @Nullable
   @Override
   public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
       return inflater.inflate(R.layout.fragment2_layout,container,false);

   }
   private Activity ControllerCompat;
   android.widget.Button Bouton1;


   @Override
   public void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.fragment2_layout);
       Bouton1 = (android.widget.Button) findViewById(R.id.button1);



       Bouton1.setOnClickListener(new View.OnClickListener() {
           public void onClick(View view) {
               AlertDialog.Builder alert = new AlertDialog.Builder(fragment2.this);
               alert.setTitle("voici les boutons");

               alert.setPositiveButton("yes   ", new DialogInterface.OnClickListener() {

                   public void onClick(DialogInterface dialog, int whichButton) {

                   }
               });

               alert.setNegativeButton("no", new DialogInterface.OnClickListener() {

                   public void onClick(DialogInterface dialog, int whichButton) {

                   }
               });

               alert.show();
           }
       });
   }
}
tktrz96b

tktrz96b1#

像这样?如果是,那么我有一个错误的buuton 1说,他不能解决符号“bouton 1”
'公共类fragment 2 extends Fragment { @Nullable @Override public View on CreateView(@非空布局inflater inflater,@可空视图组容器,@可空绑定保存示例状态){返回inflater.inflate(R.layout.fragment2_布局,容器,false);

buton1.setOnClickListener(new View.OnClickListener() {
        public void onClick(View view) {
            AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(getContext());
            alertDialogBuilder.setTitle("Title");
            alertDialogBuilder.setMessage("Message");
            alertDialogBuilder.setCancelable(true);
            alertDialogBuilder.setPositiveButton("yes", (dialog, id) ->
                    {

                    }
            );
            alertDialogBuilder.setNegativeButton("no", (dialog, id) -> dialog.cancel());
            AlertDialog myAlertDialog = alertDialogBuilder.create();
            myAlertDialog.show();
        }
    });

}

} `

6qqygrtg

6qqygrtg2#

Initialize your buton first 
    Bouton1 = (android.widget.Button) findViewById(R.id.button1); 
    Bouton1.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(getContext());
                alertDialogBuilder.setTitle("Title");
                alertDialogBuilder.setMessage("Message");
                alertDialogBuilder.setCancelable(true);
                alertDialogBuilder.setPositiveButton("yes", (dialog, id) ->
                        {

                        }
                );
                alertDialogBuilder.setNegativeButton("no", (dialog, id) -> dialog.cancel());
                AlertDialog myAlertDialog = alertDialogBuilder.create();
                myAlertDialog.show();
            }
        });

相关问题