安卓app_sl3.2弹出消息对话框


package com.example.sl3_2a;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;

import android.widget.FrameLayout;
import android.widget.FrameLayout.LayoutParams;
import android.widget.TextView;





public class MainActivity extends Activity {

    public TextView text2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //setContentView(R.layout.activity_main);
        FrameLayout frameLayout=new FrameLayout(this);
        frameLayout.setBackgroundDrawable(this.getResources().getDrawable(R.drawable.background));
        setContentView(frameLayout);
        TextView text1=new TextView(this);
        text1.setText("在代码中控制UI界面");
        text1.setTextSize(TypedValue.COMPLEX_UNIT_PX,24);
        text1.setTextColor(Color.rgb(1, 1, 1));
        frameLayout.addView(text1);
        
        text2=new TextView(this);
        text2.setText("单击进入游戏。。。");
        text2.setTextSize(TypedValue.COMPLEX_UNIT_PX,50);
        text2.setTextColor(Color.rgb(1, 1, 1));
      //  LayoutParams params=new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
            //    ViewGroup.LayoutParams.WRAP_CONTENT);
     //   params.gravity=Gravity.CENTER_HORIZONTAL|Gravity.CENTER_VERTICAL;
        LayoutParams params = new LayoutParams(
                ViewGroup.LayoutParams.WRAP_CONTENT,
                ViewGroup.LayoutParams.WRAP_CONTENT); // 创建保存布局参数的对象
        params.gravity = Gravity.CENTER_HORIZONTAL | Gravity.CENTER_VERTICAL;
        text2.setLayoutParams(params);
        text2.setOnClickListener(new OnClickListener()
                {
                   @Override
                   public void onClick(View v)
                   {
                       new AlertDialog.Builder(MainActivity.this).setTitle("系统提示")
                       .setMessage("游戏有风险,进入需谨慎,确认进入吗?")
                       .setPositiveButton("确定", new DialogInterface.OnClickListener() 
                       {
                        
                        @Override
                        public void onClick(DialogInterface dialog, int which) 
                        {
                            Log.i("3.2","进入游戏");
                            // TODO 自动生成的方法存根
                            
                        }
                       }).setNegativeButton("退出", new DialogInterface.OnClickListener() {
                        
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            // TODO 自动生成的方法存根
                            Log.i("3.2","退出游戏");
                            
                        }
                    
                    }).show();
                   }
                });
        
        frameLayout.addView(text2);                // 将text2添加到布局管理器中
        
        
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, 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();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}