简体   繁体   English

在Android / Eclipse中将按钮添加到Java代码

[英]Add button to java code in Android / Eclipse

Is it possible to automatically add buttons and lists to the java code from the fragment layout in Eclipse? 是否可以从Eclipse中的片段布局向Java代码自动添加按钮和列表?

I am thinking like it's done in C# / Visual studio where you just assign a name to the listener and the rest is added automatically in the code. 我想这就像在C#/ Visual Studio中完成的一样,您只需为侦听器分配一个名称,其余的就会自动添加到代码中。

I know how to do it manually. 我知道如何手动执行。 But it's just slightly cumbersome if you have many controls. 但是,如果您有许多控件,那只会有点麻烦。

It is not possible, but you could assing the same method to all the Views , for example android:onClick="handleClick" and then in your code you need to declare a method like this: 这是不可能的,但是您可以对所有Views都使用相同的方法,例如android:onClick="handleClick" ,然后在代码中需要声明如下方法:

public void handleClick(View target){
    switch(target.getId()){
    case R.id.View1:
        /// handle Click on View1
        break;
    case R.id.View2:
        // Handle click on view2
        break;
    /// ,......
    }
}

And so you only need one method, and then handle the click event by adding a case to the switch 因此,您只需要一种方法,然后通过向switch添加case来处理click事件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM