android学习笔记

来源:互联网 发布:模拟退火算法c 编辑:程序博客网 时间:2024/05/22 06:56

1.Activity启动过程

  (1)Android操作系统首先访问AndroidManifest.xml,了解这个程序默认的Activity是哪一个;

  (2)生成默认的Activity对象,并调用这个Activity的onCreate()方法;

  (3)在onCreate()方法中调用setContentView(R.layout.activity_main);方法,该方法接收布局文件(R.layout.activity_main)的id;

  (4)布局文件R.layout.activity_main决定在Activity上显示什么样的内容(控件及其大小、内容等);



2.代表控件的对象

 

每个控件都对应着一个对象,使用findViewById()方法可以得到控件的对象,通过对象幅值可以改变该控件。



3,控件对象的方法

使用findViewById()方法找到TextView对象之后,可以使用setText(),setBackgroundColor()等方法对控件对象进行设置。

        textView = (TextView)findViewById(R.id.textView);        textView.setText("Hello KuKuHaoGe!!!");        textView.setBackgroundColor(Color.BLUE);


4,为控件绑定监听器的步骤

  (1) 获取代表控件的对象,如2所说,使用findViewById()方法

button = (Button)findViewById(R.id.button);

  (2)定义一个类,实现监听接口

    class ButtonListener implements OnClickListener{    ......    }

  (3)生成监听器对象,也就是说显示监听接口的函数

    class ButtonListener implements OnClickListener{@Overridepublic void onClick(View arg0) {// TODO Auto-generated method stubcount++;textView.setText(count + "");}        }

  (4)为控件绑定监听对象

        ButtonListener buttonListener = new ButtonListener();        button.setOnClickListener(buttonListener);

package com.marschen05.s01_e05_view;import android.os.Bundle;//import 其他包省略 public class MainActivity extends ActionBarActivity {        //生成控件private TextView textView;private Button button;//自动导入包快捷键 ctrl+shift+oint count = 0;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        //获取控件对象        textView = (TextView)findViewById(R.id.textView);        button = (Button)findViewById(R.id.button);                //生成实现监听接口类的对象        ButtonListener buttonListener = new ButtonListener();        //为控件绑定监听对象        button.setOnClickListener(buttonListener);         }    //定义一个类,实现监听接口    class ButtonListener implements OnClickListener{@Overridepublic void onClick(View arg0) {// TODO Auto-generated method stubcount++;textView.setText(count + "");}     }    //......省略其他函数 }



5,控件布局

所谓控件布局方法是指控制控件在Activity当中的位置、大小、颜色以及其他控件样式属性的方法,可以

  (1)使用布局文件完成控件布局

  (2)在java代码当中完成控件布局

分类:Linear Layout 和Relative Layout

也可分为:List View 和Grid View



6,距离单位

  (1)px即为像素

  (2)dp的换算公式如下

   

   

dp不会因为用户设置字体大小的改变或者不同屏幕分辨率的不同而改变,所以一帮用来设置控件的高度和宽度

  (3)sp (scaled pixels)

sp单位通常用于指定字体的大小

当用户修改手机显示字体时,sp会随之改变



7,内边距和外边距


8,多选按钮(CheckBox及其常用监听方法)

  (1)多选按钮可在activity_main.xml中设置

<CheckBox         android:id="@+id/dotaId"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="dota"/>

  (2)OnClickListener监听器(监听步骤见4)

OnClickListener类的实现

class OnBoxClickListener implements OnClickListener{@Overridepublic void onClick(View view) {CheckBox box = (CheckBox)view;if(box.getId() == R.id.eatId){System.out.println("eatBox");}else if(box.getId() == R.id.sleepId){System.out.println("sleepBox");}else if(box.getId() == R.id.dotaId){System.out.println("dotaBox");}if(box.isChecked()){System.out.println("Checked");}elseSystem.out.println("Unchecked");System.out.println("CheckBox is clicked");}    }
其实现函数onClick(View view){}中穿来的参数为监听到的被点击的对象,对该对象使用view.getId()可以获得此对象的id并判断是哪个对象;可以使用view.isChecked()判断该对象是否被选中


  (3)OnCheckedChangeListener监听器,其实现函数如下:

    class CheckBoxListener implements OnCheckedChangeListener{@Overridepublic void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {// TODO Auto-generated method stubif(buttonView.getId() == R.id.eatId){System.out.println("eatBox");}else if(buttonView.getId() == R.id.sleepId){System.out.println("SleepBox");}else if(buttonView.getId() == R.id.dotaId){System.out.println("dotaBox");}else if(buttonView.getId() == R.id.allChooseId){System.out.println("全选");if(isChecked){eatBox.setChecked(true);sleepBox.setChecked(true);dotaBox.setChecked(true);}else{eatBox.setChecked(false);sleepBox.setChecked(false);dotaBox.setChecked(false);}}if(isChecked){System.out.println("checked");}else{System.out.println("uncheck");}}        }
其实现函数OnCheckedChangeLisener(CompoundButton buttonView, boolean isChecked){},其中buttonView同onClick()中的view,为传过来的被点击的对象,isChecked为boolean值,表示改对象是否被选中。



9,单选按钮(RadioGroup及其监听方法)

单选按钮类为RadioGroup,该Group下的Button同一时间只能有一个被按下,定义并设置监听的步骤如下:

  (1)在activity_main.xml中定义RadioGroup及其Group中的按钮

    <RadioGroup         android:id="@+id/radioGroupId"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:orientation="horizontal">                        <RadioButton             android:id="@+id/femaleButtonId"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="female"/>                <RadioButton             android:id="@+id/maleButtonId"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="male"/>    </RadioGroup>

  (2)在MainActivity.java中生成RadioGroup及RadioButton的对象

private RadioGroup radioGroup;private RadioButton femaleButton;private RadioButton maleButton;

  (3)生成RadioGroup的监听类RadioGroupListener和RadioButton的监听类RadioButtonListener,这两个类分别实现android.widget.RadioGroup.OnCheckedChangeListener{}接口和android.widget.CompoundButton.OnCheckedChangeListener{}

      class RadioButtonListener implements android.widget.CompoundButton.OnCheckedChangeListener{            @Override //CompoundButton buttonView 传递过来的是监听到变化的RadioButton对象;isChecked传递过来的是该RadioButton是否被选中      public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {          // TODO Auto-generated method stub          System.out.println("isChecked--->" + isChecked);      }           }              class RadioGroupListener implements android.widget.RadioGroup.OnCheckedChangeListener{            @Override //RadioGroup group传递过来的是监听到变化的RadioGroup对象;checkedId传递过来的是该RadioGroup对象中被选中的RadioButton的ID      ublic void onCheckedChanged(RadioGroup group, int checkedId) {      // TODO Auto-generated method stub      if(checkedId == femaleButton.getId()){          System.out.println("选中了female");          femaleButton1.setChecked(true);      }      else if(checkedId == maleButton.getId()){          System.out.println("选中了male");          maleButton1.setChecked(true);      }             }

  (4)在protected void onCreate(Bundle savedInstanceState) {}中通过findViewById()获取各个RadioGroup和RadioButton;新建各监听器对象;并将各个RadioGroup和RadioButton绑定相应的监听器。

        radioGroup = (RadioGroup)findViewById(R.id.radioGroupId);        femaleButton = (RadioButton)findViewById(R.id.femaleButtonId);        maleButton = (RadioButton)findViewById(R.id.maleButtonId);        RadioGroupListener listener = new RadioGroupListener();        radioGroup.setOnCheckedChangeListener(listener);                RadioButtonListener radioButtonListener = new RadioButtonListener();        femaleButton.setOnCheckedChangeListener(radioButtonListener);


10,图片视图(ImageView)

ImageView用于显示图片

  (1)在activity_main.xml中定义ImageView并显示图片

<ImageView     android:id="@+id/imageViewId1"    android:layout_width="100dp"    android:layout_height="100dp"    android:src="@drawable/kkhg"    android:background="#FF0000"    android:scaleType="center"/>


  (2)scaleType专题

可以通过设置scaleType设置图片的显示格式

    1)center:将文件的中心放在显示区域的中心,该区域可以通过android:layout_width="wrap_content"设置成匹配内容,也可以设置为android:layout_width="100dp"设置为固定大小。


    2)centerCrop:均衡的缩放图像(保持图像原始比例),使图片的两个坐标(宽、高)都大于等于 相应的视图坐标(负的内边距)。图像则位于视图的中央。


    3)centerInside:均衡的缩放图像(保持图像原始比例),使图片的两个坐标(宽、高)都小于等于 相应的视图坐标(负的内边距)。图像则位于视图的中央。


    4)fitCenter :使用 CENTER 方式等比例缩放图像。缩放之后位于区域中心


    5)fitStart:使用 START 方式等比例缩放图像。缩放之后图片位于左方


    6)fitEnd:使用 END 方式等比例缩放图像。缩放之后图片位于右方


    7)fitXY:将图片根据区域大小缩放,然后完全填充到区域中



11,layout_weight的使用

  (1)子空间未占满父控件的所有空间


  (2)layout_weight的值用于指定空闲空间的比例分配


  (3)由于layout_weight只能分配空闲空间,若想设置包含内容的空间比例,可以将内容的宽或者高设置为“0dp”

    <LinearLayout         android:layout_width="match_parent"    android:layout_height="wrap_content"    android:background="#ff0000">        <TextView        android:layout_width="0dp"       android:layout_height="wrap_content"       android:layout_weight="1"       android:background="#00ff00"       android:text="first"/>         <TextView        android:layout_width="0dp"       android:layout_height="wrap_content"       android:background="#000088"       android:layout_weight="1"       android:text="second"/>    </LinearLayout>



0 0