源码教程 2025年06月7日
0 收藏 0 点赞 778 浏览 1871 个字
摘要 :

Android开发中按钮(Button)应该是用户交互中使用最多的组件,在很多应用程序中都很常见。当用户单击按钮的时候,会有相对应的响应动作。下面在 WidgetDemo 工程的主界……

Android开发中按钮(Button)应该是用户交互中使用最多的组件,在很多应用程序中都很常见。当用户单击按钮的时候,会有相对应的响应动作。

下面在 WidgetDemo 工程的主界面 main.xml 中放置一个名为 Button 的按钮。文件代码如下:

  1. <?xml version="1.0" encoding="utf-8"?>

  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

  3. android:layout_width="fill_parent"

  4. android:layout_height="fill_parent"

  5. android:orientation="vertical">

  6. <TextView

  7. android:layout_width="fill_parent"

  8. android:layout_height="wrap_content"

  9. android:text="@string/hello" />

  10. <Button

  11. android:id="@+id/button1"

  12. android:layout_width="wrap_content"

  13. android:layout_height="wrap_content"

  14. android:text="Button" />

  15. </LinearLayout>

其中:

  1. <Button

  2. android:id="@+id/button1"

  3. android:layout_width="wrap_content"

  4. android:layout_height="wrap_content"

  5. android:text="Button" />

表明在用户界面上放置了一个 ID 为“button1”的按钮,按钮的高度(layout_height)和宽度(layout_width)都会根据实际内容调整(wrap_content),按钮上显示文字为 Button,其运行效果如图 1 所示。

Android控件Button使用  Android按钮点击事件

图 1  Button 的应用界面

按钮最重要的用户交互事件是“单击”事件。下面为 Button1 添加事件监听器和相应的单击事件。该过程在 WidgetDemoActivity.java 文件中完成,代码如下:

  1. package introduction.android.widgetdemo;

  2. import android.support.v7.app.AppCompatActivity;

  3. import android.os.Bundle;

  4. import android.util.Log;

  5. import android.view.View;

  6. import android.widget.Button;

  7. public class MainActivity extends AppCompatActivity {

  8.     @Override

  9.     protected void onCreate(Bundle savedInstanceState) {

  10.         super.onCreate(savedInstanceState);

  11.         setContentView(R.layout.activity_main);

  12.         Button btn = (Button) this.findViewById(R.id.button1);

  13.         btn.setOnClickListener(new View.OnClickListener() {

  14.             @Override

  15.             public void onClick(View v) {

  16.                 //TODO Auto-generated method stub

  17.                 setTitle("button1 被用户点击了");

  18.                 Log.i("widgetDemo", "button1 被用户点击了。");

  19.             }

  20.         });

  21.     }

  22. }

在 WidgetDemoActivity 的 onCreate() 方法中,通过 findViewById(R.id.button1) 方法获得 Button1 的对象,通过 setOnClickListener() 方法为 Button1 设置监听器。

此处新建了一个实现 OnClickListener 接口的匿名类作为监听器,并实现了 onClick() 方法。当 Button1 被点击时,当前应用程序的标题被设置成“button1 被用户点击了”运行结果如图 2 所示。

Android控件Button使用  Android按钮点击事件

图 2  点击按钮运行效果

微信扫一扫

支付宝扫一扫

版权: 转载请注明出处:https://www.zuozi.net/936.html

管理员

相关推荐
2025-07-05

对于一些使用WordPress进行外贸建站的商家来说,大部分人会通过在WordPress中添加JS代码和CSS样式表…

701
2025-07-05

当商家遇到WordPress独立站改版或者域名到期等问题时,不免会涉及到WordPress域名的更改,那么这一…

715
2025-07-05

用户在使用WooCommerce进行跨境电商独立站搭建工作时,有可能会借助WooCommerce短代码实现更加醒目…

307
2025-07-05

随着外贸建站行业的不断深入发展,WordPress的多语言功能也显得越来越重要,一个具有多语言的独立站…

1,039
2025-07-05

WooCommerce作为WordPress外贸建站生态的一部分,WooCommerce运费设置是商家在建站过程中不可忽视的…

835
2025-07-05

在外贸建站的过程中,有些商家会选择使用WordPress幻灯片为网站页面增加一定的动感和可观性,进而提…

723
发表评论
暂无评论

还没有评论呢,快来抢沙发~

助力内容变现

将您的收入提升到一个新的水平

点击联系客服

在线时间:08:00-23:00

客服QQ

122325244

客服电话

400-888-8888

客服邮箱

122325244@qq.com

扫描二维码

关注微信客服号