gpt4 book ai didi

java - 如何将 Alertdialog 添加到我的 Android 应用程序?

转载 作者:太空宇宙 更新时间:2023-11-04 14:11:34 25 4
gpt4 key购买 nike

我想向我的 Android 应用程序添加是/否对话框,我尝试了解决方案 in this answer但我无法让它与我的代码一起工作,有什么帮助吗?

这是我的代码,我想在其中从 EditText 写入文本。

    public void buttonSelect( View v ) {
View view = null;
String mac1 = "mac1";
String mac2 = "mac2";

TextView tv1, tv2;
tv1 = (TextView) findViewById(R.id.textView1);
tv2 = (TextView) findViewById(R.id.textView2);

switch (v.getId()) {
case (R.id.Write_MAC1):
writeData(view, mac1); //I need to confirm writing the data
break;

case (R.id.Write_MAC2):
writeData(view, mac2); //I need to confirm writing the data
break;

}
}

//---------------------------- Writing MACs addresses Function --------------------------------------------

public void writeData(View view, String macNum)
{
BufferedWriter bufferWriter =null;
try {
FileOutputStream fileOutputStream = openFileOutput(macNum, Context.MODE_PRIVATE);
bufferWriter = new BufferedWriter(new OutputStreamWriter(fileOutputStream));

if (macNum.equals("mac1")){
bufferWriter.write(((EditText)this.findViewById(R.id.editText1)).getText().toString());}

if (macNum.equals("mac2")){
bufferWriter.write(((EditText)this.findViewById(R.id.editText2)).getText().toString());}

} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally
{
try {
bufferWriter.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}

}

最佳答案

尝试使用这个:

AlertDialog.Builder alertbox = new AlertDialog.Builder(LauncherActivity.this);
alertbox.setTitle("Are you sure?");
alertbox.setPositiveButton("YES", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(LauncherActivity.this, "You Choose Yes!!", Toast.LENGTH_LONG).show();
}
});
alertbox.setNegativeButton("NO", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(LauncherActivity.this, "You Choose Nooo!!", Toast.LENGTH_LONG).show();
}
});
alertbox.show();

查看此git repo

此存储库中还存在一个自定义警报对话框示例。

关于java - 如何将 Alertdialog 添加到我的 Android 应用程序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28262986/

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