Android 将OnTouchListener设置为xml文件中包含的多个按钮

Android 将OnTouchListener设置为xml文件中包含的多个按钮,android,ontouchlistener,android-inflate,Android,Ontouchlistener,Android Inflate,我需要在一个主布局活动(按3行5列排列)上将此布局充气15次,并为每个按钮padButtonistance设置OnTouchListener,这是最好的方法吗??我试图膨胀布局,但并没有想法设置分离 用于膨胀drumpad.xml的布局 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/padLayout" android:layout_widt

我需要在一个主布局活动(按3行5列排列)上将此布局充气15次,并为每个按钮
padButton
istance设置OnTouchListener,这是最好的方法吗??我试图膨胀布局,但并没有想法设置分离

用于膨胀drumpad.xml的布局

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/padLayout"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/pad"
    android:orientation="vertical"
    android:padding="3dp" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center_vertical|clip_horizontal|top"
        android:orientation="horizontal" >

        <Button
            android:id="@+id/padButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/titles2" />

        <Button
            android:id="@+id/padSelect"
            style="?android:attr/buttonStyleSmall"
            android:layout_width="15dp"
            android:layout_height="wrap_content" />

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="21dp" >

        <Button
            android:id="@+id/padName"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="2dp"
            android:background="#0f0f0f"
            android:padding="2dp"
            android:text="@string/pad"
            android:textColor="#dfdfdf"
            android:textSize="10sp" />

    </LinearLayout>

</LinearLayout>

主布局(容器)活动_drum.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/DrumLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#999"
    android:gravity="center"
    android:orientation="vertical"
    tools:context=".Drum" >

</LinearLayout>

我不确定您可以有多个具有相同id的按钮。但是,您可以获取根视图,将其转换为
视图组。使用
getChildCount()
getChildAt()
,并根据需要递归。像这样:

//we start from the root view
ViewGroup rootViewGroup = findViewById(R.id.rootLinearLayout);

for (int i = 0; i < this.getChildCount(); i++) {
    View childView = this.getChildAt(i);

    //is it a button?
    if (childView instanceof Button) {
        Button button = (Button) childView;
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //insert here the code
            }
        });
    }

    if (childView instanceof ViewGroup) {
        //iterate throught childView children
    }
}
//我们从根视图开始
ViewGroup rootViewGroup=findViewById(R.id.rootLinearLayout);
for(int i=0;i
好吧,你不能不费一点力气就把这段代码用在工作上。这只是一个起点。但是

我认为您需要重新思考您的方法,并尝试开发自己的自定义视图,我将使用复合视图,请看这里:

你需要一点练习,但我认为这对你的目的来说是很好的

编辑

也许你可以在这里找到一些有用的信息:

在这里:

这里(一台鼓式机器):


我不确定您可以拥有多个具有相同id的按钮。但是,您可以获取根视图,将其强制转换为
视图组。使用
getChildCount()
getChildAt()
,并根据需要递归。像这样:

//we start from the root view
ViewGroup rootViewGroup = findViewById(R.id.rootLinearLayout);

for (int i = 0; i < this.getChildCount(); i++) {
    View childView = this.getChildAt(i);

    //is it a button?
    if (childView instanceof Button) {
        Button button = (Button) childView;
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //insert here the code
            }
        });
    }

    if (childView instanceof ViewGroup) {
        //iterate throught childView children
    }
}
//我们从根视图开始
ViewGroup rootViewGroup=findViewById(R.id.rootLinearLayout);
for(int i=0;i
好吧,你不能不费一点力气就把这段代码用在工作上。这只是一个起点。但是

我认为您需要重新思考您的方法,并尝试开发自己的自定义视图,我将使用复合视图,请看这里:

你需要一点练习,但我认为这对你的目的来说是很好的

编辑

也许你可以在这里找到一些有用的信息:

在这里:

这里(一台鼓式机器):


OnTouchListener是否始终是每个按钮的接缝?或者你想为每个按钮显示不同的操作?但是我不确定你可以有多个具有相同id的按钮…如果我为每个按钮设置标签,我能做什么?示例:button.setTag等。。。我需要一个例子来获得正确的布局方式。我可以用for循环吗?是的,我想你可以。看看我的anwser,如果你能设置一个标签,你就能在子迭代期间识别出按钮。在某种程度上,你必须“做点什么”来识别每一个按钮……我们是在谈论鼓机的垫子还是其他东西?每个按钮的接缝都是在听音乐吗?或者你想为每个按钮显示不同的操作?但是我不确定你可以有多个具有相同id的按钮…如果我为每个按钮设置标签,我能做什么?示例:button.setTag等。。。我需要一个例子来获得正确的布局方式。我可以用for循环吗?是的,我想你可以。看看我的anwser,如果你能设置一个标签,你就能在子迭代期间识别出按钮。在某种程度上,你必须“做点什么”来识别每一个按钮……我们是在谈论鼓机的垫子还是什么?