如何为TextView设置监听器?

时间:2011-07-12 06:20:51

标签: android textview

我已经为TextView设置了监听器,但它无法正常工作。我想在点击文本时启动活动。我已经放置了我的代码,任何人都可以建议我进行任何更改。

// This is my code

public void onClick(View v) {
        // TODO Auto-generated method stub

        int i = v.getId();
    //String roomName = textView[i].getText().toString();
    //Intent intentDeviceOperation = new Intent(v.getContext),DeviceOperation.class);
    //startActivity(intentDeviceOperation);

    //Intent i1 = new Intent(v.RoomForSpecificUser.this,DeviceOperation.class);
    //RoomForSpecificUser.this.startActivityForResult(i1,7);
    Intent ii = new Intent();
    ii.setClass(RoomForSpecificUser.this, DeviceOperation.class);

    }

它没有开始另一项活动,甚至也没有收到任何错误。

4 个答案:

答案 0 :(得分:6)

这样做,事情会有效

TextView tv = (TextView) findViewById(R.id.textView);
tv.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                Intent ii = new Intent();
                ii.setClass(RoomForSpecificUser.this, DeviceOperation.class);

                            startActivity(ii);

            }
        });

答案 1 :(得分:0)

您还没有开始活动:)

Intent ii = new Intent();
ii.setClass(RoomForSpecificUser.this, DeviceOperation.class);
startActivity(ii);

答案 2 :(得分:0)

    class RoomForSpecificUser extends Service implements OnClickListener{
    @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
TextView tv = (TextView) findViewById(R.id.textView);

    tv.setOnclickListener(this);


        }
    public void onClick(View v) { // TODO Auto-generated method stub

        int i = v.getId();
    //String roomName = textView[i].getText().toString();
    //Intent intentDeviceOperation = new Intent(v.getContext),DeviceOperation.class);
    //startActivity(intentDeviceOperation);

    //Intent i1 = new Intent(v.RoomForSpecificUser.this,DeviceOperation.class);
    //RoomForSpecificUser.this.startActivityForResult(i1,7);
    Intent ii = new Intent();
    ii.setClass(RoomForSpecificUser.this, DeviceOperation.class);

    }


    }

答案 3 :(得分:0)

              " Simply used Button Listener As TextView or EditText Listener  "

TextView电视=(TextView)findViewById(R.id.textView);

tv.setOnClickListener(new View.OnClickListener(){

    @Override
    public void onClick(View v) {

                                               //Action performed

                                }

                    });