蜗牛—Android基础之button监听器

满堂花醉三千客,一剑霜寒十四洲。这篇文章主要讲述蜗牛—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; }}



【蜗牛—Android基础之button监听器】




    推荐阅读