2013-03-17 76 views
0

如何在此代碼中設置PadButtonPadSelectPadName的Ontouchlistener?設置在子視圖中充氣的按鈕的OntouchListener

主要actyvity:

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> 

我不能趕上按鈕的具體實例的點擊

回答

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; 
     } 
    }); 
+0

的Eclipse給我一個錯誤空指針,我不能趕上按鈕的具體實例的點擊 – kosma822 2013-03-17 20:54:26

0

只需添加一個正常的點擊收聽

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

     } 
    });