手动创建Activity

1、右击com.example.activitytest包 -> New -> Activity -> Empty Activity。

勾选Generate Layout File表示会自动为FirstActivity创建一个对应的布局文件

勾选Launcher Activity表示会自动将FirstActivity设置为当前项目的主活动

勾选Backwards Compatibility表示会为项目启动向下兼容的模式

2、手动创建布局。

右击app/src/main/res目录 -> New -> Directory,会弹出一个新建目录窗口。先创建layout目录,再在layout右键 -> New -> Layout resource file,新建一个布局资源文件。File name: first_layout, Root element: LinearLayout。

3、在布局中创建组件。

在first_layout中添加

<?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">

    <!--android:id表示给当前的元素定义一个唯一标识符
        anddoid:layout_width指定当前元素宽度,match_parent表示让当前元素和父元素一样宽
        android:layout_height指定当前元素高度,wrap_content表示当前元素的高度只要能刚好包含里面内容
    -->
    <Button

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

</LinearLayout>

4、在活动中加载布局

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.first_layout);
        //通过findViewById()方法获取在布局文件中定义的元素,也就是在first_layout.xml中通过android:id属性指定的值
        Button button1 = (Button) findViewById(R.id.button_1);
        //通过调用setOnClickListener()方法为按钮注册一个监听器,点击按钮时会执行监听器中的onClick()方法
        button1.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v){
                //Toast通过静态方法makeText()创建Toast对象,然后调用show()将Toast显示出来
                //Toast三个参数,第一个参数时Context,即Toast要求的上下文,由于活动本身就是一个Context对象,因此这里直接传入FirstActivity.this
                //第二个参数是Toast显示的文本内容
                //第三个参数是Toast显示的时长,可选择Toast.LENGTH_SHORT或者Toast.LENGHT_LONG
                Toast.makeText(MainActivity.this, "You clicked Button 1",
                        Toast.LENGTH_SHORT).show();
            }
        });
    }
}

5、在AndroidManifest文件中注册

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

    <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"
            android:label="This is FirstActivity">
            <intent-filter>
                <action android:name="android.intent.action.Main"/>
                <category android:name="android.intent.category.LAUNCHER"/>
            </intent-filter>
        </activity>
    </application>

</manifest>

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值