你好,游客 登录 注册 搜索
背景:
阅读新闻

Android 应用开发笔记 - 按钮(Button, ImageButton)

[日期:2012-09-15] 来源:Linux社区  作者:xiaobin_hlj80 [字体:大 中 小]
我们在讲Toast时,已经使用了Button。首先,在面板拖拽图标,然后更改相关属性(Properties),如下图:

activity_main.xml清单如下:

    <Button

       Android:id="@+id/btnOk"

       android:layout_width="wrap_content"

       android:layout_height="wrap_content"

       android:layout_alignLeft="@+id/textView1"

       android:layout_below="@+id/textView1"

       android:layout_marginLeft="52dp"

       android:layout_marginTop="76dp"

       android:text="@string/btn1Caption"

        android:textSize="@dimen/font_size20" />

然后,我们开始编码。

其相关部分的代码如下:

Button btnOk = (Button) findViewById(R.id.btnOk);

btnOk.setOnClickListener(this);

效果如下图:

 


4.2.6 带图标的按钮(ImageButton)

在面板拖拽图标,然后更改相关属性(Properties),如下图:


activity_main.xml清单如下:

    <ImageButton

       android:id="@+id/imgBtn1"

       android:layout_width="wrap_content"

       android:layout_height="wrap_content"

       android:layout_alignLeft="@+id/chkBox2"

       android:layout_alignTop="@+id/spin01"

       android:contentDescription="@string/btn1Caption"

        android:onClick="@string/hello_world"

        android:src="@drawable/ic_launcher" />

然后,我们开始编码。

其相关部分的代码如下: 

        // ImageButton

        ImageButton imgBtn1 = (ImageButton)findViewById(R.id.imgBtn1);

       

        imgBtn1.setOnClickListener(newButton.OnClickListener() {

 

           @Override

           public void onClick(View arg0) {

              // TODO Auto-generatedmethod stub

              setToastString("Hello China").show();

           }

       

       }); 

效果如下图:

linux
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款