如何在onintercepttouchevent中获取视图文本

时间:2012-02-24 07:29:15

标签: android

我有一个应用程序代码,它由一个相对布局和一个表组成,里面有一组按钮。我需要处理onintercepttouchevent,我想要做的是我想要检索被单击的按钮的文本并使用其文本将其设置为字符串。但我想知道如何为每个按钮和父布局实现onintercepttouchevent和ontouchevent方法。我想知道如何检索按钮的文本以及如何处理各种动作,如DOWN,MOVE和UP事件。所以请给我一个如何实现这一目标的明确例子。如何用一个例子来实现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;
    }

}

但我不知道,如何为每个Button和父布局实现上述两种方法。我还想知道如何检索触摸的按钮文本。我尝试过ontouch(MotionEvent,View)方法。我可以使用以上两种方法吗?怎么做?

还有一件事:

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

没事吗?如何在android中检索布局?在里面我有桌子,里面有按钮,如何处理它们?

1 个答案:

答案 0 :(得分: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
    ....
    }
}