zoukankan      html  css  js  c++  java
  • Android-入门学习笔记-面向对象编程

    在 Android Studio 中打开方法声明的键盘快捷键为:

    • Mac:command+b
    • Windows:control + b

    MainActivity.java 文件中点击方法,右键→Go To→Declaration,即可跳转至方法声明处。

    Android框架代码,Android团队所编写的。

    定义 调用

    方法的输入参数:零个或多个参数;

    输出一个返回值:零个或一个。

    Defining Methods

    Javadoc,即Java注释。

    //方法签名
    private String createCalendarEventReminder(String eventName, String location, int minutesAway) 
    //访问修饰符 + 返回数据类型 + 方法名 + 输入参数/(参数1数据类型 参数1变量名,参数2数据类型 参数2变量名,...)

      返回数据类型void是有必要的,告诉程序编辑器无返回值,不必添加return语句。

    arguments 实参

    field 字段

    /**
     * Calculates the price of the order.
     * 
     * @param quantity is the number of cups of coffee ordered
     */
    //@参数,可看作在注释中调用了参数,使代码编辑器可以对注释进行语法错误的检查
    private void calculatePrice(int quantity) {
        int price = quantity * 5;
        return price;
    }

    函数调用部分的输入参数形式应该和函数定义的相符合,否则视作语法错误。

    Returning a Value from a Method,函数执行到return则返回,后续语句无效。

    Java Language Keywords

    java标识符(What is a Java Identifier?),包、类、接口、方法、变量的名称,可用于调用。

    public void submitOrder(View view) {
            int price = calculatePrice();//将calculatePrice的返回值赋给price
          String priceMessage = "Total $" + price; 
         priceMessage = priceMessage + "
    Thank you!"; 
         displayMessage(priceMessage); }

    Java - Basic Operators 

    资源概览支持多种屏幕

    访问资源,编译应用时,aapt 会生成 R 类,其中包含 res/ 目录中所有资源的资源 ID。 

    Java中的类 好比是房屋平面构图,对象实例 好比是不同的房屋。

    What Is a Class?

    What is 'Context' on Android?

    Googlesource 中 TextView 的源代码 需FQ

    Googlesource 中 ImageView 的源代码  需FQ

     grepcode-Android 源代码目录   不必FQ

     Chrome 扩展-Android SDK Search   需FQ使用

    //简化的 TextView 代码
    /**
     * Displays text to the user.
     */
    public class TextView extends View {        //类名,首字母大写驼峰式
     
        // String value
        private String mText;                        //mText中的m表示成员变量
     
        // Text color of the text
        private int mTextColor;                    //变量名,首字母小写驼峰式
        
        // Context of the app
        private Context mContext;
     
        /**
         * Constructs a new TextView with initial values for text and text color.
         */
        public TextView(Context context) {      //构造函数
          mText = "";
          mTextColor = 0;
          mContext = context;
        }
     
        /**
         * Sets the string value in the TextView.
         *
         * @param text is the updated string to be displayed.
         */
        public void setText(String text) {
            mText = text;
        }
     
        /**
         * Sets the text color of the TextView.
         *
         * @param color of text to be displayed.
         */
        public void setTextColor(int color) {
            mTextColor = color;
        }
     
        /**
         * Gets the string value in the TextView.
         *
         * @return current text in the TextView.
         */
        public String getText() {
            return mText;
        }
     
        /**
         * Gets the text color of the TextView.
         *
         * @return current text color.
         */
        public int getTextColor() {
            return mTextColor;
        }
    }                        

    使用构造函数创建对象 

    TextView priceTextView = new TextView(context); 对象数据类型  变量名 = new 对象数据类型(构造函数中的输入参数)

    输入参数的顺序及类型需与构造函数中声明的相匹配

    使用工厂方法模式创建对象

    MediaPlayer player = MediaPlayer.create(context, R.raw.song); //对象数据类型 变量名 = 对象数据类型.工厂方法名称(输入参数)
    Toast toastMessage = Toast.makeText(context, "Hi", duration); //

    Toasts ,即时弹出即时消失的消息

    //
    Toast.makeText(this, "You cannot have more than 100 coffees", Toast.LENGTH_SHORT).show();
    //Toast toast = Toast.makeText(context, text, duration);
    //toast.show();

    在对象上调用方法 priceTextView.setText("News");  对象数据类型.方法名称(输入参数)

    类内调用/访问,实际上是this.方法,而this.往往被省略。

    私有变量、私有方法可以在类内被读取。

    java类的继承主要通过extends关键字来完成的。一旦继承后,子类将获得父类的成员属性跟成员方法。子类继承父类。

    若需对继承的父类中的行为进行修改可使用@override 进行重写/重载/复写

    //通过资源编号获取View类型对象
    TextView priceTextView = (TextView) findViewById(R.id.price_text_view);
    //R.id.price_text_view为View类型对象,将其转换为TextView类型对象后,再赋给TextView类型的变量priceTextView。

    详细了解 Java 中的类型转换(请参阅“Casting Objects”部分)

     方法中传入的数据类型及返回的数据类型需查阅文档。

    修改大量代码后,应及时运行进行检查。

    这里,我们使用了 Log.i(),表示“信息”级别的日志。其他级别的选项如下所示:

    • e(String, String)(错误)
    • w(String, String)(警告)
    • i(String, String)(信息)
    • d(String, String)(调试)
    • v(String, String) (详情) 

    logcat 命令行工具

    Log开发文档

     

    Red lines,对应用的可视化界面的非常详细的说明。

    为什么学写代码这么难:每个编程初学者都需要了解的事情

    1. 甜蜜的蜜月期
    2. 困惑的悬崖
    3. 绝望的沙漠
    4. 重新崛起

    StackOverflow忽略问题详述,直接查看已采纳的答案,效率更高。

    官方文档中,直接阅读示例代码更高效。

     

    //链式调用
    EditText text = (EditText)findViewById(R.id.vnosEmaila);
    String value = text.getText().toString();//text.getText()方法得到Editable类型数据,再调用toString()方法将之转化为String类型数据。然后再赋给value

     常见 Intent 指南 ,组件间传递动作、数据的机制。用于调用相关另一组件,但不必关心具体调用的是什么组件。如同抛出篮球但不必管何人接球。

    Ctrl + / ,注释掉所有选中的代码块

    Java 中常量命名:全大写_全大写

    在发布应用前,请参考本地化检查清单。本地化,处理字符串,以适应用户的本地语言(用户可在设置—语言中选择本地语言)。

    支持不同语言

    es,西班牙语的语言代码;fr,法语的语言代码。

    strings.xml 中 点击Open editor 或文件树中右键单击Open Translation Editor来打开翻译编辑器。单击globe图标添加新一组语言,翻译编辑器会自动创建相应语言版本的的strings.xml文件

    Localizing with Resourcesxliff标签可查阅此文档

        <resources xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2">
        <!-- 需在资源文件中添加 xmlns:xliff这行代码 使xliff成为资源标签 -->
        <!-- Name for the order summary. It will be shown in the format of "Name: Amy" where Amy is the
          user's name. [CHAR LIMIT=NONE]
        -->
        <!-- 添加注释可帮助不同语言的翻译者/翻译服务理解字符串使用处的上下文,也可提供限制字符数的信息 -->
        <string name="order_summary_name">Nombre: <xliff:g id="name" example="Amy">%s</xliff:g></string>
      
        <!-- xliff标签用于标记不需翻译的部分
        -->

      

    NumberFormat.getCurrencyInstance().format(price));//获取本地货币
    priceMessage += "
    " + getString(R.string.thank_you);//原代码:priceMessage += "
    Thank you!",Java代码中调用字符串资源

    要详细了解样式资源,请参阅 样式和主题Style Resource,注意,styles.xml 位于资源文件夹里。意味着,你可以重写相关格式并为不同尺寸的设备提供不同的属性。你只需在相关值文件夹里创建一个新的 styles.xml 文件。例如,如果你希望当用户使用平板电脑(屏幕宽至少为时 600 dp),HeaderTextStyle 显示更大的字体,可以保存一个新版本的 HeaderTextStyle 并放入 res/values-sw600dp/styles.xml 文件夹中。详情请参阅使用最小宽度限定符 。

    标题高度、“含文字图标”参考 Material Design 单行列表规范中的规定

     正文字体推荐大小参考

    复选框应该距离屏幕左边缘 16dp。复选框的文字应该距离屏幕左边缘 72dp。这些是Material Design 规范中推荐的两大主要框线规格(距离左边缘 16dp 和 72dp)

    可以使用Google Play中的 Keyline Pushing 应用(需FQ)来验证你的应用是否距离基线网格 8dp,你可以在 Google Play 上找到该应用。如果你打开网格,然后再回到你的应用中,你可以看到大部分情况下,我们的内容都符合 16dp 和 72dp 基线网格标准,这是因为 Android 提供的默认资源(例如复选框)已经内置了一定的内边距。

    对 UI 应用样式和主题,主题可应用于所有Activity。

    使用材料主题 ,点击此处详细了解 Material Design 系统主题背景。

    维护兼容性 ,是如何定义与旧版 Android 系统兼容的主题背景。

  • 相关阅读:
    K好数
    最大最小公倍数
    十六进制转十进制
    利用malloc定义数组
    01字串
    ubuntu 14.04 下jdk和sdk+eclipse 的配置
    Mysql3
    求最大连续子串
    UC笔试
    java实现随机洗牌算法
  • 原文地址:https://www.cnblogs.com/infocodez/p/7886788.html
Copyright © 2011-2022 走看看