在程序建立两个相同界面,界面包含一个EditText组件,做输入文本框,一个Button组件和一个TextView组件。要求在一个界面的EditText组件输入字符串,在另一个界面的TextView组件内显示
时间: 2024-03-04 14:48:10 浏览: 20
可以使用Android中的Intent和Bundle进行界面之间数据传递。在第一个界面的Button组件的onClick事件中,获取EditText中的字符串,并通过Intent和Bundle传递到第二个界面。在第二个界面的onCreate方法中,获取传递过来的字符串,并在TextView组件中显示。
以下是示例代码:
第一个界面的Button的onClick事件:
```java
public void onClick(View v) {
String inputText = editText.getText().toString();
Intent intent = new Intent(FirstActivity.this, SecondActivity.class);
Bundle bundle = new Bundle();
bundle.putString("inputText", inputText);
intent.putExtras(bundle);
startActivity(intent);
}
```
第二个界面的onCreate方法:
```java
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
TextView textView = findViewById(R.id.textView);
Bundle bundle = getIntent().getExtras();
String inputText = bundle.getString("inputText");
textView.setText(inputText);
}
```
需要注意,需要在AndroidManifest.xml中注册第二个界面。另外,也要在第二个界面的布局文件中添加一个TextView组件,id为textView。
相关推荐
![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)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)