android第二周第一节课学习笔记

项目开始

一、新建项目:

把布局改成线性布局LinearLayout

<?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">
    <Button
        android:id="@+id/but1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="打开网站"/>
</LinearLayout>

二、后端固定部分

  • 第一个模块:包名

  • 第二个模块:引用工具

  • 第三个继承父类:项目名字不要轻易改

  • 第四步:重写父类@override

  • 第五步:生命周期:savedInstanceState 英译:被保护的实例状态

    作用:保留状态,节省内存

  • 第六步:初始化前端

package dpsy.yingshijie.yanshi;//1包名
​
import android.content.Intent;//2引用工具
import android.net.Uri;
import android.os.Bundle;
​
import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
​
public class MainActivity extends AppCompatActivity {//3继承父类
​
    @Override//4重写父类
    protected void onCreate(Bundle savedInstanceState) {//5生命周期
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);//6初始化前端
}
}

访问服务器的代码

    //访问服务器的代码
        Intent intent = new Intent(Intent.ACTION_VIEW);
      // 设置要打开的网址
        intent.setData(Uri.parse("http://8.140.194.125:3000"));
      // 启动Activity
        startActivity(intent);

三、按钮功能

  1. xml中按钮的布局:

    <?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">
    ​
        <Button
            android:id="@+id/but1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="打开网站"
            />
    </LinearLayout>
  2. 按钮的功能:

      //按钮功能
        //第一步:查找按钮
        Button button = findViewById(R.id.but1);
        
        //第二步:设置监听
        button.setOnClickListener(new View.OnClickListener() {
            //第三步:重写功能
            @Override
            public void onClick(View view) {
                // 访问服务器的代码
                Intent intent = new Intent(Intent.ACTION_VIEW);
                // 设置要打开的网址
                intent.setData(Uri.parse("http://8.140.194.125:3000"));
                // 启动Activity
                startActivity(intent);
            }
        });
评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值