<p>Button基本使用方法</p><p> 首先,添加Button控件到XML布局文件中。也可通过程序添加。</p><p> 在布局文件中设置按钮的一些属性,如位置,宽高,按钮上的字,颜色等。</p><p> 比较重要的是要给按钮一个id号,这是按钮唯一的名字。</p><p> 这样在程序中可以通过如下形式获得按钮:</p><p> button = (Button)findViewById(R.id.buttonId);</p><p></p><p>处理按钮点击</p><p> 按钮点击有两种处理方法。</p><p> 第一种是通过onClick属性,通过这个属性设置处理点击事件的方法名,在Activity中实现这个方法。</p><p> 另一种方法是典型的事件监听机制的应用形式,下面详细说明这两种方法。</p><p></p><p>1.通过onClick属性设置处理方法</p><p> 在XML布局文件中设置Button的属性:</p><p> android:onClick="yourMethodName"</p><p> 然后在该布局文件对应的Acitivity中实现该方法:</p><p></p><p> /** Called when the user touches the button */</p><p> public void yourMethodName(View view)</p><p> {</p><p> // Do something in response to button click</p><p> }</p><p></p><p> 需要注意的是这个方法必须符合三个条件:</p><p> 1.public</p><p> 2.返回void</p><p> 3.只有一个参数View,这个View就是被点击的这个控件。</p><p></p><p>2.使用setOnClickListener添加监听器对象</p><p> 关于事件监听模式,参见</p><p> http://www.cnblogs.com/mengdd/archive/2012/09/08/2676587.html</p><p></p><p> 可以写一个内部类,实现OnClickListener接口,在这个类中实现onClick方法,方法里面写在按钮点击时想做的具体工作。</p><p> 将这个内部类的对象传入按钮的setOnClickListener方法中,即完成监听器对象和按钮的绑定(在事件源Button上注册了事件监听器),这时候只要按钮被点击,那么监听器对象的onClick方法就会被调用。</p><p> 当然这里也不一定要自己写一个内部类出来,比如这个例子:</p><p></p><p> Button button = (Button) findViewById(R.id.button_send);</p><p> button.setOnClickListener(new View.OnClickListener() {</p><p> public void onClick(View v)</p><p> {</p><p> // Do something in response to button click</p><p> }</p><p> });</p><p></p><p></p><p>按钮基本操作实例</p><p> 奉上实例一个:</p><p> XML布局文件</p><p><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"</p><p> xmlns:tools="http://schemas.android.com/tools"</p><p> android:layout_width="match_parent"</p><p> android:layout_height="match_parent" ></p><p> <TextView</p><p> android:layout_width="wrap_content"</p><p> android:layout_height="wrap_content"</p><p> android:layout_centerHorizontal="true"</p><p> android:layout_centerVertical="true"</p><p> android:padding="@dimen/padding_medium"</p><p> android:text="@string/hello_world"</p><p> tools:context=".ButtonActivity" /></p><p> <Button</p><p> android:id="@+id/button_first"</p><p> android:layout_height="wrap_content"</p><p> android:layout_width="wrap_content"</p><p> android:text="@string/buttonText"</p><p> android:onClick="changeButtonColor"</p><p> > </p><p> </Button> </p><p> </p><p> <Button</p><p> android:id="@+id/button_second"</p><p> android:layout_height="wrap_content"</p><p> android:layout_width="wrap_content"</p><p> android:text="@string/buttonText2"</p><p> android:layout_below="@id/button_first"</p><p> </p><p> > </p><p> </Button></p><p></RelativeLayout></p><p></p><p> Activity代码</p><p>package com.example.buttontest;</p><p>import android.app.Activity;</p><p>import android.os.Bundle;</p><p>import android.view.Menu;</p><p>import android.view.View;</p><p>import android.view.View.OnClickListener;</p><p>import android.widget.Button;</p><p>public class ButtonActivity extends Activity</p><p>{</p><p> private Button button01 = null;</p><p> private Button button02 = null;</p><p> @Override</p><p> public void onCreate(Bundle savedInstanceState)</p><p> {</p><p> super.onCreate(savedInstanceState);</p><p> setContentView(R.layout.activity_button);</p><p> </p><p> button01 = (Button)findViewById(R.id.button_first);</p><p> button02 = (Button)findViewById(R.id.button_second);</p><p> </p><p> //绑定事件源和监听器对象</p><p> button02.setOnClickListener(new MyButtonListener());</p><p> }</p><p> @Override</p><p> public boolean onCreateOptionsMenu(Menu menu)</p><p> {</p><p> getMenuInflater().inflate(R.menu.activity_button, menu);</p><p> return true;</p><p> }</p><p> </p><p> //按钮1的点击事件</p><p> public void changeButtonColor(View view)</p><p> {</p><p> button01.setBackgroundColor(getResources().getColor(R.color.red));</p><p> </p><p> }</p><p> </p><p> //内部类,实现OnClickListener接口</p><p> //作为第二个按钮的监听器类</p><p> class MyButtonListener implements OnClickListener</p><p> {</p><p> public void onClick(View v)</p><p> {</p><p> button02.setBackgroundColor(getResources().getColor(R.color.blue));</p><p> </p><p> }</p><p> </p><p> </p><p> }</p><p> </p><p>}</p><p></p><p> 运行截图 </p><p> 点击前:</p><p><img src="/up_pic/201812/120428315258.png" title="120428315258.png" alt="1.png"/><br/></p><p> 点击后:</p><p><img src="/up_pic/201812/120428399080.png" title="120428399080.png" alt="2.png"/></p><p></p><p> 相关的资源文件中内容</p><p> strings.xml</p><p><resources></p><p> <string name="app_name">ButtonTest</string></p><p> <string name="hello_world">Hello world!</string></p><p> <string name="menu_settings">Settings</string></p><p> <string name="title_activity_button">ButtonActivity</string></p><p> <string name="buttonText">ThisIsAButton</string></p><p> <string name="buttonText2">ThisIsAnotherButton</string></p><p></resources></p><p></p><p> colors.xml</p><p><?xml version="1.0" encoding="UTF-8"?></p><p><resources></p><p> <color name="red">#f00</color></p><p> <color name="green">#0f0</color></p><p> <color name="blue">#00f</color></p><p> <color name="black">#000</color></p><p></resources></p>