鱼C论坛

 找回密码
 立即注册
查看: 3201|回复: 0

[学习笔记] java android编程3.2.1 3.2.2

[复制链接]
发表于 2017-9-23 08:45:34 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
把原来的项目关闭,再新建个项目。把activity_main.xml文件修改为:
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3.     android:layout_width="match_parent"
  4.     android:layout_height="match_parent"
  5.     android:orientation="vertical"
  6.     >

  7.     <TextView
  8.         android:id="@+id/text"
  9.         android:layout_width="match_parent"
  10.         android:layout_height="wrap_content"
  11.         android:text="This is TextView"
  12.         android:gravity="center"
  13.         android:textSize="24sp"
  14.         android:textColor="#00ff00"
  15.         android:textStyle="bold"
  16.         />
  17.     <Button
  18.         android:id="@+id/button"
  19.         android:layout_width="wrap_content"
  20.         android:layout_height="wrap_content"
  21.         android:layout_gravity="center"
  22.         android:background="#00ff00"
  23.         android:layout_marginTop="50dp"
  24.         android:text="button"
  25.         />
  26. </LinearLayout>
复制代码

控件的属性很容易理解,我就不详细介绍了。然后我们再学一种按钮使用的方法,把mainactivity.java文件修改为:
  1. package com.example.xinwei.viewcontroller;

  2. import android.content.DialogInterface;
  3. import android.graphics.Color;
  4. import android.support.v7.app.AppCompatActivity;
  5. import android.os.Bundle;
  6. import android.view.View;
  7. import android.widget.Button;
  8. import android.widget.TextView;

  9. public class MainActivity extends AppCompatActivity implements View.OnClickListener{
  10.     private TextView myText;
  11.     @Override
  12.     protected void onCreate(Bundle savedInstanceState) {
  13.         super.onCreate(savedInstanceState);
  14.         setContentView(R.layout.activity_main);
  15.         Button button = (Button)findViewById(R.id.button);
  16.         myText=(TextView)findViewById(R.id.text);
  17.         button.setOnClickListener(this);
  18.     }

  19.     @Override
  20.     public void onClick(View view) {
  21.         myText.setTextColor(Color.parseColor("red"));
  22.     }
  23. }
复制代码

这就是直接在我们的主类里实现监听事件接口,来操纵按钮,效果图为:
jdfw.gif

本帖被以下淘专辑推荐:

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-4-26 18:12

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表