2012-02-24 85 views
1

我有一個應用程序代碼,它包含一個相對佈局和一個表格,裏面有一組按鈕。我需要處理intercepttouchevent,我想要做的是我想要檢索被點擊的按鈕的文本,並使用它的文本將其設置爲一個字符串。但我想知道如何爲每個按鈕和父佈局實現onintercepttouchevent和ontouchevent方法。我想知道如何檢索按鈕的文本以及如何處理各種動作,如DOWN,MOVE和UP事件。所以請給我一個清楚的例子,說明如何實現這一點。如何通過一個例子來實現onintercepttouchevent方法。如何獲取onintercepttouchevent中的視圖文本

這裏是我的代碼

public class ContactActivity extends Activity 
implements OnInitListener,OnTouchListener 
{ 
    /** Called when the activity is first created. */ 
    Handler mhandler = new Handler(); 
    Button mybtn[] = new Button[10]; 
    // int[] mybtn1 = new int[] {R.id.btn0,R.id.btn1,R.id.btn2,R.id.btn11}; 

    Button select_btn, home_btn; 
    View l; 
    EditText et; 
    Bundle b; 
    Intent in; 
    int var = 0; 
    int pass = 1; 
    int quit = 1; 
    int current = 0; 
    boolean status = false; 
    private int MY_DATA_CHECKCODE = 0; 
    private TextToSpeech tts = null; 
    Runnable mUpdateTimeTask = null; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 

     // select_btn = (Button) findViewById(R.id.ok); 
     // home_btn = (Button) findViewById(R.id.home); 
     et = (EditText) findViewById(R.id.entry); 
     et.setText("", TextView.BufferType.EDITABLE); 
     l = (View) findViewById(R.id.main_layout); 
     b = new Bundle(); 


     for (int i = 0; i < mybtn.length; i++) { 
      String btnid = "btn" + i; 
      int resid = getResources().getIdentifier(btnid, "id", 
        getPackageName()); 
      mybtn[i] = (Button) findViewById(resid); 
      mybtn[i].setOnTouchListener(this); 

     } 


    } 

    public boolean onInterceptTouchEvent(MotionEvent ev) { 
     final int action = ev.getAction(); 

     // final float x = ev.getX(); 

     switch (action) { 

     case MotionEvent.ACTION_DOWN: 

      // Remember each down event, 
      // as it is needed in startDragging above 
      // mLastMotionX = x; 
      status = false; 
      break; 

     case MotionEvent.ACTION_MOVE: 
      status = true; 
      break; 
     case MotionEvent.ACTION_UP: 
      status = false; 
      break; 
     } 
     // Whilst dragging, we return true in order to dispatch 
     // the MotionEvent to our onTouchEvent method below. 
     // return mIsDragging; 
     return status; 
    } 

    public boolean onTouchEvent(MotionEvent ev) { 

     if (!status) { 
      // Not in dragging mode, so no tyaction taken 
      return super.onTouchEvent(ev); 
     } 

     final int action = ev.getAction(); 
     // float x = ev.getX(); 

     switch (action) { 

     case MotionEvent.ACTION_DOWN: 
      // et.setText("down android" + s, TextView.BufferType.EDITABLE); 
      break; 

     case MotionEvent.ACTION_MOVE: 
      // Move the object. 
      // doDrag(x); 
      // Call back to user 
      // callMoveCallback(x); 
      break; 

     case MotionEvent.ACTION_CANCEL: 
      break; 
     case MotionEvent.ACTION_UP: 
      // stopDragging(x); 
      // et.setText("down android" + s, TextView.BufferType.EDITABLE); 
      break; 
     } 
     return true; 
    } 

} 

但我不知道,如何實現上述兩種方法的每一個按鈕和父佈局。我也想知道如何檢索觸摸的按鈕的文本。我已經嘗試了ontouch(MotionEvent,View)方法。我可以使用上述兩種方法嗎?怎麼做?

還有一兩件事:

View l = (View)findViewById(R.id.main_layout); 

它是罰款?如何檢索android中的佈局?在裏面我有桌子,裏面有按鈕,如何處理它們?

回答

0

你可以使用onClickListener代替onTouchListener

mybtn[i].setOnClickListener(this); 

然後,在你的onClick監聽器,你可以得到該按鈕的字符串或ID,做你想做的事情。

public void onClick(View v) 
{ 
    String buttontext = (String) ((Button) v).getText(); 

    int id = v.getId(); 

    switch(id) 
    { 
    case R.id.button1 
    .... 
    } 
} 
+0

感謝您的建議,但它不是我的問題的解決方案。而不是在用戶觸摸按鈕時點擊,然後文本應該被設置。我想要處理DOWN,MOVE,UP事件,而不是點擊事件。如果可能的話,請給我提供一個使用oncepttouchevent的例子 – siva 2012-02-24 08:15:48

相關問題