项目开始
一、新建项目:
把布局改成线性布局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);
三、按钮功能:
-
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>
-
按钮的功能:
//按钮功能 //第一步:查找按钮 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); } });





