2017-10-13 6 views
1

いくつかのボタンがあり、それらのクリックが共通のハンドラによって処理されると、リスナを一度定義し、各ボタンにonClick属性を定義しない方法がありますか?ブラウザや一部のインターセプタのようAndroidのリスナー(共通)クリック

<Button android:onClick="handler" /> 

おそらく親委任.....あなたは50個のボタンを持っている想像し、あなたはそれぞれの明示のクリックで宣言します?

+0

onClick(View v)メソッド –

+0

を使用するすべてのボタンに対して同じクリックハンドラを指定してから、onClick()メソッドでさまざまな操作にスイッチケースを使用できます。 –

答えて

1

は、ここで私は最終的に解決策としてに来た何それは例えばTextViewためtouchablesでのみ動作します触れることではありません。

Javaで:

Kotlinで
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    final MainActivity activity = this; 
    /* Any kind of ViewGroup that is 
     parent of the common listener views. In the example it is the rootView */ 
    View rootView = this.getWindow().getDecorView().getRootView(); 

    // Buttons are touchables 
    ArrayList<View> touchables = rootView.getTouchables(); 

    // Define the common on click listener for the buttons 
    View.OnClickListener listener = new View.OnClickListener() { 
     public final void onClick(View view) { 
      activity.onBtnClick((Button) view); 
     } 
    }; 

    // Iterate trough touchables and if the View is a button assign the listener 
    for (View v: touchables) { 
     // TODO: omit button if that common listener isn't meant for it 
     if(v instanceof Button) { 
      v.setOnClickListener(listener); 
     } 
    } 
} 

// The listener callback 
protected final void onBtnClick(Button btn) { 
    String btnStringId = btn.getResources().getResourceEntryName(btn.getId()); 
} 

override fun onCreate(savedInstanceState: Bundle?) { 
    super.onCreate(savedInstanceState) 
    setContentView(R.layout.activity_main) 

    /* Any kind of ViewGroup that is 
     parent of the common listener views. In the example it is the rootView */ 
    val rootView = window.decorView.rootView 
    val touchables = rootView.touchables 
    val listener = View.OnClickListener { 
     onBtnClick(it as Button) 
    } 
    touchables.forEach { if (it is Button) { 
     it.setOnClickListener(listener) 
    }} 
} 

protected fun onBtnClick(btn: Button) { 
    var btnStringId = btn.getResources().getResourceEntryName(btn.id) 
} 
2

最善の方法:

public void onClick(View v) { 
    final int id = v.getId(); 
    switch (id) { 
    case R.id.button1: 
     // your code for button1 here 
     break; 
    case R.id.button2: 
     // your code for button2 here 
     break; 
    // even more buttons here 
    } 
} 

次に、あなたのXMLレイアウトファイルでは、あなたが直接属性android:onClick:

を使用して、クリックリスナーを設定することができますちょうどあなたの活動は View.OnClickListenerを実装しており、このようなあなたの onClick方法を書いてみましょう
<Button 
    android:id="@+id/button1" 
    android:onClick="onClick" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="Button 1" /> 
+0

しかし、それは私が各ボタンに対して 'onClick'を明示的に宣言しないようにしたいのです。 –

+0

これで大丈夫です。一般的なonClickListenerを試してみてください。そのリスナーをボタンに割り当てます。 –

1

はい、プログラムレベルでアクティビティレベルで達成できます。したがって、XMLで "onClick"属性を定義する必要はありません。

View.OnClickListenerクラスを定義します。例えば、

View.OnClickListener buttonListener = new View.OnClickListener() { 
     public void onClick(View v) { 
      // here you can define different code for different buttons 
     }; 

あなたははい、あなたがそうすることができ

final Button button = findViewById(R.id.button_id); 
    button.setOnClickListener(buttonListener); 
1

一箇所にすべてのボタンにこのリスナーを添付してください。

import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 
import android.util.Log; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.Button; 

import java.util.ArrayList; 
import java.util.List; 

public class MainActivity extends AppCompatActivity { 
List<Button> buttons; 
ViewGroup parentView; 


@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    buttons = new ArrayList<>(); 
    parentView = (ViewGroup) findViewById(R.id.cord); 


    //Replace the above line with the container view i.e. 
    //Replace parentView = (ViewGroup) findViewById(R.id.cord); 
    // With parentView = (ViewGroup) findViewById(R.id.<YOUR MAIN VIEW/PARENT CONTAINER>); 
    //R.id.cord is R.id.<YOUR MAIN VIEW/PARENT CONTAINER> for me because my Relative Layout 
    // container's name is cord. 


    for(int i=0; i < parentView.getChildCount(); i++) { 

     childView = parentView.getChildAt(i); 
     //The if may change there are other options try them too. 
     if(childView.getClass().getName().substring(35).equals("Button")){ 


      buttons.add((Button)parentView.getChildAt(i)); 
     } 
     /* 
     //Else part optional 
     //Remove comment to use 
     else 
      Log.e("Not","A Button");*/ 
    } 

    for (int i =0;i<buttons.size();i++){ 
     buttons.get(i).setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 
       //Do the work here 
       Log.d("Button Clicked",Integer.toString(view.getId())); 
      } 
     }); 
    } 





    } 
} 

これはあなたの問題を解決することができます方法です...

+0

使用するボタンの数が分かれば、リストの代わりに配列を使用できます。 –

関連する問題