AndroidStudio点击第一个界面里不同的3个textview,跳转至第二个界面且在textview分别滚动显示不同的txt文件,第二界面布局写代码
时间: 2023-06-13 13:07:31 浏览: 51
好的,以下是Android Studio中实现此功能的代码:
第一个界面的布局文件:
```xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView 1"
android:layout_centerHorizontal="true"
android:layout_marginTop="50dp"
android:textSize="30sp"/>
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView 2"
android:layout_below="@+id/textView1"
android:layout_marginTop="50dp"
android:layout_marginLeft="50dp"
android:textSize="30sp"/>
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView 3"
android:layout_below="@+id/textView1"
android:layout_marginTop="50dp"
android:layout_marginRight="50dp"
android:layout_alignParentRight="true"
android:textSize="30sp"/>
</RelativeLayout>
```
第二个界面的布局文件:
```xml
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/textView_content"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="20sp"
android:padding="20dp"/>
</ScrollView>
```
MainActivity.java文件的代码:
```java
public class MainActivity extends AppCompatActivity {
private TextView textView1, textView2, textView3;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView1 = findViewById(R.id.textView1);
textView2 = findViewById(R.id.textView2);
textView3 = findViewById(R.id.textView3);
textView1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
displayText("file1.txt");
}
});
textView2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
displayText("file2.txt");
}
});
textView3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
displayText("file3.txt");
}
});
}
private void displayText(String filename) {
try {
InputStream is = getAssets().open(filename);
int size = is.available();
byte[] buffer = new byte[size];
is.read(buffer);
is.close();
String text = new String(buffer);
Intent intent = new Intent(this, DisplayActivity.class);
intent.putExtra("text", text);
startActivity(intent);
} catch (IOException e) {
e.printStackTrace();
}
}
}
```
DisplayActivity.java文件的代码:
```java
public class DisplayActivity extends AppCompatActivity {
private TextView textViewContent;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_display);
textViewContent = findViewById(R.id.textView_content);
String text = getIntent().getStringExtra("text");
textViewContent.setText(text);
}
}
```
在assets文件夹下创建三个txt文件,命名为file1.txt、file2.txt、file3.txt,并分别填入不同的文本内容。最后在AndroidManifest.xml文件中添加DisplayActivity的声明:
```xml
<activity android:name=".DisplayActivity"/>
```
这样点击第一个界面中的任意一个TextView,就可以跳转至第二个界面,并且在TextView中滚动显示对应的txt文件内容。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)