Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
99 views
in Technique[技术] by (71.8m points)

android - Which is the best way to add a button?

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.

Question&Answers:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...