安卓:Eclipse的:这是添加一个按钮的最佳方法? [英] Android: Eclipse: Which is the best way to add a button?

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

问题描述

我是新来的android的发展。我有一个疑问。 我知道,你可以添加一个按钮并初始化它像

 按钮B1 =(按钮)findViewById(R.id.button1);
 

和我也可以在XML文件中提供一个恩膏的名字。

 安卓的onClick =click_event
 

我的疑问是,这是最好的和有效的方法是什么? 像它说,它能够更好地使用的硬codeD一@string资源来代替。

解决方案

我想你感到困惑。你给的例子是两回事。

添加一个按钮

这行

 按钮B1 =(按钮)findViewById(R.id.button1);
 

不添加按钮。该声明并初始化实例按钮这是指按钮在当前膨胀的XML其中有一个 ID 按钮1

因此​​,在你的XML,你就会有地方

 <按钮
     机器人:ID =@ + ID /按钮1
     <! - 其它性能 - >
/>
 

您可以编程方式添加按钮与

 按钮BT1 =新的按钮(这一点);
//给它的属性
 

但它通常是容易做到的XML,因为在这里你必须编程给它的参数,性能,并将其添加到一个充气布局

的OnClick

至于的onClick()这取决于你觉得什么是最简单,最适合您的情况。我喜欢经常传扬在这样的XML,但你可以做到这几个方面。使用这种方法,你只需要确保你有一个像这样的是一个函数公开键,只需要一个参数和参数必须是查看

 公共无效clickEvent(视图v)
{
    // code在这里
}
 

我也改了名称,以便您的XML将会像

 <按钮
     机器人:ID =@ + ID /按钮1
     <! - 其它性能 - >
     机器人:的onClick =clickEvent/>
 

您还可以设置的onClick()在Java的东西,如

 按钮B1 =(按钮)findViewById(R.id.button1);
b1.setOnClickListener(新OnClickListener()
{
    @覆盖
    公共无效的onClick(视图v)
    {
        // code在这里
    }
});
 

 按钮B1 =(按钮)findViewById(R.id.button1);
b1.setOnClickListener(本);

    @覆盖
    公共无效的onClick(视图v)
    {
        // code在这里
    }
 

请注意,您将需要添加过去的方式实现OnClickListener 活动声明

 公共类MyActivity扩展活动实现OnClickListener
{
 

您还可以通过将其更改为类似

创建自己的点击监听器

  b1.setOnClickListener(myBtnClick);
 

然后创建一个类似

它的一个实例

 公共OnClickListener myBtnClick =新OnClickListener()
{
    @覆盖
    公共无效的onClick(视图v)
    {
        //单击code在这里
    }
};
 

您可以使用此为多个按钮和交换机上的 ID 或检查查看参数知道哪些按钮被点击或创建单独的监听器不同的按钮秒。

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.

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

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