Android - 使用单一布局的按钮

标签 android android-layout

我创建了一个使用 ViewFlipper 在不同元素之间切换的 Activity 。每个元素代表商店中的一个项目。我想为每个 View 添加一个“购买”按钮。但是我不确定如何执行此操作,因为所有 View 都使用我创建的默认布局。我已经以编程方式添加了商品价格等信息。所以我不确定如何向按钮添加监听器,因为它们都将引用 xml 文件中的同一个按钮:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >

<Button
    android:id="@+id/credit_button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentTop="true"
    android:layout_marginBottom="220dp"
    android:layout_marginLeft="30dp"
    android:layout_marginRight="30dp"
    android:layout_marginTop="104dp" />

<TextView
    android:id="@+id/credit_type"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignRight="@+id/credit_button"
    android:layout_alignTop="@+id/credit_button"
    android:layout_marginRight="22dp"
    android:layout_marginTop="21dp"
    android:text="Large Text"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:typeface="normal" />

<TextView
    android:id="@+id/credit_number"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/credit_button"
    android:layout_alignRight="@+id/credit_type"
    android:layout_centerVertical="true"
    android:layout_marginBottom="30dp"
    android:layout_marginLeft="15dp"
    android:text="Medium Text"
    android:textAppearance="?android:attr/textAppearanceMedium" />

</RelativeLayout>

如您所见,Button id 是“Credit Button”。那么现在要能够区分不同商店商品的按钮,我需要做什么?

请注意,我也在动态添加商店项目,所以我不能简单地使用 xml 单独创建所有 View 。

好的,这是更新后的答案。我使用下面每个人的回复来解决问题。所以谢谢大家:)

        // PerkView
    View PerkView = View.inflate(this, R.layout.store_category, null);
    viewFlipper.addView(PerkView);
    Button perkButton = (Button)
    PerkView.findViewById(R.id.StoreCatItem);

    // TitleView
    View TitleView = View.inflate(this, R.layout.store_category, null);
    viewFlipper.addView(TitleView);
    Button titleButton = (Button)
    TitleView.findViewById(R.id.StoreCatItem);

    // ProfileView
    View ProfileView = View.inflate(this, R.layout.store_category, null);
    viewFlipper.addView(ProfileView);
    Button profileButton = (Button)
    ProfileView.findViewById(R.id.StoreCatItem);

我只是以编程方式创建了多个 View ,然后从这些 View 中检索了按钮。然后我将监听器添加到按钮,如下所示:

perkButton.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            Intent i = new Intent(getBaseContext(), Perks.class);
            i.putExtra("player", player);
            startActivity(i);
        }
    });

    titleButton.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            Intent i = new Intent(getBaseContext(), Titles.class);
            i.putExtra("player", player);
            startActivity(i);
        }
    });

非常感谢:)

最佳答案

有两种方法可以清楚地处理按钮点击,

  1. 在为每家商店展开布局时,仅在其中添加带有 onClick() 方法的按钮监听器。
  2. 或者您可以为每个商店分配一些uniqueId或flag,然后通过这个uniqueID处理商店的按钮点击。

关于Android - 使用单一布局的按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20836731/

相关文章:

android - 用两种不同颜色的文本填充形状

android - 如何最好地处理 Android ListActivity 的滑动手势

java - Android 从数据库中删除一行

android - 使用自定义属性在 <include> 标签中使用的布局内编辑 View (TextView/ImageView)

android - 如何以编程方式设置编辑文本的布局高度

android - ListFragment Textview 不必要的行

android - 当我单击编辑它时,我的文本字段隐藏在 BottomNavigationBar 的键盘后面

android - 创建 XML 可绘制对象时出现问题

android - 有条件地显示按钮

android - 如何以编程方式水平滚动 RecyclerView?