如何在android中添加按钮?

vij*_*jay 19 android button

任何人都可以告诉如何在Android中添加一个按钮?

sys*_*out 14

查看此Android按钮教程; 这个简单的例子创建一个关闭按钮.

你需要做的就是:

1.将Button小部件添加到您的布局

<Button android:id="@+id/close"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:text="@string/title_close" />
Run Code Online (Sandbox Code Playgroud)

2.将setOnClickListener方法附加到按钮实例:

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();
    }
  });
}
Run Code Online (Sandbox Code Playgroud)


Ani*_*tel 6

动态:

Button btn= new Button(this);  
btn.settext("Submit");  
btn.setOnClickListener(new View.OnClickListener()   
{
    public void onClick(View view) 
     {
           //your write code
       }
});
Run Code Online (Sandbox Code Playgroud)