2012-02-24 17 views
1

私は相対レイアウトとテーブルで構成されたアプリケーションコードを持っており、その中に一連のボタンがあります。私はonintercepttoucheventを処理する必要があります。私がしたいのは、クリックされたボタンのテキストを取得し、そのテキストを文字列で設定することです。ボタンと親のレイアウトごとに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; 
    } 

} 

あるしかし、私はそれぞれ、すべてのボタンと親のレイアウトのための上記の2つのメソッドを実装する方法を、知りません。私はまた、タッチされたボタンのテキストを取得する方法を知りたい。 ontouch(MotionEvent、View)メソッドを試しました。上記の2つの方法を使用してそれを行うことはできますか?どうやってするの?

そしてもう一つ:それは罰金

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

ですか?どのようにアンドロイドでレイアウトを取得するには?その中にテーブルがあり、その中にボタンがあり、それをどう扱うか?

答えて

0

あなたのonClickリスナーにボタンの文字列またはIDを取得して、必要なことを行うことができ、そして、代わりにonTouchListener

mybtn[i].setOnClickListener(this); 

のonClickListener使用することができます。

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

    int id = v.getId(); 

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

私の問題の解決策ではありません。ユーザーがボタンに触れるときにクリックするのではなく、テキストを設定する必要があります.i DOWN、MOVE、UPイベントは処理したいが、クリックイベントは処理しない。可能であれば、例を使ってonintercepttoucheventを使用する明確な例を私に教えてください – siva

関連する問題