添加按钮的最佳方式是什么? [英] Which is the best way to add a button?

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

问题描述

我是 Android 开发的新手.我有一个疑问.我知道你可以添加一个按钮并像

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);

我也可以在 XML 文件中给出一个函数名称.

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

  android:onClick="click_event"

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

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.

添加按钮

这一行

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

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

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

所以在你的 xml 中你会有一个地方

So in your xml you would have somewhere

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

您可以使用

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

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

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() 而言,这取决于您认为在您的情况下最简单和最好的方法.我喜欢经常在 xml 中声明它,但您可以通过多种方式进行声明.使用这种方法,你只需要确保你有一个这样的函数,它是 public 并且只接受一个参数,并且该参数必须是一个 View

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
}

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

I also changed the name so your xml would be like

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

你也可以在你的 Java 中设置 onClick() 类似

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
    }
});

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

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

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

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      
    }
};

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

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天全站免登陆