如何以透明的形式在屏幕中央显示3 2 1文本

时间:2015-08-10 11:00:57

标签: java android textview

我正在开发一个游戏。我有一个AlertDialog用于退出游戏但是当我按下cancle按钮ao alertDialog我的游戏停止了3秒钟并再次恢复。我已实现此代码,但文本在屏幕中央不可见。 我想要的东西是当我在屏幕上按下cancle按钮3 2 1文本显示并且在显示1文本不可见之后再次开始游戏。 这是我的代码

public class Game extends Activity {

MediaPlayer backgroungMusic;
Toast toast;
TextView textView;

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


    getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);



    //turn title off
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    // set full screen
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);

    setContentView(new GamePanel(this));

    backgroungMusic = MediaPlayer.create(Game.this, R.raw.music);
    backgroungMusic.setLooping(true);
    backgroungMusic.start();

}

@Override
protected void onPause()
{

    super.onPause();
    backgroungMusic.release();
    finish();
    System.exit(0);
}

@TargetApi(Build.VERSION_CODES.HONEYCOMB)
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {

    if (keyCode == KeyEvent.KEYCODE_BACK) {


        //  GamePanel.thread.setStoped(true);
        GamePanel.thread.setRunning(false);
        // in the next line of code we also style the dialog through xml which i put in styles
        AlertDialog alertDialog = new AlertDialog.Builder(this,R.style.myBackgroundStyle).create();
        alertDialog.setTitle("Exit Alert");
        alertDialog.setMessage("Do you really want to exit the Game?");
        alertDialog.setButton("Quit", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                //Best way is firstly use finish() and after that use System.exit(0) to clear static variables. It will give you some free space.
                // A lot of applications leave working processes and variables what makes me angry. After 30 minutes of using memory is full and i have to run Task Manager - Lvl 2 clear memory
                finish();
                System.exit(0);
                return;

            }
        });



        alertDialog.setButton2("Cancle", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {

                        // dialog.cancel();
                        // GamePanel.thread.resume();
                      // When user press the "Cancle" button then game resume for 3 seconds then start again
                   //     dialog.dismiss();
                        // Here is the Code of the toasts and each toast appear with delay of one second.

                        final Handler handler = new Handler();
                        final TextView textView = (TextView) findViewById(R.id.textView123);
                        final java.util.concurrent.atomic.AtomicInteger n = new AtomicInteger(3);
                        final Runnable counter = new Runnable() {
                            @Override
                            public void run() {
                                          if(n.getAndDecrement() >= 1 )
                                    handler.postDelayed(this, 1000);

                                else {
                                             GamePanel.thread.setRunning(true);
                                }
                            }
                        };
                        handler.postDelayed(counter, 1000);

                        return;
                    }
                }

        );


        alertDialog.show();

        return true;
    }
    return super.onKeyDown(keyCode, event);

}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_game, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}

}

这是xml布局中的textview。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".Game">

<TextView
    android:id="@+id/textView123"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"
    android:paddingRight="10dip"
    android:textSize="50dp" />

请帮助我谢谢!

2 个答案:

答案 0 :(得分:1)

您不需要dialog.dismiss();

您还遇到final TextView textView = (TextView) findViewById(R.id.textView123);正在寻找对话框视图而不是活动所在位置的问题的问题,Logcat中是否没有错误?您需要在某个时候致电textView.setText,但我希望您能获得NullPointerException

我要做的是使用对活动的回调,最好通过创建DialogFragment来实现。

public class MyDialogFragment extends DialogFragment {

    public MyDialogFragment newInstance() {
        MyDialogFragment fragment = new MyDialogFragment();
        return fragment;
    }

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity(), R.style.myBackgroundStyle);
        builder.setTitle("Exit Alert");
        builder.setMessage("Do you really want to exit the Game?");
        builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int id) {
                if (getActivity() instanceof DialogClickListener) {
                    ((DialogClickListener) getActivity()).onPositive();
                }
            }
        });
        return builder.create();
    }
}

public interface DialogClickListener {
    void onPositive();
}

public class MyActivity extends Activity implements DialogClickListener {

    private TextView textView; //rename this

    public void onCreate(...) {
        ...
        textView = (TextView) findViewById(R.id.textView123);
    }

    public void showExitDialog() {
         FragmentManager fm = getSupportFragmentManager();
         MyDialogFragment exitDlg = (MyDialogFragment) fm.findFragmetByTag(MyDialogFragment.TAG);
         if (exitDlg != null && exitDlg.isAdded()) {
             exitDlg.dismiss();
         }
         exitDlg = MyDialogFragment.newInstance();
         exitDlg.show(fm, AddEditDialogFragment.TAG);
    }

    public void onPositive() {
        final Handler handler = new Handler();
        final java.util.concurrent.atomic.AtomicInteger n = new AtomicInteger(3);
        final Runnable counter = new Runnable() {
            @Override
            public void run() {
                if (n.getAndDecrement() >= 1 ) {
                    handler.postDelayed(this, 1000);
                } else {
                    GamePanel.thread.setRunning(true);
                }
                textView.setText(String.valueOf(n));
            }
         };
         handler.postDelayed(counter, 1000);
    }

    public boolean onKeyDown(int keyCode, KeyEvent event) {

        if (keyCode == KeyEvent.KEYCODE_BACK) {
            showExitDialog()
        }
    }

}

答案 1 :(得分:-1)

在RelativeLayout中,您可以使用centerInParent:

print_car_and_speed