安卓怎样将获取的信息写入文本框里面
时间: 2024-05-30 18:12:39 浏览: 8
你可以使用 Android 上的 TextView 控件来将获取的信息写入文本框中。在 Java 代码中,你可以使用 TextView 的 setText() 方法来设置文本框里的文字。例如:
TextView textView = findViewById(R.id.my_text_view);
textView.setText("这是要显示的文本");
相关问题
android sudio 写入读取编辑框内容
要在 Android Studio 中读取和编辑文本框的内容,您需要完成以下步骤:
1.在您的布局 XML 文件中添加一个 EditText 元素,例如:
```
<EditText
android:id="@+id/edit_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
```
2.在您的活动(Activity)中获取 EditText 的引用,例如:
```
EditText editText = findViewById(R.id.edit_text);
```
3.要读取 EditText 中的文本,使用 getText() 方法,例如:
```
String text = editText.getText().toString();
```
4.要编辑 EditText 中的文本,使用 setText() 方法,例如:
```
editText.setText("New text");
```
通过这些步骤,您就可以在 Android Studio 中读取和编辑文本框的内容了。
使用安卓代码分别写两个按钮实现写入存储和读出存储,读取的时候要有时间戳,显示在下方
好的,以下是一个简单的示例代码,实现了在安卓应用程序中点击按钮写入数据和读取数据,并且在 UI 上显示时间戳和数据文本。
在布局文件中,我们需要添加两个按钮和一个文本框,代码如下:
```xml
<LinearLayout
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/writeButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="写入数据" />
<Button
android:id="@+id/readButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="读取数据" />
<TextView
android:id="@+id/resultTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="结果显示区域" />
</LinearLayout>
```
在 Activity 中,我们需要实现按钮的点击事件,并使用 SharedPreferences 来存储和读取数据,代码如下:
```java
public class MainActivity extends AppCompatActivity {
private EditText editText;
private Button writeButton;
private Button readButton;
private TextView resultTextView;
private SharedPreferences sharedPreferences;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editText = findViewById(R.id.editText);
writeButton = findViewById(R.id.writeButton);
readButton = findViewById(R.id.readButton);
resultTextView = findViewById(R.id.resultTextView);
sharedPreferences = getSharedPreferences("MyData", MODE_PRIVATE);
writeButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String data = editText.getText().toString();
long timestamp = System.currentTimeMillis();
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString("data", data);
editor.putLong("timestamp", timestamp);
editor.apply();
Toast.makeText(MainActivity.this, "数据已写入", Toast.LENGTH_SHORT).show();
}
});
readButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String data = sharedPreferences.getString("data", "");
long timestamp = sharedPreferences.getLong("timestamp", 0);
String result = "时间戳:" + timestamp + "\n数据:" + data;
resultTextView.setText(result);
}
});
}
}
```
在代码中,我们首先获取 EditText、Button 和 TextView 的实例,并使用 getSharedPreferences 方法获取 SharedPreferences 对象。然后,我们为写入按钮添加一个点击事件,获取输入框中的数据和当前时间戳,并使用 SharedPreferences.Editor 来写入数据。为了显示写入的结果,我们使用了一个 Toast。对于读取按钮,我们从 SharedPreferences 中读取数据,并将它们显示在 TextView 上,用于显示时间戳和数据文本。
这样,当用户在应用程序中点击“写入”按钮时,我们将获取输入框里的文本数据,并获取当前时间戳,然后将它们存储到应用程序的偏好设置中。当用户点击“读取”按钮时,我们将从偏好设置中读取数据,并在 UI 上显示时间戳和数据文本。