• 第三章 用户界面设计


    3.1.2 在代码中控制ui界面

    package com.example.administrator.a11;
    
    import android.content.DialogInterface;
    import android.graphics.Color;
    import android.graphics.drawable.Drawable;
    import android.support.v7.app.ActionBar;
    import android.support.v7.app.AlertDialog;
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.util.Log;
    import android.util.TypedValue;
    import android.view.Gravity;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.FrameLayout;
    import android.widget.TextView;
    
    public class MainActivity extends AppCompatActivity {
    
        public TextView text2;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            //setContentView(R.layout.activity_main);
    
            //在MainActivity的OnCreate()方法中,创建一个帧布局管理器,并未改布局管理器设置背景
            FrameLayout frameLayout=new FrameLayout(this);//创建帧布局管理器
            frameLayout.setBackgroundColor(Color.BLACK);//设置背景
            setContentView(frameLayout);//设置在Activity中显示frameLayout
    
            TextView text1=new TextView(this);
            text1.setText("在代码中控制UI界面");//设置显示的文字
            text1.setTextSize(TypedValue.COMPLEX_UNIT_PX,50);//设置文字大小,单位为像素
            text1.setTextColor(Color.rgb(100,1,1));//设置文字的颜色
            frameLayout.addView(text1);//将text1添加到布局管理器中
    
    
            TextView text2 = new TextView(this);
            text2.setText("单机进入游戏......");//设置显示文字
            text2.setTextSize(TypedValue.COMPLEX_UNIT_PX,50);//设置文字大小,单位像素
            text2.setTextColor(Color.rgb(100,1,1));//设置文字颜色
            text2.setGravity( Gravity.CENTER_HORIZONTAL | Gravity.CENTER_VERTICAL);;//设置居中显示
    
            //为text2添加单击事件监听器
            text2.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    new AlertDialog.Builder(MainActivity.this).setTitle("系统提示")//设置对话框的标题
                            .setMessage("游戏有风险,进入需谨慎,真的要进入吗?")//设置对话框的显示内容
                            .setPositiveButton("确定",//为确定添加单击事件
                            new DialogInterface.OnClickListener(){
                                public void onClick(DialogInterface dialog,int which)
                                {
                                    Log.i("3.2","进入游戏");//输出消息日志
                                }
                            })
                            .setNegativeButton("退出",//为退出按钮添加单击事件
                            new DialogInterface.OnClickListener()
                            {
                                public void onClick(DialogInterface dialog,int which)
                                {
                                    Log.i("3.2","退出游戏");
                                    finish();//j结束游戏
                                }
                            }).show();//显示对话框
                }
            });
    
    
            frameLayout.addView(text2);
    
        }
    }
  • 相关阅读:
    C语言中for循环的使用
    详解C语言的main函数
    计算机语言的发展(the history of computer's language)
    hdu 1001
    hoj 1002
    hdu 1000
    POJ 1000(水题)
    hoj 1001
    code hunt题解(1)
    《C和指针》学习笔记(3)
  • 原文地址:https://www.cnblogs.com/xiaochi/p/6409459.html
Copyright © 2020-2023  润新知