写一段简单的Java语言的Android studio代码
时间: 2024-09-09 18:06:39 浏览: 49
在Android Studio中编写简单的Java代码,通常包括创建一个Activity,这个Activity会处理用户界面和事件。下面是一个非常基础的Android应用的MainActivity代码示例,它仅仅创建一个带有文本的界面。
```java
package com.example.myfirstapp;
import android.os.Bundle;
import android.app.Activity;
import android.widget.TextView;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// 创建一个TextView来显示内容
TextView textView = new TextView(this);
textView.setText("Hello, Android!");
textView.setTextSize(20);
// 设置TextView为这个Activity的布局
setContentView(textView);
}
}
```
在这个例子中,`MainActivity` 继承自 `Activity` 类,并重写了 `onCreate` 方法。这是Android应用的标准做法,所有的界面逻辑和活动都是在这个方法中实现的。创建了一个 `TextView` 对象,并设置了要显示的文本以及字体大小。最后,通过调用 `setContentView` 方法将这个 `TextView` 设置为当前的布局。
阅读全文
相关推荐


















