设置在子视图中膨胀的按钮的OntouchListener

时间:2013-03-17 20:12:17

标签: android eclipse ontouchlistener layout-inflater

如何在此代码中设置PadButton PadSelect PadName的Ontouchlistener?

主要活动:

public class Drum extends Activity {

    boolean loaded = false;
    int sound;
    SoundPool soundPool = null;
    Button PadButton,PadSelect,PadName;

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

        //Remove notification bar
        this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);


        PadButton = (Button) findViewById(R.id.padButton);
        PadSelect = (Button) findViewById(R.id.padSelect);
        PadName = (Button) findViewById(R.id.padName);


        LinearLayout root = (LinearLayout)findViewById(R.id.DrumLayout);
        LinearLayout row1 = new LinearLayout(this);
        LinearLayout row2 = new LinearLayout(this);
        LinearLayout row3 = new LinearLayout(this);

        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
                 LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT,5f);

            params.setMargins(3, 3, 3, 3);

        View Vcassa = View.inflate(this, R.layout.drumpad, null);
        View Vrullo = View.inflate(this, R.layout.drumpad, null);
        View Vtom1 = View.inflate(this, R.layout.drumpad, null);
        View Vtom2 = View.inflate(this, R.layout.drumpad, null);
        View Vtom3 = View.inflate(this, R.layout.drumpad, null);

        View Vtim1 = View.inflate(this, R.layout.drumpad, null);
        View Vtim2 = View.inflate(this, R.layout.drumpad, null);
        View VhatClose = View.inflate(this, R.layout.drumpad, null);
        View VhatOpen = View.inflate(this, R.layout.drumpad, null);
        View VhatPedal = View.inflate(this, R.layout.drumpad, null);

        View VrideTin = View.inflate(this, R.layout.drumpad, null);
        View VrideCrash = View.inflate(this, R.layout.drumpad, null);
        View Vridebell = View.inflate(this, R.layout.drumpad, null);
        View Vcrash1 = View.inflate(this, R.layout.drumpad, null);
        View Vcrash2 = View.inflate(this, R.layout.drumpad, null);

        root.addView(row1);
        root.addView(row2);
        root.addView(row3);

        row1.addView(Vcassa,params);
        row1.addView(Vrullo,params);
        row1.addView(Vtom1,params);
        row1.addView(Vtom2,params);
        row1.addView(Vtom3,params);

        row2.addView(Vtim1,params);
        row2.addView(Vtim2,params);
        row2.addView(VhatClose,params);
        row2.addView(VhatOpen,params);
        row2.addView(VhatPedal,params);

        row3.addView(VrideTin,params);
        row3.addView(VrideCrash,params);
        row3.addView(Vridebell,params);
        row3.addView(Vcrash1,params);
        row3.addView(Vcrash2,params);


    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.activity_drum, menu);
        return true;
    }

    @Override
    protected void onResume(){
        super.onResume();

        if (soundPool == null) {
            soundPool = new SoundPool(1, AudioManager.STREAM_MUSIC, 100);
            soundPool.setOnLoadCompleteListener(new OnLoadCompleteListener() {
                @Override
                public void onLoadComplete(SoundPool soundPool, int sampleId,
                        int status) {
                    loaded = true;
                }
            });
        }
    }
}

夸大的布局:

<?xml version="1.0" encoding="utf-8"?>
<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>

主要布局:

<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>

我无法抓住按钮的特定实例的点击

2 个答案:

答案 0 :(得分:0)

您已有权访问该对象。只需将OnTouchListener设置为正常。

PadButton = (Button) findViewById(R.id.padButton);
PadSelect = (Button) findViewById(R.id.padSelect);
PadName = (Button) findViewById(R.id.padName);

PadButton.setOnTouchListener( new OnTouchListener() {

        @Override
        public boolean onTouch( View v, MotionEvent event ) {

            return false;
        }
    } );
PadSelect.setOnTouchListener( new OnTouchListener() {

        @Override
        public boolean onTouch( View v, MotionEvent event ) {

            return false;
        }
    } );
PadName.setOnTouchListener( new OnTouchListener() {

        @Override
        public boolean onTouch( View v, MotionEvent event ) {

            return false;
        }
    } );

答案 1 :(得分:0)

只需在Click侦听器上添加常规

即可
    PadButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub

        }
    });