Activity学习日记(二)

                                  Activity学习日记(二)

  • 主要内容
  1.     UI布局--四种布局
  2.     UI控件的事件处理
  3.     UI常用控件及使用
  4.     Toast--吐司
  5.     dailoge--对话框

View的概念

        a,用来显示数据、影像或是其他信息的组件,组件全部继承与View
           ViewGroup是一种View容器,本身也是一种View,但是可以包含View及其他ViewGroup组件的View,
           例如: LinearLayout,ViewGroup继承自View,所以ViewGroup is-aView的观念,只是
           ViewGroup有容器的特色。
        b,UI组件都放在android.widget包,android.view包中,

        c,UI编程方式
            1.通过java代码创建view
            2.用xml文件(ui语言)--用的比较多
    2,布局对象
        a, activtiy与layout的关系:
            Activity就是布满整个窗口或者悬浮于其他窗口上的交互界面
            一般一个Activity都有一个以上的layout,用于摆放要显示的组件
        常用的几个布局:
            线性:LinearLayout
            表格:TableLayout
            相对:RelativeLayout
            以上布局都可以嵌套使用
        
        xml布局文件和属性:
            xml布局文件:
                1,必须在res/layout目录
                2,xml中的根节点必须是xmlns:android="http://schemas.android.com/apk/res/android"
                    XML命名空间,告诉Android开发工具你准备使用Android命名空间里的一些通用属性

                3,xml中的每个组件的id会在R类中生成对应的变量,在代码中可以引用的到
                4,需要在Activity的onCreate方法中调用setContentView(R.layout.main)显示xml中的view
            xml中的属性
                长度单位:px,像素,表示屏幕的实际像素,比如320*480  (很少用)
                      dp(dip) 是屏幕的物理尺寸, 大小为1英寸的1/72
                      sp(与刻度无关的像素),
                      技巧:长度和高度: 可以选择dp/sp, 如果是字体的话,用sp
                 android:layout_width="68dp"   // 是指定控件的显示大小的区域
                 android:layout_height="94dp"

                layout_margin: 是控件边缘相对于父空间的边距,有top,buttom,left,right
                (注意哦,如果选择了对齐方式,比如居中对齐,margin=0dp时,是以中间为开始的,不是从手机的顶部开始的)
                layout_padding: 是控件内容相对于空间边缘的边距
                layout_gravity : 设置组件相对容器(layout)的对齐方式
                gravity: 设置View组件(即是控件中内容)的对齐方式
                "wrap_content" : 填满 父控件空白
                match_parent 和fill是一样的,刚好显示空间中的内容
                android:layout_weight: 各个空间在布局中的比重
                @+id/test :为组件设定id
    ----------------------------------------------------------------------------------
    线性布局:
        掌握点
        0,控件是依次一个一个的摆放
        1,在xml文件中通过<LinearLayout></LinearLayout>来表示
        2,分为垂直和水平布局特性,每行每列只能有一个组件
        3,子view中的gravity属性和weight属性
        4,layout_weight:layout中控件在某个方向上的占用比例
        5,线性布局是可以嵌套
    属性解释:
        layout_weight属性:权重,默认为0,意思是需要显示多大的视图就占据多大的屏幕空间
                最好和wrap_content配合使用
            
            在一个容器中的view的占用比例为view的权重/所有view权重之和
            比如有三个view: v1 :1
                    v2:2   :该view占用整个容器的2/4
                    v3:1   
        layout_gravity属性:在整个容器中的对齐方式:上下左右居中等等
    例子:
    <LinearLayout 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:orientation="vertical" >

        <Button
        android:id="@+id/test"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="right"
        android:gravity="right"
        
        android:text="这是一个测试" />

        <!-- 里面在包含两个大的线性布局 , 横向一个比重1,一个比重3 -->

        <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <!-- 里面在包含两个小的线性布局 , 垂直,比重1:1 -->

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:orientation="vertical" >

            <!-- 这个layout中有三个按钮, 垂直 -->

            <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:orientation="horizontal" >

            <Button
                android:id="@+id/seven"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="7" />

            <Button
                android:id="@+id/eight"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="8" />

            <Button
                android:id="@+id/nine"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="9" />
            </LinearLayout>

            <!-- 这个layout中有两个按钮  横向 -->

            <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:orientation="horizontal" >

            <Button
                android:id="@+id/zero"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="0" />

            <Button
                android:id="@+id/point"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="2"
                android:text="." />
            </LinearLayout>
        </LinearLayout>

        <!-- 这个layout中只有一个按钮 -->

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="3"
            android:orientation="vertical" >

            <Button
            android:id="@+id/equal"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:text="=" />
        </LinearLayout>
        </LinearLayout>

    </LinearLayout>

    计算器实例
    线性布局不易嵌套过多,过多会影响运行速度
    ---------------------------------------------------------------
    表格布局:
        掌握点
        1,有行和列,继承了linearLayout,在创建表格的时候不需要明确声明包含多少行和列,
          而是通过tableRow和其他组件来控制行和列数据
        2,tablerow表示的就是一个表格行,也是一个容器,可以往里面添加组件,添加一个就算是一个列
        3,同时如果添加一个另外的组件,该组件占用一行
        4,列宽是由该列中最宽的那个单元格决定
        5,TableLayout所特有的单元格中组件三种行为:(设置是在TableLayout标签,效果影响tablerow)
            a,shrinkableColumns,该列能收缩,以适应父容器的大小(内容过多,则收缩)
                android:stretchColumns="0"
            b,stretchableColumns,该列的宽度可以拉伸,保证组件能填满父容器(有空白则填充)
                android:stretchColumns="0,2,3"
            c,collapsedColumns: 该列会被隐藏(索引列从0开始)

        6,行中的组件的id从0开始
    例子:
    <LinearLayout 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:orientation="vertical" >

        <TableLayout
        android:id="@+id/tablelayout1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:stretchColumns="0" >
        
        <TableRow
            android:id="@+id/tablerow1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="#ffffff" >

            <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="#fd8d8d"
            android:text="你好呀"
            android:textColor="#000000" />
        </TableRow>
        </TableLayout>

        <TableLayout
        android:id="@+id/tablelayout2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:stretchColumns="0,2,3" >

        <TableRow
            android:id="@+id/tablerow2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" >

            <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="button1" />

            <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="button2" />

            <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="button3" />

            <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="button4" />
        </TableRow>
        </TableLayout>

    </LinearLayout>
    --------------------------------------------------------------
    frameLayout: 也叫堆栈布局,视图以层叠的方式显示,一层一层的显示,一般可以用于设置背景
    比如一个背景,上面有按钮,当按钮按下后就可以直接播放音乐
    最后一个视图放在顶端
    第一个视图放在最低层

    属性:
        top:将视图放在屏幕的顶端
        button: 将视图放在屏幕的底端
        left
        right
        center_vertical: 视图垂直居中
        horizontal_vertical:视图水平居中

    例子: 比如说要做背景,或者是一个合成图
        
    <FrameLayout 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"
            >
            <!-- 前一个比后一个大,后一个会叠加在前一个上面 -->
            <TextView
            android:id="@+id/tv_04"
            android:layout_width="180sp"
            android:layout_height="180sp"
            android:layout_gravity="center"
            android:background="#0f0"
            android:text="04" />
            
            <TextView
            android:id="@+id/tv_03"
            android:layout_width="160sp"
            android:layout_height="160sp"
            android:layout_gravity="center"
            android:background="#ff0"
            android:text="03" />
            <TextView
            android:id="@+id/tv_02"
            android:layout_width="140sp"
            android:layout_height="140sp"
            android:layout_gravity="center"
            android:background="#f0f"
            android:text="02" />
            <TextView
            android:id="@+id/tv_01"
            android:layout_width="120sp"
            android:layout_height="120sp"
            android:layout_gravity="center"
            android:background="#0ff"
            android:text="01" />
            

        </FrameLayout>
    -------------------------------------------------------------
    相对布局:
    在配置布局xml文件的时候,都需要考虑该布局在横竖屏不同状态下的显示样式,
    尽可能的将一个xml文件即适应于横屏,又适应于竖屏。这就要求在xml文件中尽量少使用
    类似于“50dip”,”“13px”这样的硬性数据
    相对布局:某个控件的位置是相对于另外一个控件做参照物的
    属性:  
    根据父容器来定位:
        左对齐:android:layout_alighParentLeft
        右对齐:android:layout_alighParentRight
        顶端对齐:android:layout_alighParentTop
        底部对齐:android:layout_alighParentBottom
        水平居中:android:layout_centerHorizontal
        垂直居中:android:layout_centerVertical
        中央位置:android:layout_centerInParent
    根据兄弟组件来定位(根据兄弟组件的id)
        左边:android:layout_toLeftOf
        右边:android:layout_toRightOf
        上方:android:layout_above
        下方:android:layout_below
        对齐上边界:android:layout_alignTop
        对齐下边界:android:layout_alignBottom
        对齐左边界:android:layout_alignLeft
        对齐右边界:android:layout_alignRight
    Margin和Padding属性
        Margin:设置组件与父容器(通常是布局)的边距
            android:layout_margin: 指定控件的四周的外部留出一定的边距
            android:layout_marginLeft: 指定控件的左边的外部留出一定的边距
            android:layout_marginTop: 指定控件的上边的外部留出一定的边距
            android:layout_marginRight: 指定控件的右边的外部留出一定的边距
            android:layout_marginBottom: 指定控件的下边的外部留出一定的边距

            android:layout_marginLeft="100dp"
            
        Padding:设置组件内部元素间的边距(可以理解为填充)
            android:padding :指定控件的四周的内部留出一定的边距
            android:paddingLeft: 指定控件的左边的内部留出一定的边距
            android:paddingTop: 指定控件的上边的内部留出一定的边距
            android:paddingRight: 指定控件的右边的内部留出一定的边距
            android:paddingBottom: 指定控件的下边的内部留出一定的边距

            android:paddingLeft="100dp"
        
        
    一般没有android:orientation
    必须有一个id: @+id/xxx
    例子:
        A                         B
            中心
        C                     D
-----------------------------------------------------------------------------------------------------------------------
android中UI的事件处理机制和编程方式:
    为什么要有事件处理?
        当用户在程序界面上执行了各种操作时,应用程序必须为用户动作提供相应
        的响应动作,这种相应动作就需要通过事件处理来完成,比如按下某个控件,
        拖动某个控件,对应应用程序可以做出不同的响应
    UI如何处理响应事件?
            1,引用Java事件处理机制,包括事件,事件源和事件监听器
            2,事件可以是鼠标,键盘,触摸事件,比如单击,双击,长按,拖动等等
            3,事件源是产生事件的组件,比如Button,ProgressBar,CheckBox等等
            4,事件监听器是组件产生事件时响应的接口,事件产生后的处理方法
    android系统提供了哪些事件处理方式?
        两种:
            1,基于回调的事件处理方式(不推荐,了解)
                android中的每个控件本身自带处理事件的回调方法,当用户在该对象上,发生了动作,Android系统框架的代码会自动去调用
                比如,当一个视图(如一个按钮)被触摸时,该对象上的onTouchEvent()方法会被调用
                缺点:不够灵活,很明显,扩展每个你想使用的视图对象(只是处理一个事件)是荒唐的,因为如果有一百个控件,就要重写
                    一般个控件的回调方法,麻烦的很
                    public interface Callback { 
                            boolean onKeyDown(int keyCode,KeyEvent event):当用户在该组件上按下某个键时触发的方法。
                            boolean onKeyLongPress(int keyCode,KeyEvent event):当用户在该组件上长按某个按钮时触发该方法。
                            boolean onKeyShortcut(int keyCode,KeyEvent event): 当一个快捷键事件发生时触发该放过。
                            boolean onKeyUp(int keyCode,KeyEvent event):当用户在该组件上松开某个按键时触发该方法
                            boolean onTouchEvent(MotionEvent event):当用户在该组件上触发触摸屏事件时触发该方法。
                            boolean onTrackballEvent(MotionEvent event):当用户在该组件上触发轨迹球屏事件时触发该事
                    }
            2,基于监听的事件处理方式
                    ui控件自己去处理事件不好办,那就交给别人去办,通过观察者模式,为Android界面组件绑定特定的事件监听器
                    这种处理方式将事件源和事件监听器分离,有利于提供程序的可维护性
                    每个组件可以针对特定的事件指定一个事件监听器来处理,每个事件监听器也可以监听一个或多个事件源头
    
    基于监听的事件处理(第二种)编程方式:
            基本步骤:
                a,获取普通组件(事件源),也就是被监控的对象
                b,实现事件监听类,比如xxxListener
                c,调用setOnXXXListener
            
            不同的实现方法:
                    1,匿名类:
                                btn1.setOnclickListener(new OnclickListener(){
                                            public void onClick(View v){
                                            // 要执行的操作
                                               }
                                })
                    2,直接构建一个OnxxListener:
                                private OnClickListener mCorkyListener = new OnClickListener() {
                                            public void onClick(View v) {
                                              // do something when the button is clicked
                                            }
                                };

                                protected void onCreate(Bundle savedValues) {
                                        Button button = (Button)findViewById(R.id.corky);
                                        //设置监听
                                        button.setOnClickListener(mCorkyListener);
                                }
                    
                    3,在Activity组件中实现xxxListener的接口,这样在同一个窗口中多个控件共享一个处理接口,相对方便
                    但是容易导致Activity工作过于繁琐,因为Activity的工作应该是完成界面的初始化,而不必要去处理UI的事件
                             public class TestMedia extends Activity implements OnClickListner{
                                           Button btn1=(Button)findViewById(R.id.myButton1);
                                           Button btn2=(Button)findViewById(R.id.myButton2);
                                           btn1.setOnclickListener(this);
                                           btn2.setOnclickListener(this);
                                }
                                public void onClick(View v){
                                           switch (v.getId()){
                                              case R.id.myButton1;
                                              //要执行的动作1
                                              break;
                                              case R.id.myButton2;
                                              //要执行的动作2
                                              break;
                                           }
                                    }

----------------------------------------------------------------------------------------
TextView和EditText
            1,用于文字显示(TextView)或输入EditText
            2,TextView其实是一个文本编译器,但是关闭了编辑功能,
                EditText和Button继承于TextView,
            
        TextView的xml属性:(可以参考API文档)
             android:id="@+id/textView2"
             android:layout_width="match_parent"
             android:layout_height="wrap_content"
             android:textSize="20sp"
             android:textColor="#669933"
             android:drawableRight="@drawable/ic_launcher"
             android:autoLink="phone|email"
             android:text="信息: 12306; www.12306.cn"
        代码中:
            setText();
            getText().toString();
        
        EditText对内容可以进行限制:
            android:digits="1234567890.+-*/%\n()"
                限制输入框中只能输入自己定义的这些字符串 如果输入其它将不予以显示
            android:phoneNumber="true" 
                限制输入框中只能输入手机号码
            android:password="true" 
                限制输入框中输入的任何内容将以"*"符号来显示
            android:hint="默认文字" 
                输入内容前默认显示在输入框中的文字
            android:textColorHint="#FF0000"
                设置文字内容颜色
            android:enabled="false" 
                设置输入框不能被编辑 
            android:text="在图片下方" 
            android:drawableBottom="@drawable/jay" 
                可以显示图片,比如在文字的下面显示一张图片
            
        

        radioButton(单选按钮), checkbox(复选框), ToggleButton(开关按钮), 都是继承了Button
        前两个有一个可选中的功能,所以android:checked属性可以设置
        
        radioButton用于多选一,如果想在选中的某一个选项按钮后,其他的选项都设置为未选择的状态,
        需要将<RadioButton>添加到<RadioGroup>标签中
        特性:
            1,单个RadioButton在选中后,通过点击无法变为未选中
            2,在没有RadioGroup的情况下,RadioButton可以全部都选中;
               当多个RadioButton被RadioGroup包含的情况下,RadioButton只可以选择一个
            3,RadioButton表示单个圆形单选框,而RadioGroup是可以容纳多个RadioButton的容器
            4,大部分场合下,一个RadioGroup中至少有2个RadioButton
            5,大部分场合下,一个RadioGroup中的RadioButton默认会有一个被选中,并建议您将它放在RadioGroup中的起始位置
            6,在radiogroup里面我们也可以使用RelativeLayout,LinearLayout这样的布局的    
        xml的属性:
            android:checked="true" 
                表示初始化的时候是否被选中,默认选择
            
        1,布局xml文件
        <RadioGroup
            android:id="@+id/radioGroup1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" >

            <RadioButton
                android:id="@+id/male"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:checked="true"
                android:text="男" />

            <RadioButton
                android:id="@+id/female"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="女" />

            <RadioButton
                android:id="@+id/nosex"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="人妖" />
            </RadioGroup>
             <!-- 之间画条横线 --> 
            <View   
            android:layout_width="match_parent"   
            android:layout_height="1dp" 
            android:background="#ffffff" 
            /> 
            <Button 
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/btnGetRet"
            android:text="获取当前选择的性别"
            />
        监听事件:
        OnCheckedChangeListener radioListener = new OnCheckedChangeListener(){
            public void onCheckedChanged(RadioGroup group, int checkedId)
                 if(checkedId==R.id.radioBtn1){                    
                    Toast.makeText(MainActivity.this, "你来自广东省", Toast.LENGTH_LONG).show();  
                 }  
                //获取变更后的选中项的ID
                int radioButtonId = group.getCheckedRadioButtonId();
                //根据ID获取RadioButton的实例
                 RadioButton rb = (RadioButton)MyActiviy.this.findViewById(radioButtonId);
                 tv.setText("您的性别是:" + rb.getText());
        }
        RadioGroup group = (RadioGroup)this.findViewById(R.id.radioGroup);
        group.setOnCheckedChangeListener(radioListener);
        

        -------------------------------------------------------------------------------
        checkbox:复选框,默认情况是未选中状态:
        xml的属性:
             android:checked="false"   <!-- 缺省为flase,可以不进行说明 --> 
        例子:
        <LinearLayout 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:orientation="vertical" >    
            <CheckBox
            android:id="@+id/cb_red"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" 
            android:text="red"/>
               <CheckBox
            android:id="@+id/cb_green"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="green" />
              <CheckBox
            android:id="@+id/cb_blue"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="blue" />
        </LinearLayout>

        监听事件:和上面的稍微有点不一样
        android.widget.CompoundButton.OnCheckedChangeListener checkListener 
            public void onCheckedChanged(CompoundButton buttonView,boolean isChecked)
            {
                

                            //buttonView     选中状态发生改变的那个按钮
                            //isChecked        按钮新的状态
                if(isChecked){
                }
            }
        beijing=(CheckBox)findViewById(R.id.beijing); 
        beijing.setOnCheckedChangeListener(checkListener);
    --------------------------------------------------------------
        ToggleButton:带开关的按钮,选中和未选择状态,并且需要为不同的状态设置不同的操作
        xml属性:
             android:checked="true"  
            android:textOn="灯亮"
            android:textOff="灯灭"
        例子:
        <ToggleButton
            android:id="@+id/tgBtn1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textOn="灯亮"
            android:textOff="灯灭"
        />
        监听事件
        android.widget.CompoundButton.OnCheckedChangeListener checkListener 
            public void onCheckedChanged(CompoundButton buttonView,boolean isChecked){
            if(isChecked){//true时  
                test.setOrientation(1);// 1 的话,设置为纵向  
             }else{//false时  
                test.setOrientation(0);// 0 的话,设置为横向  
                }  
        }
        ToggleButton toggle = (ToggleButton) this.findViewById(R.id.toggle);  
        final LinearLayout test = (LinearLayout) this.findViewById(R.id.test);  
        toggle.setOnCheckedChangeListener(checkListener);
    
------------------------------------------------------------------------------------------------
动态操作布局文件和通过代码动态产生ui控件:

动态操作布局文件: LayoutInflater 
        LayoutInflater适用于初始化Layout布局xml文件,不可以直接new出来

        //1,通过Framework中的服务管理器中获取到inflater服务实例对象
        LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);  
        // 2,通过inflater服务实例对象加载xml布局文件
        View layout = inflater.inflate(R.layout.main, null);  
        例子:
            onCreate
                |
                LayoutInflater inflater = LayoutInflater.from(this);  
                View layout = inflater.inflate(R.layout.main, null); 

                et = (EditText) layout.findViewById(R.id.edittext);
                et.setBackgroundColor(Color.YELLOW);  
                btn = (Button) layout.findViewById(R.id.btn);  
                btn.setBackgroundColor(Color.CYAN);  
                
                setContentView(layout);  //findViewById一般都是在该函数之后才能适用,而现在可以在之前适用

动态产生ui控件: 
Android 硬编码-代码来实现UI界面:
        好处:从单独语言和简单程序来说具有运行效率高和设计简单等好处
        坏处:代码和ui没有分离,耦合性强,对于复杂的工程还是建议MVC方式设计比较合理,即在xml文件中控制ui
    代码实现:
        onCreate()
            |
            //setContentView(R.layout.main); 不需要xml文件了
            //视图的参数,可以用来设置视图中属性,类似android:layout_height="fill_parent"
            LayoutParams params=new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT);
            //创建布局
            LinearLayout layout=new LinearLayout(this);
            layout.setOrientation(LinearLayout.VERTICAL);
            
            //创建一个TextView
            TextView tv=new TextView(this);
            tv.setText("This is a TextView");
            tv.setLayoutParams(params);

            //创建一个Button
            Button btn=new Button(this);
            btn.setText("This isa Button");
            btn.setLayoutParams(params);

            btn1.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    tv1.setText("Hello,Java Activity! " + new java.util.Date());
                }
            });


            //向布局中添加TextView
            layout.addView(tv);
            //向布局中添加Button
            layout.addView(btn);
            
            //创建布局使用的属性
            LinearLayout.LayoutParamslayoutParam= new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT);
            //将layout显示出来,LinearLayout对象添加到Activity中去
            this.addContentView(layout,layoutParam);
            //或者用这个
            setContentView(linear);

    注意:以上的ui对象都是通过new创建出来,然后用容器来盛装,任何UI都需要传入this参数,即Context。
    UI通过context获取Android应用环境全局info。

-----------------------------------------------------------------------------------------------------------------
    Toast:向用户提供简单的提示信息    
            1,提示信息不会获取焦点
            2,提示信息过一段时间后会消失
          使用:
            1,创建Toast对象:
            2,设置消息的内容和显示时间ms
            3,通过Toast的show()方法显示出来
         使用方法:
        1,默认
            Toast.makeText(getApplicationContext(), "默认的Toast", Toast.LENGTH_SHORT).show();
        2,设定显示位置
            Toast toast=Toast.makeText(getApplicationContext(), "自定义显示位置的Toast", Toast.LENGTH_SHORT);
            toast.setGravity(Gravity.TOP|Gravity.CENTER, -50, 100);   
             //第一个参数:设置toast在屏幕中显示的位置。我现在的设置是居中靠顶  
             //第二个参数:相对于第一个参数设置toast位置的横向X轴的偏移量,正数向右偏移,负数向左偏移  
             //第三个参数:同的第二个参数道理一样  
             //获取一个用于显示的view,并将其设置成layout布局
             LinearLayout layout  = (LinearLayout) toast.getView();
            ImageView imageView = new ImageView(ToastActivity.this);
            imageView.setImageResource(R.drawable.ic_launcher);
            layout.addView(imageView);
             toast.show();  
        
    对话框:
            AlertDialog:跟用户进行交互,方便用户可操作,比如玩游戏退出时就会有对话框进行选择
            界面描述:
                1,图标区域
                2,标题区域
                3,内容区域
                4,按钮区域
            创建步骤:
                1,创建一个AlertDialog.Builder
                    AlertDialog.Builder builder=new AlertDialog.Builder(this);
                2,调用builder.setTitle("this is title")设置标题
                3,调用builder.setIcon();设置图标
                4,调用builder.setMessage设置文本内容,当然也可以设置其他
                5,调用builder.setPositiveButton()--确定,取消

                6, 利用create创建,再调用show()方法显示
                    builder.create();

  • log.d 使用以及TAG标签使用
package com.hq.hello;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;

public class MainActivity extends Activity {
	private final String TAG = "HelloMainActivity";
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		//setContentView(R.layout.activity_test);
		
		System.out.println("My hello app log");
		
		//参数1--标签--将自己的调试信息表示为唯一
		Log.d(TAG, "---> mytest debug log");
		Log.i(TAG, "---> mytest info log");
		Log.w(TAG, "---> mytest warning log");
		Log.e(TAG, "---> mytest error log");
	
		
	}

	@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);
	}
}
  • 线性布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <EditText
        android:id="@+id/editText1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10" >

        <requestFocus />
    </EditText>

    <LinearLayout
        android:id="@+id/layout_eqaul_789"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <!-- 7, 8, 9, 0, . -->

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="vertical" >

            <!-- 7, 8, 9 -->

            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:orientation="horizontal" >

                <Button
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="7" />

                <Button
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="8" />

                <Button
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="9" />
            </LinearLayout>

            <!-- 0, . -->

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="horizontal" >

                <Button
                    android:layout_weight="8"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="0" />

                <Button
                     android:layout_weight="1"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="." />
            </LinearLayout>
        </LinearLayout>

        <Button
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:text="=" />
    </LinearLayout>

</LinearLayout>
  • 相对布局
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <TextView
        android:id="@+id/tv_user_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="18dp"
        android:layout_marginTop="34dp"
        android:text="用户名"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <EditText
        android:id="@+id/et_username"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/tv_user_name"
        android:layout_alignBottom="@+id/tv_user_name"
      	android:layout_toRightOf="@id/tv_user_name"
        android:layout_marginLeft="30dp"
        android:ems="10"
        android:inputType="textPersonName" >

        <requestFocus />
    </EditText>

    <TextView
        android:id="@+id/tv_passwd"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/tv_user_name"
        android:layout_below="@+id/tv_user_name"
        android:layout_marginTop="59dp"
        android:text="密  码"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <EditText
        android:id="@+id/et_passwd"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/tv_passwd"
        android:layout_alignLeft="@+id/et_username"
        android:ems="10"
        android:inputType="textPassword" />

    <CheckBox
        android:id="@+id/cb_mark_pwd"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/tv_passwd"
        android:layout_centerVertical="true"
        android:text="记住密码" />

    <CheckBox
        android:id="@+id/cb_auto_login"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/cb_mark_pwd"
        android:layout_marginLeft="45dp"
        android:layout_toRightOf="@+id/cb_mark_pwd"
        android:text="自动登录" />

    <Button
        android:id="@+id/btn_login"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/cb_mark_pwd"
        android:layout_below="@+id/cb_mark_pwd"
        android:layout_marginTop="67dp"
        android:text="登录" />

    <Button
        android:id="@+id/btn_exit"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/btn_login"
        android:layout_alignRight="@+id/et_username"
        android:layout_marginRight="16dp"
        android:text="退出" />

</RelativeLayout>
  • 表格布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" 
    android:orientation="vertical"
    >
    
	<TableLayout
	     android:layout_width="match_parent"
	     android:layout_height="wrap_content"
	     >
	     
	    
	    <TableRow 
	        android:layout_marginTop="20dp"
	        android:layout_marginLeft="10dp"
	        android:layout_marginRight="10dp"
	         android:layout_width="match_parent"
	    	 android:layout_height="wrap_content"
	        >
	        
	        <TextView 
	            android:layout_width="wrap_content"
	            android:layout_height="wrap_content"
	            android:text="价格"
	            />
	        
	        <EditText 
	            android:layout_width="wrap_content"
	            android:layout_height="wrap_content"
	            android:hint="请输入价格"
	         	android:ems="6"
	            />
	        
	         <TextView 
	            android:layout_width="wrap_content"
	            android:layout_height="wrap_content"
	            android:text="建筑面积"
	            />
	        
	        <EditText 
	            android:layout_width="wrap_content"
	            android:layout_height="wrap_content"
	            android:hint="请输入面积"
	         	android:ems="6"
	            />
	        
	    </TableRow>
	    
	    
	    <TableRow 
	       android:layout_marginTop="20dp"
	        android:layout_marginLeft="10dp"
	        android:layout_marginRight="10dp"
	         android:layout_width="match_parent"
	    	 android:layout_height="wrap_content"
	        >
	        
	        <TextView 
	            android:layout_width="wrap_content"
	            android:layout_height="wrap_content"
	            android:text="买方"
	            />
	        
	        <TextView 
	            android:layout_width="wrap_content"
	            android:layout_height="wrap_content"
	            android:text="        "
	            />
	        
	         <TextView 
	            android:layout_width="wrap_content"
	            android:layout_height="wrap_content"
	            android:text="卖方"
	            />
	        
	        <TextView 
	            android:layout_width="wrap_content"
	            android:layout_height="wrap_content"
	            android:text="        "
	            />
	        
	    </TableRow>
	    
	    <TableRow 
	        android:layout_marginTop="20dp"
	        android:layout_marginLeft="10dp"
	        android:layout_marginRight="10dp"
	         android:layout_width="match_parent"
	    	 android:layout_height="wrap_content"
	        >
	        
	        <TextView 
	            android:layout_width="wrap_content"
	            android:layout_height="wrap_content"
	            android:text="合计"
	            />
	        
	        <EditText 
	            android:layout_width="wrap_content"
	            android:layout_height="wrap_content"
	         	android:ems="6"
	            />
	        
	         <TextView 
	            android:layout_width="wrap_content"
	            android:layout_height="wrap_content"
	            android:text="合计"
	            />
	        
	        <EditText 
	            android:layout_width="wrap_content"
	            android:layout_height="wrap_content"
	         	android:ems="6"
	            />
	        
	    </TableRow>
	    
	</TableLayout>

	<Button
	    android:id="@+id/button1"
	    android:layout_width="140dp"
	    android:layout_height="wrap_content"
	    android:paddingLeft="120dp"
	    android:text="计算" />

</LinearLayout>

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值