如何在android中添加按钮?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2681640/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to add a button in android?
提问by vijay
Can anybody tell how to add a button in android?
谁能告诉如何在android中添加按钮?
回答by systempuntoout
Check this Android Buttontutorial; this simple example creates a Close Button.
检查此Android 按钮教程;这个简单的例子创建了一个关闭按钮。
All you need to do is:
您需要做的就是:
1.Add Button widget to your Layout
1.将按钮小部件添加到您的布局
<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:
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();
}
});
}
回答by AnilPatel
Dynamic:
动态的:
Button btn= new Button(this);
btn.settext("Submit");
btn.setOnClickListener(new View.OnClickListener()
{
public void onClick(View view)
{
//your write code
}
});
回答by Ahmad
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
不添加按钮。它声明并初始化一个 Button 实例,该实例指的是当前膨胀的 xml 中的一个 Button,其 ID 为 button1
So in your xml you would have somewhere
所以在你的 xml 你会有某个地方
<Button
android:id="@+id/button1"
<!-- other properties -->
/>
You can add a Buttonprogrammatically 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
但是在 xml 中通常更容易做,因为在这里您必须以编程方式为其提供参数、属性,并将其添加到膨胀的布局中
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
就 onClick() 而言,它取决于您认为在您的情况下最简单和最好的方法。我喜欢经常在 xml 中声明它,但您可以通过多种方式来实现。使用这种方法,你只需要确保你有一个这样的函数,它是公共的,只接受一个参数,并且该参数必须是一个视图
public void clickEvent(View v)
{
// code here
}
I also changed the name so your xml would be like
我还更改了名称,因此您的 xml 会像
<Button
android:id="@+id/button1"
<!-- other properties -->
android:onClick="clickEvent"/>
You also can set onClick() in your Java with something like
你也可以在你的 Java 中设置 onClick() 类似
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
请注意,您需要在 Activity 声明中添加实现 OnClickListener 的最后一种方式
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.
您可以将其用于多个按钮并打开 id 或检查视图参数以了解单击了哪个按钮或为不同的按钮创建单独的侦听器。
回答by Rehan Sarwar
According to official documentation of Buttonsprovided by Android.
You can first create Button in your .xml
file.
根据Android提供的按钮的官方文档。您可以先在.xml
文件中创建 Button 。
Button.xml
按钮.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 Class 投射您的按钮并设置 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
有关更多详细信息,您可以访问此链接