OnclickListener无法处理小屏幕尺寸?

时间:2014-03-28 21:36:12

标签: java android onclick

所以我有一些代码为一个按钮设置onClickListener,这个按钮似乎不适用于屏幕尺寸小于4英寸的设备,但仅适用于特定按钮。我不确定为什么,因为它似乎不是影响操作系统级别版本,而只是屏幕尺寸。

我在onclick方法中记录了代码,该方法显示除new_game按钮之外所有按钮正确注册和触发。关于为什么会发生这种情况的任何意见都将不胜感激。

OnCreate的代码:

Button acknowledgements = (Button) findViewById(R.id.acknolwedgments_word_Game);
acknowledgements.setOnClickListener(this);
Button quit = (Button) findViewById(R.id.quit_word_game_button);
quit.setOnClickListener(this);
Button new_game = (Button) findViewById(R.id.word_game_new_Button);
Log.e("NEW GAME BUTTON", String.valueOf(new_game));
new_game.setOnClickListener(this);
Log.e("SET ONCLICK", "DONE");

OnClickListener:

public void onClick(View view) {
    int id = view.getId();
    Log.e("CLICKED BUTTON", String.valueOf(view));
    if (id == R.id.quit_word_game_button){
        Intent i = new Intent(this, Game.class);
        startActivity(i);
    }
    else if (id == R.id.acknolwedgments_word_Game){
        Intent i = new Intent(this, Acknowledgements.class);
        startActivity(i);
    }
    else if (id == R.id.word_game_new_Button){
        final AlertDialog alert = new AlertDialog.Builder(word_game_mainscreen.this).create();
        final EditText edit = new EditText(getBaseContext());
        edit.setHint("Username");
        alert.setView(edit);
        alert.setButton(DialogInterface.BUTTON_NEGATIVE, "CANCEL", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                alert.dismiss();
            }
        });
        alert.setButton(DialogInterface.BUTTON_POSITIVE, "PLAY!", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                final String opponent = String.valueOf(edit.getText());
                new AsyncTask(){

                    @Override
                    protected Object doInBackground(Object[] objects) {
                        //Code to synchronize it to a server
                    }

                    @Override
                    protected void onPostExecute(Object o) {
                        super.onPostExecute(o);
                        Log.e("POST EXECUTE", (String)o);
                        //Creates intent to take you to the game
                    }
                }.execute();
            }
        });
        alert.show();
    }
    else if (id == R.id.togglesound){
        ToggleButton music = (ToggleButton) findViewById(R.id.togglesound);
        if (music.isChecked()){
            Music.play(this, R.raw.wordgame);
        }
        else{
            Music.stop(this);
        }
    }
}

1 个答案:

答案 0 :(得分:0)

简单建议:swith案例远远优于if-elseclass_name.this大多数情况下都比getBaseContext()友好。

要显示AlertDialog,我们必须首先创建构建器。例如:

AlertDialog dialog;
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setIcon(...).setTitle(...)
.setView(...)
.setPositiviButton(...)
.setNegativeButton(...);

//Now create the builder and assign to AlertDialog
dialog = builder.create();
dialog.show;