How to add a button in android?

2019-02-08 07:05发布

Can anybody tell how to add a button in android?

4条回答
时光不老,我们不散
2楼-- · 2019-02-08 07:10

Check this Android Button tutorial; this simple example creates a Close Button.

All you need to do is:

1.Add Button widget to your Layout

<Button android:id="@+id/close"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:text="@string/title_close" />

2.Attach a setOnClickListener method to the button instance:

protected void onCreate(Bundle savedInstanceState) {
  this.setContentView(R.layout.layoutxml);
  this.closeButton = (Button)this.findViewById(R.id.close);
  this.closeButton.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View v) {
      finish();
    }
  });
}
查看更多
倾城 Initia
3楼-- · 2019-02-08 07:17

Adding a Button

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.

查看更多
叛逆
4楼-- · 2019-02-08 07:18

Dynamic:

Button btn= new Button(this);  
btn.settext("Submit");  
btn.setOnClickListener(new View.OnClickListener()   
{
    public void onClick(View view) 
     {
           //your write code
       }
});
查看更多
淡お忘
5楼-- · 2019-02-08 07:24

According to official documentation of Buttons provided by Android. You can first create Button in your .xml file.

Button.xml

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/button_text"
... />

And then cast your button with Button Class and set ClickListener.

Button button = (Button) findViewById(R.id.button_send);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
    // Do something in response to button click
}
  });

For further detail you can visit this link

查看更多
登录 后发表回答