zoukankan      html  css  js  c++  java
  • 蜗牛—Android基础之button监听器

    XML文件中有一个textView 和 一个button。

    <LinearLayout 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"
        android:orientation="vertical"
        android:paddingBottom="@dimen/activity_vertical_margin"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        tools:context=".MainActivity" >
    
        <!-- 一个id为textView的文本 宽度充满父容器 高度自适应 背景为红色 初识文字为wjj -->
    
    
        <TextView
            android:id="@+id/textView"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:background="#FF0000"
            android:text="@string/wjj" >
        </TextView>
    
        <!-- 一个id为button的button  宽度自适应 高度自适应 初识文字为button -->
    
        <Button
            android:id="@+id/button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="button" />
    
    </LinearLayout>

    Java文件

    package com.wjj.day_01_genesis;
    
    import android.app.Activity;
    import android.graphics.Color;
    import android.os.Bundle;
    import android.view.Menu;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;
    import android.widget.TextView;
    
    public class MainActivity extends Activity {
    
    	private TextView textView;
    	private Button button;
    	int count = 0;
    
    	@Override
    	protected void onCreate(Bundle savedInstanceState) {
    		super.onCreate(savedInstanceState);
    		setContentView(R.layout.activity_main); // 设置布局文件
    		textView = (TextView) findViewById(R.id.textView); // 找到文本
    		textView.setBackgroundColor(Color.BLUE); // 设置文本背景的颜色
    		button = (Button) findViewById(R.id.button); // 找到按钮
    		buttonOnClickLisnter lisnter = new buttonOnClickLisnter(); // 初识化一个监听器
    		button.setOnClickListener(lisnter); // 给按钮设置监听器
    	}
    
    	class buttonOnClickLisnter implements OnClickListener { // 实现OnClickListener接口
    
    		@Override
    		public void onClick(View view) { // 当绑定此监听器的按钮被按下时会调用此方法
    			// TODO Auto-generated method stub
    			count++;
    			textView.setText(count + ""); // 设置文本的显示
    		}
    
    	}
    
    	@Override
    	public boolean onCreateOptionsMenu(Menu menu) {
    		// Inflate the menu; this adds items to the action bar if it is present.
    		getMenuInflater().inflate(R.menu.main, menu);
    		return true;
    	}
    
    }
    


  • 相关阅读:
    direct path write 等待事件导致数据库hang
    Sql Server数据库视图的创建、修改
    MVC视图中Html.DropDownList()辅助方法的使用
    Ubuntu16.04下安装.NET Core
    Ubuntu16.04下部署golang开发环境
    win7环境下安装运行gotour【转载整理】
    一.Windows I/O模型之选择(select)模型
    Windos下的6种IO模型简要介绍
    编码介绍(ANSI、GBK、GB2312、UTF-8、GB18030和 UNICODE)
    串口通信知识点详解
  • 原文地址:https://www.cnblogs.com/mthoutai/p/6847022.html
Copyright © 2011-2022 走看看