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;
    	}
    
    }
    


  • 相关阅读:
    这些简单优化能让你的Win10流畅很多
    win7系统登录界面背景怎么修改?
    如何在win7下通过easyBCD引导安装Ubuntu14.04
    为什么我的电脑打不开便签?
    打开Word为什么会出现感叹号呢???
    图像变换原理
    运行
    php、前端开发(网站建设)环境搭建
    zend studio面板功能
    zend studio汉化
  • 原文地址:https://www.cnblogs.com/mthoutai/p/6847022.html
Copyright © 2011-2022 走看看