2017Google Study Jams之L3面向对象编程终极

2017-03-26  本文已影响42人  Shawpoo的

此次活动的举办方:Google Study Jams活动官网

我的博客(同步此次活动笔记):CSDN博客我的简书

Google Developers

通过前面的学习我们都知道Android是由Java语言所编写的,所以L3主要介绍的就是Java语言的这种面向对象的编程,并且使用到Android中。

一、面向对象编程

1、方法

修饰符  返回值类型 方法名(参数) {
      //方法体
}

例如:

public void add(int num) {
      System.out.printf("num的值是:" + num);
}

访问权限级别:public > protected > default > private。

public void add() {
    //无参数方法
}

public void add(int num) {
    //有参且有一个参数
}
public void addNum(int num) {

}

2、Java的基本数据类型

Java的基本数据类型分为八种:

3、类和对象

  修饰符 class 类的名称 {
       //类的成员变量
       //类的方法
  }

  例如:

  public class StudyJams{  //Study Jams论坛
      private int mStudyTime; //在线时间

      private void writeNote() {
             // 写笔记
      }
  }

创建对象:

   SutdyJams studyJams = new StudyJams();

可以通过对象使用类里方法和全局变量。例如:

  studyJams.mStudyTime = 100; // 为成员变量赋值
  studyJams.wirteNote(); //调用写笔记的方法

4、if/else条件语句的使用

if条件语句常用来对流程的控制,条件的判断等。

  if(表达式1) {
      // 满足条件1
  } else if(表达式2){
      // 满足条件2
  } else {
      // 前面的条件都不满足
  } 

  例如:
  if (3 > 1) {
       //...
  }

  if(表达式) {
      //方法体
  }
  if(表达式) {
      //方法体
  } else {
   //方法体
   }
  if(表达式1) {
      //方法体
  } else if(表达式2) {
      //方法体
   } else {
      //方法体
   }

二、Android的跳转和资源的引用

1、基础控件延伸

<?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/edit_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:hint="请输入论坛名称"/>
</LinearLayout>

android:hint属性为文本框的提示语,android:text则表示文本框的值。
在代码中获取文本框的值:

EditText editText = (EditText) findViewById(R.id.edit_text);
String str = editText.getText().toString();
//str即为文本框中输入的值

<?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">
    <CheckBox
        android:id="@+id/checkBox"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:checked="true" //是否选中,默认未选中
        android:text="课程学习"/>
    <CheckBox 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="笔记分享"/>
    <CheckBox 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="每日签到"/>
</LinearLayout>
CheckBox

监听事件:cb.isCheck();来判断是否选中

CheckBox cb = (CheckBox) findViewById(R.id.checkBox); //初始化

cb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
       @Override
       public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
            Toast.makeText(MainActivity.this, "触发了选中事件", Toast.LENGTH_SHORT).show();
       }
 });

ScrollView就是一个可以滚动的View,且滚动的方向是垂直方向的。需要注意的是ScrollView中只允许出现一个子控件。

<?xml version="1.0" encoding="utf-8"?>
<ScrollView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
        <CheckBox
            android:id="@+id/checkBox"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:checked="true"
            android:text="课程学习"/>
        <CheckBox android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:text="笔记分享"/>
        <CheckBox android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:text="每日签到"/>
    </LinearLayout>
</ScrollView>

2、资源引用

Android的资源引用,需要在res文件夹的values文件夹下建立对应的资源文件,对应关系如下:

资源类型 在Java文件中 在Xml文件中
Image R.drawable.photo @drawable/photo
String R.string.hello @string/hello
Layout Xml File R.layout.activity_main @layout/activity_main
ID R.id.text_view @+id/text_view
Color R.color.red @color/red
Dimen R.dimen.padding_left @dimen/padding_left

3、使用Intent跳转页面

Intent的中文意思是“意图,意向”,在Android中提供了Intent机制来协助应用间的交互与通讯,Intent负责对应用中一次操作的动作、动作涉及数据、附加数据进行描述,Android则根据此Intent的描述,负责找到对应的组件,将 Intent传递给调用的组件,并完成组件的调用。

下面举个简单的例子:从当前页面跳转到另一个页面:

Intent intent = new Intent(MainActivity.this, StudyJamsActivity.class);                 
startActivity(intent); // 启动Activity  

//跳转
Intent intent = new Intent(MainActivity.this, StudyJamsActivity.class);   
intent.putExtra(EXTRA_TIME, time);              
startActivity(intent); // 启动Activity  

//接收
int time = getIntent().getIntExtra(EXTRA_TIME, 0);
// time就是由上个页面传递过来的值

使用Intent可以传递基本数据类型,集合,对象等数据类型。

好了,Google Study Jams的笔记到这里就结束了,最后祝贺各位顺利结业!

**Congratulations to all! **

上一篇 下一篇

猜你喜欢

热点阅读