【问题标题】:Butter Knife - Listeners For Buttons inside include黄油刀 - 内部按钮的侦听器包括
【发布时间】:2016-05-01 17:39:58
【问题描述】:

我有一个简单的相对布局,只有一个按钮。

我在主布局中多次使用此布局。

可以使用 listener(this) 为所有这些按钮编写一个方法(使用或不使用 ButterKnife),而不是像这样创建多个侦听器---->

  (relativeLayout1.findViewById(R.id.currencyButton)).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Toast.makeText(ForeignExchangeActivity.this, "Euro", Toast.LENGTH_SHORT).show();
        }
    });

    (relativeLayout2.findViewById(R.id.currencyButton)).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Toast.makeText(ForeignExchangeActivity.this, "GBP", Toast.LENGTH_SHORT).show();
        }
    });

单项布局

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/colorBackgroundSeekbar">

    <TextView
        android:id="@+id/currencyTitle"
        android:text="EUR - PLN"
        android:padding="5dp"
        android:textSize="20sp"
        android:textStyle="bold"
        android:textColor="#FFFFFF"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:background="@color/colorButtonHiglight"/>

    <TextView
        android:id="@+id/currencyDate"
        android:layout_below="@id/currencyTitle"
        android:text="24-01-2015"
        android:textStyle="bold"
        android:layout_marginTop="12dp"
        android:gravity="center_horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <TextView
        android:id="@+id/currencyValue"
        android:layout_below="@+id/currencyDate"
        android:text="4.4567"
        android:layout_marginTop="12dp"
        android:textColor="#FFFFFF"
        android:textStyle="bold"
        android:textSize="25sp"
        android:paddingTop="10dp"
        android:paddingBottom="10dp"
        android:paddingLeft="10dp"
        android:paddingRight="10dp"
        android:layout_centerHorizontal="true"
        android:gravity="center_horizontal"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@color/colorCalculatorActivity"/>


    <TextView
        android:text="Inna data"
        android:textAllCaps="false"
        android:layout_marginTop="10dp"
        android:id="@+id/currencyButton"
        android:textSize="12sp"
        android:padding="5dp"
        android:gravity="center"
        android:layout_below="@id/currencyValue"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:background="@drawable/button_rounder_corners"
        android:layout_alignLeft="@+id/currencyValue"
        android:layout_alignRight="@+id/currencyValue"
        android:onClick="onClickDataButton"
        />


    <View
        android:layout_marginTop="10dp"
        android:layout_below="@id/currencyButton"
        android:layout_width="match_parent"
        android:layout_height="1dp"/>





</RelativeLayout>

示例包含

<LinearLayout
                android:layout_width="match_parent"
                android:weightSum="2"
                android:layout_height="wrap_content"
                android:orientation="horizontal"
                android:layout_marginTop="@dimen/activity_horizontal_margin"
                android:paddingLeft="@dimen/activity_horizontal_margin"
                android:paddingRight="@dimen/activity_horizontal_margin">


                <include
                    android:id="@+id/currency1"
                    android:layout_height="wrap_content"
                    android:layout_width="0dp"
                    android:layout_weight="1"
                    layout="@layout/single_item_foreign_exchange"/>


                <View
                    android:layout_width="10dp"
                    android:layout_height="match_parent"/>

                <include
                    android:id="@+id/currency2"
                    android:layout_height="wrap_content"
                    android:layout_width="0dp"
                    android:layout_weight="1"
                    layout="@layout/single_item_foreign_exchange"/>

            </LinearLayout>

绑定包含布局

@Bind(R.id.currency1) RelativeLayout relativeLayout1;
    @Bind(R.id.currency2) RelativeLayout relativeLayout2;

已解决

在我的 RelativeLayout 中添加了 onClick 方法。然后创建了这样的东西

public void onClickDataButton(View view){
        if (view == buttonEuro){
            Toast.makeText(ForeignExchangeActivity.this, "Euro", Toast.LENGTH_SHORT).show();
        } else if ( view == buttonGBP){
            Toast.makeText(ForeignExchangeActivity.this, "GBP", Toast.LENGTH_SHORT).show();
        }
    }

我的按钮看起来像

buttonEuro = (TextView) relativeLayout1.findViewById(R.id.currencyButton);
        buttonGBP = (TextView) relativeLayout2.findViewById(R.id.currencyButton

);

所以相同的按钮 ID 但在不同的布局中找到。

【问题讨论】:

  • 在 XML 中的按钮上使用 OnClick 函数并在函数中执行您想要的操作
  • 我没有按钮.. 一个布局中只有一个按钮.. 我多次包含此布局
  • 尝试在 onClickListener() 中创建 switch 语句或 If() 逻辑。
  • 我试过但我需要 -> R.id.exampleButtonId,R.id.exampleButtonI2, R.id.exampleButtonId3 .. 但我只有一个 Id ( R.id.currencyButton ) 怎么能我告诉点击了哪个按钮?
  • 我认为唯一的方法是检查父级的 id - 因为按钮似乎有不同的父级,对吗? check here on how to get parent's id

标签: android listener onclicklistener buttonclick butterknife


【解决方案1】:

编辑:添加的案例示例:不同布局中具有相同 ID 的按钮。

1) 您可以遍历按钮数组。

2) 或者:创建一个自定义按钮类,其中包含您需要的任何自定义内容(即货币字段等)并在您的布局中使用它。

一种伪代码(未测试):

class CurrencyButton extends android.widget.Button {
     string currency;

     public void init(Context ctx, String currency) {
       this.currency = currency;

       this.setOnClickListener(new View.OnClickListener() {
         @Override
         public void onClick(View v) {
            Toast.makeText(ctx, this.currency, Toast.LENGTH_SHORT).show();
         }
       });
     }

     public void doSomeOtherFancyCurrencyLogicHere() {
       ....
     }
}

这可以在你的布局文件中使用:(伪)

<your.package.CurrencyButton 
   id="@+id/currency_button_eur"
   LayoutParams etc />

<your.package.CurrencyButton 
   id="@+id/currency_button_gbp"
   LayoutParams etc />

<!-- EDIT: as in your example with layouts in single_item_foreign_exchange -->

<your.package.CurrencyButton 
   id="@+id/currency_button"
   LayoutParams etc />

如果你使用黄油刀

@InjectView(R.id.currency_button_eur)
your.package.CurrencyButton buttonEUR;

@InjectView(R.id.currency_button_gbp)
your.package.CurrencyButton buttonGBP;

// ... initialize them somewhere
buttonEUR.init(this, "EUR");
buttonGPB.init(this, "GBP");

// EDIT: as in your example with layouts
((your.package.CurrencyButton) relativeLayout1.findViewById(R.id.currencyButton)).init(this, "EUR");
((your.package.CurrencyButton) relativeLayout2.findViewById(R.id.currencyButton)).init(this, "GBP");

实际上,您可以更进一步,为每种货币扩展这个类,但首先应该这样做。

【讨论】:

    【解决方案2】:

    好的,我看到你已经解决了你的问题。但是由于我已经在研究这个答案,所以无论如何我都会在这里添加它:P

    class CurrencyLayout extends FrameLayout {
     String currency;
    
     // Constructors that calls init after super(...);
    
     public void init(Context ctx, AtributeSet attrs) {
       View includedLayout = LayoutInflater.fromContext(ctx).inflate(R.layout.single_item_foreign_exchange, this, false);
    
       TypedArray a = context.getTheme().obtainStyledAttributes(
                attrs,
                R.styleable.CurrencyLayout,
                0, 0);
    
        try {
            final String currency = a.getString(R.styleable.CurrencyLayout_currencyName);
        } finally {
            a.recycle();
        }
    
       includedLayout.findViewById(R.id.currencyName).setOnClickListener(new View.OnClickListener() {
         @Override
         public void onClick(View v) {
            Toast.makeText(ctx, this.currency, Toast.LENGTH_SHORT).show();
         }
       });
     }
    
    this.addView(includedLayout);
    

    }

    res/values/attrs.xml 内添加如下内容:

    <resources>
        <declare-styleable name="CurrencyLayout">
            <attr name="currencyName" format="string" />
        </declare-styleable>
    </resources>
    

    在您的 Activity 布局中:

    <LinearLayout
    android:layout_width="match_parent"
    android:weightSum="2"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:layout_marginTop="@dimen/activity_horizontal_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin">
    
    
        <your_package.CurrencyLayout
            android:id="@+id/currency1"
            android:layout_height="wrap_content"
            android:layout_width="0dp"
            android:layout_weight="1"
            custom:currencyName="GBP"/>
    
    
        <View
            android:layout_width="10dp"
            android:layout_height="match_parent"/>
    
        <your_package.CurrencyLayout
            android:id="@+id/currency2"
            android:layout_height="wrap_content"
            android:layout_width="0dp"
            android:layout_weight="1"
            custom:currencyName="EUR"/>
    
    </LinearLayout>
    

    我希望这对你有帮助:D

    更多关于custom views and attributes的信息

    【讨论】:

    • 我稍后会试试这个。谢谢!
    猜你喜欢
    • 2014-07-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-11
    • 1970-01-01
    • 2014-02-26
    相关资源
    最近更新 更多