添加onclick监听器到预定义按钮?

时间:2011-03-22 17:09:13

标签: android

我的xml布局文件中有以下按钮...

<Button
    android:layout_width="150dip"
    android:id="@+id/button1"
    android:layout_height="50dip"
    android:text="@string/login"
    android:layout_marginRight="10dip">
</Button>

我想以编程方式在其Java文件中添加onclick()侦听器。我该怎么做?

6 个答案:

答案 0 :(得分:75)

你只需要这样的东西:

Button buttonOne = (Button) findViewById(R.id.button1);
buttonOne.setOnClickListener(new Button.OnClickListener() {
    public void onClick(View v) {
            //Do stuff here
    }
});

答案 1 :(得分:19)

这个答案来自Five Ways to Wire Up an Event Listener。请阅读该博文,以获得作者更全面的解释。请参阅我的另外一个答案,将这五种方法重新修改为add multiple onClick listeners

1。会员等级

public class main extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        //attach an instance of HandleClick to the Button
        findViewById(R.id.button1).setOnClickListener(new HandleClick());
    }    
    private class HandleClick implements OnClickListener{
        public void onClick(View arg0) {
            Button btn = (Button)arg0;  //cast view to a button
            // get a reference to the TextView
            TextView tv = (TextView) findViewById(R.id.textview1);
            // update the TextView text
            tv.setText("You pressed " + btn.getText());
        }
    }
}

2。接口类型

public class main extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        //use the handleClick variable to attach the event listener
        findViewById(R.id.button1).setOnClickListener(handleClick);
    }    
    private OnClickListener handleClick = new OnClickListener(){
        public void onClick(View arg0) {
            Button btn = (Button)arg0;
            TextView tv = (TextView) findViewById(R.id.textview1);
            tv.setText("You pressed " + btn.getText());
        }
    };
}

3。匿名内部班级

public class main extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        findViewById(R.id.button1).setOnClickListener(new OnClickListener(){
            public void onClick(View arg0) {
            Button btn = (Button)arg0;
            TextView tv = (TextView) findViewById(R.id.textview1);
            tv.setText("You pressed " + btn.getText());
            }
        });
    }     
}

4。在Activity中实现

public class main extends Activity implements OnClickListener{
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        findViewById(R.id.button1).setOnClickListener(this);
    }    
    public void onClick(View arg0) {
        Button btn = (Button)arg0;
        TextView tv = (TextView) findViewById(R.id.textview1);
        tv.setText("You pressed " + btn.getText());
    }
}

5。 OnClick事件的视图布局中的属性

public class main extends Activity{
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }    
    public void HandleClick(View arg0) {
        Button btn = (Button)arg0;
        TextView tv = (TextView) findViewById(R.id.textview1);
        tv.setText("You pressed " + btn.getText());
    }
}

的xml:

<Button android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button 1"
        android:onClick="HandleClick"/>

答案 2 :(得分:6)

你可以试试这个。

public class myNewClass extends Activity implements OnClickListener {
    ................... 
    ...................       

    Button button = (Button)findViewById(R.id.button1);
    button.setOnClickListener(this);

        public void onClick(View v) {
                Intent i = new Intent();
                Bundle extras = new Bundle();

        // This will catch the button click 
        // Now do what you wanted to do as a 
        // result of the onClick
        }
 }

答案 3 :(得分:0)

1.使用findViewById(R.id.button1).setOnClickListener(this);

2.and activty实现OnClickListener

答案 4 :(得分:0)

您可以通过两种方式申请onClicklistner: 的 1。在onCreate方法下
2。在创建方法

之外

如果我们将在onCreate方法下使用,那么我们将使用如下: -

/AI

}

如果您想要onCreate方法,那么首先在onCreate方法中注册onClickListner,如下所示: -

protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_graphics1);

        textview1 = (TextView) findViewById(R.id.textview1);
        circleBtn = (Button) findViewById(R.id.circleBtn);

        // Click Listner Under on Create Method
        circleBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });

然后在onCreate方法之外实现clicklistner,所以完整的代码将是这样的: -

@Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_graphics1);

        circleBtn = (Button) findViewById(R.id.circleBtn);

        **// Register on click on button
        circleBtn.setOnClickListener(new ClickMe());**

    }

答案 5 :(得分:0)

<Button
    android:id="@+id/btnOk"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="Ok"
    android:layout_marginRight="50dp"
    android:layout_marginLeft="50dp"/>
public class MainActivity extends AppCompatActivity {

    Button btnOk;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        btnOk = findViewById(R.id.btnOk);
        btnOk.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(MainActivity.this, "This button ok click.", Toast.LENGTH_SHORT).show();
            }
        });
    }
}