android studio简单的程序代码
时间: 2023-06-28 13:12:42 浏览: 31
以下是一个简单的 Android Studio 程序代码:
```java
public class MainActivity extends AppCompatActivity {
private TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = findViewById(R.id.text_view);
textView.setText("Hello World!");
}
}
```
这个代码创建了一个名为 `MainActivity` 的活动,该活动显示了一个包含文本“Hello World!”的文本视图。在 `onCreate()` 方法中,我们使用 `setContentView()` 方法将布局文件与活动相关联,并使用 `findViewById()` 方法获取 `TextView` 的实例,最后使用 `setText()` 方法设置文本视图的文本。
相关问题
android studio简单代码
当然可以!Android Studio是一款专门用于开发Android应用程序的集成开发环境(IDE)。下面是一个简单的Android Studio代码示例:
```java
public class MainActivity extends AppCompatActivity {
private Button button;
private TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = findViewById(R.id.button);
textView = findViewById(R.id.textView);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
textView.setText("Hello, Android Studio!");
}
});
}
}
```
这段代码展示了一个简单的Android应用程序界面,其中包含一个按钮和一个文本视图。当按钮被点击时,文本视图的内容会被修改为"Hello, Android Studio!"。
Android Studio简单记事本代码
以下是一个简单的 Android Studio 记事本应用程序代码示例,可以参考:
布局文件 `activity_main.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"
android:orientation="vertical">
<EditText
android:id="@+id/editText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="输入文本" />
<Button
android:id="@+id/saveButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="保存" />
<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
```
Java 代码文件 `MainActivity.java`:
```
package com.example.notepad;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class MainActivity extends AppCompatActivity {
private EditText editText;
private TextView textView;
private Button saveButton;
private static final String FILE_NAME = "note.txt";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editText = findViewById(R.id.editText);
textView = findViewById(R.id.textView);
saveButton = findViewById(R.id.saveButton);
saveButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
saveNote();
}
});
loadNote();
}
private void saveNote() {
String text = editText.getText().toString();
FileOutputStream fos = null;
try {
fos = openFileOutput(FILE_NAME, Context.MODE_PRIVATE);
fos.write(text.getBytes());
Toast.makeText(this, "保存成功", Toast.LENGTH_SHORT).show();
} catch (IOException e) {
e.printStackTrace();
Toast.makeText(this, "保存失败", Toast.LENGTH_SHORT).show();
} finally {
if (fos != null) {
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
private void loadNote() {
FileInputStream fis = null;
try {
fis = openFileInput(FILE_NAME);
int length = fis.available();
byte[] buffer = new byte[length];
fis.read(buffer);
String text = new String(buffer);
textView.setText(text);
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fis != null) {
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
```
这个简单的记事本应用程序只有一个文本框、一个保存按钮和一个文本视图。用户可以在文本框中输入文本,然后点击保存按钮将文本保存到文件中。当应用程序启动时,它会尝试从文件中加载之前保存的文本,并将其显示在文本视图中。