开发者

onclick event problem in android

I have some TextViews, EditTexts and Buttons in a linear layout. If I touch/click any side of the LinearLayout it goes to the next activity. I have 开发者_开发问答tried setting onClick listeners for all elements in the LinearLayout. This works but is there any way to write code for touching any area of the LinearLayout to go next activity without needing to add the onClick handler for all elements? Because there are more elements in the layout. Please assist me.


Try setting onClick listener event for the linerlayout from code as below

LinearLayout l1 = (LinearLayout)findViewById(R.id.layout1);
l1.setOnClickListener(this);
l1.setClickable(true);


Try this one.

login.java

package com.android;


import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class Login extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        Button newuser = (Button) findViewById(R.id.Button02);
        newuser.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                Intent myIntent = new Intent(view.getContext(), Register.class);
                startActivityForResult(myIntent, 0);
            }

        });
    }
}




Register.java


package com.android;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class Register extends Activity {

    /** Called when the activity is first created. */
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.register);

        Button register = (Button) findViewById(R.id.Button03);
        register.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                Intent intent = new Intent();
                setResult(RESULT_OK, intent);
                finish();
            }

        });
    }}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜