哪个是添加按钮的最佳方法? [英] Which is the best way to add a button?

查看:61
本文介绍了哪个是添加按钮的最佳方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是android开发的新手.我很怀疑 我知道您可以添加一个按钮并对其进行初始化

Button b1=(Button) findViewById(R.id.button1);

并且我还可以在XML文件中给出一个联合名称.

  android:onClick="click_event"

我的疑问是,哪种方法是最好,最有效的? 就像它说的那样,最好使用@string资源而不是硬编码的资源.

解决方案

我认为您很困惑.您提供的示例是两件事.

添加按钮

此行

Button b1=(Button) findViewById(R.id.button1);

不添加Button.它声明并初始化Button的实例,该实例引用当前膨胀的xml中idbutton1

Button

因此在您的xml中,您将有一个地方

<Button
     android:id="@+id/button1"
     <!-- other properties -->
/>

您可以通过

以编程方式添加Button

Button bt1 = new Button(this);
// give it properties

但是通常在xml中更容易实现,因为在这里您必须以编程方式为其提供参数,属性,并将其添加到膨胀的layout

OnClick

onClick()而言,这取决于您所感到的最简单和最佳的情况.我喜欢经常在xml中声明它,但是您可以通过几种方法来声明它.使用此方法,只需确保您具有类似这样的函数,即public并且仅接受一个参数,并且该参数必须为View

 public void clickEvent(View v)
{
    // code here
}

我还更改了名称,因此您的xml就像

<Button
     android:id="@+id/button1"
     <!-- other properties -->
     android:onClick="clickEvent"/>

您还可以在Java中使用类似的设置onClick()

Button b1=(Button) findViewById(R.id.button1);
b1.setOnClickListener(new OnClickListener()
{
    @Override
    public void onClick(View v)
    {
        // code here
    }
});

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

    @Override
    public void onClick(View v)
    {
        // code here
    }

请注意,您需要在Activity声明中添加implements OnClickListener的最后一种方法

public class MyActivity extends Activity implements OnClickListener
{

您还可以通过将其更改为类似的内容来创建自己的点击Listener

b1.setOnClickListener(myBtnClick);

然后使用类似的东西创建它的实例

public OnClickListener myBtnClick = new OnClickListener()
{
    @Override
    public void onClick(View v)
    {
        // click code here      
    }
};

您可以将其用于多个Button并打开id或检查View参数以了解单击了哪个Button或为不同的Button创建单独的Listeners.

I'm new to android development. I've a doubt. I know that you can add a button and initialize it like

Button b1=(Button) findViewById(R.id.button1);

and I can also give a unction name in the XML file.

  android:onClick="click_event"

My doubt is, which is the best and efficient way? like it says that its better to use @string resource instead of a hard-coded one.

解决方案

I think you are confused. The examples you give are two different things.

Adding a Button

This line

Button b1=(Button) findViewById(R.id.button1);

doesn't add a Button. It declares and initializes an instance of Button which refers to a Button in your currently inflated xml which has an id of button1

So in your xml you would have somewhere

<Button
     android:id="@+id/button1"
     <!-- other properties -->
/>

You can add a Button programmatically with

Button bt1 = new Button(this);
// give it properties

But it is generally easier to do in xml because here you have to programmatically give it parameters, properties, and add it to an inflated layout

OnClick

As far as the onClick() it depends on what you feel is the easiest and best in your situation. I like to declare it in the xml like that often but you can do it several ways. Using this method you just have to be sure that you have a function like this that is public and takes only one parameter and that parameter must be a View

 public void clickEvent(View v)
{
    // code here
}

I also changed the name so your xml would be like

<Button
     android:id="@+id/button1"
     <!-- other properties -->
     android:onClick="clickEvent"/>

You also can set onClick() in your Java with something like

Button b1=(Button) findViewById(R.id.button1);
b1.setOnClickListener(new OnClickListener()
{
    @Override
    public void onClick(View v)
    {
        // code here
    }
});

or

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

    @Override
    public void onClick(View v)
    {
        // code here
    }

Note that the last way you will need to add implements OnClickListener in your Activity declaration

public class MyActivity extends Activity implements OnClickListener
{

You can also create your own click Listener by changing it to something like

b1.setOnClickListener(myBtnClick);

then create an instance of it with something like

public OnClickListener myBtnClick = new OnClickListener()
{
    @Override
    public void onClick(View v)
    {
        // click code here      
    }
};

You can use this for multiple Buttons and switch on the id or check the View param to know which Button was clicked or create separate Listeners for different Buttons.

这篇关于哪个是添加按钮的最佳方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆