<?xml version="1.0" encoding="utf-8"?><GridLayout 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="com.hanqi.testapp3.MainActivity" android:rowCount="5" android:columnCount="4" > <TextView android:layout_width="match_parent" android:layout_height="50dp" android:layout_columnSpan="4" android:id="@+id/tv_1" android:text="7" android:gravity="right|center_vertical" android:textSize="30sp" android:paddingRight="10dp" android:textColor="#0f0"/> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center" android:text="7" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center" android:text="8" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center" android:text="9" android:id="@+id/bt9"/></GridLayout>
package com.hanqi.testapp3; import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.view.View;import android.widget.Button;import android.widget.TextView; public class MainActivity extends AppCompatActivity { TextView tv_1; Button bt9; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); tv_1=(TextView)findViewById(R.id.tv_1); //事件源 bt9=(Button)findViewById(R.id.bt9); //给bt9按钮添加监听器 实现方式:1.匿名内部类 bt9.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { //处理事件的业务逻辑 tv_1.setText("9"); } }); //内部类 } public void bt7_OnClick(View v) { tv_1.setText("7"); } public void bt8_OnClick(View v) { tv_1.setText("8"); } public void bt9_OnClick(View v) { tv_1.setText("9"); }}
时间: 2024-10-10 22:56:05