Android基础控件之Button的基本使用

<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=&quot;yourMethodName&quot;</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>&lt;RelativeLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;</p><p> xmlns:tools=&quot;http://schemas.android.com/tools&quot;</p><p> android:layout_width=&quot;match_parent&quot;</p><p> android:layout_height=&quot;match_parent&quot; &gt;</p><p> &lt;TextView</p><p> android:layout_width=&quot;wrap_content&quot;</p><p> android:layout_height=&quot;wrap_content&quot;</p><p> android:layout_centerHorizontal=&quot;true&quot;</p><p> android:layout_centerVertical=&quot;true&quot;</p><p> android:padding=&quot;@dimen/padding_medium&quot;</p><p> android:text=&quot;@string/hello_world&quot;</p><p> tools:context=&quot;.ButtonActivity&quot; /&gt;</p><p> &lt;Button</p><p> android:id=&quot;@+id/button_first&quot;</p><p> android:layout_height=&quot;wrap_content&quot;</p><p> android:layout_width=&quot;wrap_content&quot;</p><p> android:text=&quot;@string/buttonText&quot;</p><p> android:onClick=&quot;changeButtonColor&quot;</p><p> &gt; </p><p> &lt;/Button&gt; </p><p> </p><p> &lt;Button</p><p> android:id=&quot;@+id/button_second&quot;</p><p> android:layout_height=&quot;wrap_content&quot;</p><p> android:layout_width=&quot;wrap_content&quot;</p><p> android:text=&quot;@string/buttonText2&quot;</p><p> android:layout_below=&quot;@id/button_first&quot;</p><p> </p><p> &gt; </p><p> &lt;/Button&gt;</p><p>&lt;/RelativeLayout&gt;</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>&lt;resources&gt;</p><p> &lt;string name=&quot;app_name&quot;&gt;ButtonTest&lt;/string&gt;</p><p> &lt;string name=&quot;hello_world&quot;&gt;Hello world!&lt;/string&gt;</p><p> &lt;string name=&quot;menu_settings&quot;&gt;Settings&lt;/string&gt;</p><p> &lt;string name=&quot;title_activity_button&quot;&gt;ButtonActivity&lt;/string&gt;</p><p> &lt;string name=&quot;buttonText&quot;&gt;ThisIsAButton&lt;/string&gt;</p><p> &lt;string name=&quot;buttonText2&quot;&gt;ThisIsAnotherButton&lt;/string&gt;</p><p>&lt;/resources&gt;</p><p></p><p>  colors.xml</p><p>&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;</p><p>&lt;resources&gt;</p><p> &lt;color name=&quot;red&quot;&gt;#f00&lt;/color&gt;</p><p> &lt;color name=&quot;green&quot;&gt;#0f0&lt;/color&gt;</p><p> &lt;color name=&quot;blue&quot;&gt;#00f&lt;/color&gt;</p><p> &lt;color name=&quot;black&quot;&gt;#000&lt;/color&gt;</p><p>&lt;/resources&gt;</p>
RangeTime:0.006653s
RangeMem:211.58 KB
返回顶部 留言