(二)手动配置第一个HelloWorld程序

  • 上例的HelloWorld是由Android sutudio 自动生成的,现在我们手动来配置。

  • 1. 重新创建工程

  • 2. 创建空的Activity

  •   生成的MainActivity.java 文件:
package com.shyroke.myhelloworld;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }
}
  •  3.创建布局文件

    •   Android程序设计讲究逻辑和视图分离,最好每一个活动对应一个布局,布局就是用来显示界面的。

  • 生成的main_layout.xml文件后添加一个按钮组件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

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

</LinearLayout>
  •  4. 在活动中加载布局文件

    •   上一步我们虽然创建了一个布局文件,现在我们要把这个布局文件和活动绑定在一起,即在活动中加载该布局文件。

 MainActivity.java

package com.shyroke.myhelloworld;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main_layout);

        Button btn=(Button)findViewById(R.id.button1);
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(MainActivity.this,"点击了按钮",Toast.LENGTH_SHORT).show();
            }
        });
    }
}
  • MainActivity.this 指向MainActivity的一个对象(引用),这里如果用this则表示OnclickListener对象。
  • 项目中添加的任何资源都会在R文件中生成一个相应的资源id,因为刚才创建的布局文件main_layoout.xml布局的id现在已经添加到R文件中,我们只需要调用就行了。
  •  5. 在AndroidManifest文件中注册该活动

    •   所有活动都要在AndroidManifest中注册才能生效,事实上MainActivity已经注册过了,因为创建MainActivity的时候Android studio已经帮我们注册了。

 AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.shyroke.myhelloworld">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN"></action>
                <category android:name="android.intent.category.LAUNCHER"></category>
            </intent-filter>

        </activity>
    </application>

</manifest>
  • .MainActivity 表示com.shyroke.myhelloworld.MainActivity 的缩写,最外层的package制订了程序的报名是com.shyroke.myhelloworld。
  • android:label 指定活动中标题栏的内容,标题栏是显示在活动最顶部。
  •  <action android:name="android.intent.action.MAIN"></action> 表示该活动是项目的主活动,点击app首先启动的就是该活动。
  • <category android:name="android.intent.category.LAUNCHER"></category>:决定应用程序是否显示在程序列表里。
  • 如 果一个应用没有LAUNCHER则该apk仍能安装到设备上,但是在主程序图中看不到。如果给那个Activity 设定了LAUNCHER,且同时设定了Main,则这个Activity就可出现在程序图中;如果没有Main,则不知启动哪个Activity,故也不会有图标出现。

原文地址:https://www.cnblogs.com/shyroke/p/7454335.html