首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Android警报对话框不会在模拟器中打开

Android警报对话框不会在模拟器中打开
EN

Stack Overflow用户
提问于 2022-03-13 14:17:32
回答 1查看 132关注 0票数 0

当选择了旋转器中的某些选项时,我一直试图调试一个警告对话框的问题,当选择了某个旋转器中的某些选项时,它不会在单击图像按钮时打开。

下面是我的MainActivity.java文件,其中包括警报对话框的相关代码(遗漏了按预期工作的自旋器代码),任何帮助都将不胜感激!

代码语言:javascript
复制
    public class MainActivity extends AppCompatActivity {
    public static final String EXTRA_MESSAGE = "com.example.myfirstapp.MESSAGE";


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Initialise Inputs & Fields
        this.UnitSelector();
        Spinner spinner = (Spinner) findViewById(R.id.options_spinner);

        // UI Relationships
        ImageButton tapeButton = (ImageButton)findViewById(R.id.imageButton_tape);

        Log.d("SpinnerValue", spinner.getSelectedItem().toString());

        EditText editText = (EditText) findViewById(R.id.value);
        String message = editText.getText().toString();

        // Listeners & Actions
        tapeButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String spinnerValue = spinner.getSelectedItem().toString();
//                float messageFloat = Float.parseFloat(message);
                Log.d("Button Clicked", spinnerValue);
                if (spinnerValue == "Kilogram" || spinnerValue == "Celsius"){
                    createDialog("Nah mate", "Okay", MainActivity.this);
                }
            }
        });
    }

void createDialog( String title, String msg, Context context){
        AlertDialog.Builder builder
                = new AlertDialog
                .Builder(context);

        // Set the message show for the Alert time
        builder.setMessage(msg);

        // Set Alert Title
        builder.setTitle(title);

        // Set Cancelable false
        // for when the user clicks on the outside
        // the Dialog Box then it will remain show
        builder.setCancelable(true);


        builder.setNeutralButton("Yes", new DialogInterface.OnClickListener() {
            @Override public void onClick(DialogInterface dialog, int which){
                dialog.cancel();
            }
        });


        // Create the Alert dialog
        AlertDialog alertDialog = builder.create();
        alertDialog.show();
    }
}
EN

回答 1

Stack Overflow用户

发布于 2022-03-13 14:54:16

我想你忘了设置适配器

代码语言:javascript
复制
  Spinner spinner = (Spinner) findViewById(R.id.options_spinner);

        String[] values = {"Kilogram","Celsius","other"};

        ArrayAdapter<String> adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1,values);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

        spinner.setAdapter(adapter);

        ImageButton tapeButton = (ImageButton)findViewById(R.id.imageButton_tape);

        tapeButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String spinnerValue = spinner.getSelectedItem().toString();
                if (spinnerValue.equals("Kilogram") || spinnerValue.equals("Celsius")){
                    createDialog("Nah mate", "Okay", MainActivity.this);
                }
            }
        });
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71457618

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档