码迷,mamicode.com
首页 > 移动开发 > 详细

Android笔记(十四) Android中的基本组件——按钮

时间:2015-08-16 01:58:34      阅读:163      评论:0      收藏:0      [点我收藏+]

标签:

         Android中的按钮主要包括Button和ImageButton两种,Button继承自TextView,而ImageButton继承自ImageView。Button生成的按钮上显示文字,而ImageButton上则显示图片。

         主要功能是在UI界面上生成一个按钮,当用户点击这个按钮时,出发一个OnClick事件来执行某项任务。

         简单示例

<TableLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    android:orientation="horizontal"
    >

    <TableRow>
        <!-- 普通文字按钮 -->
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="普通按钮"
            android:textSize="10pt"
            />
        <!-- 普通图片按钮 -->
        <ImageButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/blue"
            android:background="#000000"
            />
        <!-- 图片背景文字按钮 -->
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="图片背景文字按钮"
            android:background="@drawable/red"
            android:textSize="10pt"
            />
    </TableRow>

</TableLayout>

 

         运行结果:

技术分享

         

         一个小demo,要求界面显示一个文本一个数字文本,点击按钮,数字增加

         MainActivity.java

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

public class ButtonActivity extends Activity {

    TextView tv = null;
    String count = null;
    int num = 0;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_button);

        tv = (TextView) findViewById(R.id.count);
        count = (String) tv.getText();
        num = Integer.parseInt(count);

        findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                tv.setText((++num) + " ");
                onRestart();
            }
        });
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        Log.d("TEST",num + "");

    }
}

         activity_main.xml

<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="cn.lixyz.layoutdemo.ButtonActivity">

    <!-- 普通文字按钮 -->
    <TableRow>
        <Button
            android:id="@+id/button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/red"
            android:text="普通按钮"
            android:textSize="10pt"/>

    </TableRow>
    <TextView
        android:id="@+id/count"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="50pt"
        android:textColor="#ff211c"
        android:text="1"/>


</TableLayout>

 

           运行结果:

技术分享


         

Android笔记(十四) Android中的基本组件——按钮

标签:

原文地址:http://www.cnblogs.com/xs104/p/4733475.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!