zoukankan      html  css  js  c++  java
  • Android笔记(十四) Android中的基本组件——按钮

             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>

               运行结果:

    
    

             

  • 相关阅读:
    基于 WebGL 的 HTML5 楼宇自控 3D 可视化监控
    基于 HTML5 的 WebGL 楼宇自控 3D 可视化监控
    基于 WebGL 3D 的 HTML5 档案馆可视化管理系统
    基于 HTML5 的 WebGL 3D 档案馆可视化管理系统
    基于 HTML5 的 WebGL 和 VR 技术的 3D 机房数据中心可视化
    代码管理(四)SVN和Git对比
    代码管理(二)sourcetree 安装与使用
    代码管理(一)git
    iOS11新特性之LargeTitle
    使用Cordova搭建Andoid和iOS开发环境
  • 原文地址:https://www.cnblogs.com/xs104/p/4733475.html
Copyright © 2011-2022 走看看