根据上述案例给出activity_cart.xml的代码
时间: 2023-11-25 10:08:47 浏览: 37
以下是购物车页面(activity_cart.xml)的代码:
```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"
android:padding="16dp">
<TextView
android:id="@+id/cart_text_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="18sp" />
</LinearLayout>
```
上述代码中,我们只使用了一个TextView控件来显示购物车信息。您可以根据自己的需求,在该布局中添加其他控件,实现更丰富的购物车页面效果。
相关问题
根据上述代码给出activity_cart的代码
当用户成功购买商品后,我们需要将商品信息传递到购物车页面进行显示。购物车页面应该能够获取传过来的用户账号、商品名称、价格,并用TextView控件显示出来。下面是购物车页面(CartActivity.java)的代码:
```java
public class CartActivity extends AppCompatActivity {
public static final String EXTRA_USERNAME = "username";
public static final String EXTRA_PRODUCT_NAME = "product_name";
public static final String EXTRA_PRODUCT_PRICE = "product_price";
private TextView mCartTextView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_cart);
mCartTextView = findViewById(R.id.cart_text_view);
Intent intent = getIntent();
String username = intent.getStringExtra(EXTRA_USERNAME);
String productName = intent.getStringExtra(EXTRA_PRODUCT_NAME);
String productPrice = intent.getStringExtra(EXTRA_PRODUCT_PRICE);
mCartTextView.setText("User: " + username + "\nProduct: " + productName + "\nPrice: " + productPrice);
}
}
```
上述代码中,我们首先获取传递过来的用户账号、商品名称、价格,并将其显示在TextView控件中。在购物车页面中,我们只需要显示当前购买的商品信息,不需要进行其他操作。如果您希望在购物车页面中显示多个商品信息,可以使用ListView或RecyclerView来实现。
帮我写一段通过单击button从activity_login.xml跳转activity_main.xml的代码
好的,以下是一个简单的例子:
在 activity_login.xml 中添加一个 Button 控件:
```xml
<Button
android:id="@+id/btn_login"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Login" />
```
在 LoginActivity 中为 Button 添加点击事件,通过 Intent 跳转到 MainActivity:
```java
public class LoginActivity extends AppCompatActivity {
private Button btnLogin;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
btnLogin = findViewById(R.id.btn_login);
btnLogin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(LoginActivity.this, MainActivity.class);
startActivity(intent);
}
});
}
}
```
在 AndroidManifest.xml 中添加 MainActivity 的声明:
```xml
<application>
...
<activity android:name=".MainActivity" />
</application>
```
这样点击 Login 按钮后就可以跳转到 MainActivity 页面了。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.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)