• 手动创建活动


    1.创建layout

    2.新建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">
    
        <!-- 添加按钮 -->
        <Button
            android:id="@+id/button_1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Button 1"
            />
    
    </LinearLayout>
    

    3.设置布局

    package activitytest.example.com.activitytest;
    
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    
    public class FirstActivity extends AppCompatActivity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.first_layout); // 设置默认布局
        }
    }
    
    

    4.注册活动,设置主活动

    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="activitytest.example.com.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=".FirstActivity" 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>
    

    5.运行

    6.增加按钮事件和toast提醒

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.first_layout); // 设置默认布局
    
            Button button1 = (Button) findViewById(R.id.button_1);
            button1.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Toast.makeText(FirstActivity.this,"You clicked Button 1",Toast.LENGTH_SHORT).show();
                }
    
            });
    
        }
    

  • 相关阅读:
    Go part 7 反射,反射类型对象,反射值对象
    activemq BytesMessage || TextMessage
    Go part 6 接口,接口排序,接口嵌套组合,接口与类型转换,接口断言
    mysql 查询表的字段名称,字段类型
    冒泡(bubblesort)、选择排序、插入排序、快速排序
    用 python 写一个模拟玩家移动的示例
    day 14(作业)
    day 13
    day 12
    day 11
  • 原文地址:https://www.cnblogs.com/jiqing9006/p/7459904.html
Copyright © 2020-2023  润新知