1500字范文,内容丰富有趣,写作好帮手!
1500字范文 > Android开发 第2课 控件TextView Plain Text ImageView Button ImageButton以及点击事件

Android开发 第2课 控件TextView Plain Text ImageView Button ImageButton以及点击事件

时间:2019-03-23 16:22:59

相关推荐

Android开发  第2课 控件TextView Plain Text ImageView  Button ImageButton以及点击事件

控件 TextView 显示文本

Plain Text 输入文本

//将布局xml文件引入到activity当中setContentView(R.layout.activity_main);<!-- wrap_content:包裹实际文本内容(内容有多少我的长宽高就多少)match_parent:当前控件铺满父类容器 ———— 2.3api之后添加的一个属性值fill_parent:当前控件铺满父类容器 ————— 2.3api之前的一个属性值android:textSize="28sp" 文本字体的大小android:textColor="#eeeeee" 文本字体的颜色--><TextView android:id="@+id/textView"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="姓名:"android:textSize="28sp"android:textColor="#eeeeee"tools:layout_editor_absoluteX="57dp"tools:layout_editor_absoluteY="32dp" /><!--android:hint="请输入你的姓名" 只是文本的提示android:text="Name" 文本控件的真正显示--><EditText android:hint="请输入你的姓名"android:textSize="20sp"android:id="@+id/editText"android:layout_width="266dp"android:layout_height="43dp"tools:layout_editor_absoluteY="99dp"tools:layout_editor_absoluteX="59dp" />

针对虚拟机控件集中在左上角:

是布局设置的不对,如LinearLayout

3ImageView

显示图片控件

android:src = ”’@drawable /ic_launcher’

应用 drawable 的图片内容

android:background = ”’@drawable /ic_launcher’

应用 drawable 的图片当背景

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="/apk/res/android"xmlns:app="/apk/res-auto"xmlns:tools="/tools"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"tools:context="com.adb.li806.myapplication.MainActivity"><ImageView android:id="@+id/imageView"android:layout_width="match_parent"android:layout_height="wrap_content"android:src="@mipmap/ic_launcher"android:background="#f0f0f0"/><ImageView android:id="@+id/imageView2"android:layout_width="match_parent"android:layout_height="wrap_content"android:background="@mipmap/ic_launcher" /><ImageView android:id="@+id/imageView3"android:layout_width="match_parent"android:layout_height="20dp"android:background="#f0f0f0" /></LinearLayout>

不同分辨率图片设置

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"><ImageView android:id="@+id/imageView"android:layout_width="match_parent"android:layout_height="wrap_content"android:src="@mipmap/ic_launcher" /><!--无论高分辨率还是低分辨率根据以上代码ImageView设备自动寻找适应的图片图标的名称都一样只不过分辨率不一样--></LinearLayout>

4 Button 按钮

特征:

1.作为一个按钮产生点击事件

2.Button有text属性,ImageButton没有5 ImageButton 图片按钮

特征:

1.作为一个按钮产生点击事件

2.ImageButton由src属性,Button没有(src属性就是引用图片的属性)

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="/apk/res/android"xmlns:app="/apk/res-auto"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"android:weightSum="1"><Button android:id="@+id/button2"android:layout_width="match_parent"android:layout_height="wrap_content"android:text="登陆" /><Button android:id="@+id/button2"android:layout_width="match_parent"android:layout_height="wrap_content"android:text="@string/Button_name" /><!--@string 访问string下面的资源res下面的所有资源都会在gen更目录下的R.java文件中生成对应的资源id(唯一标识的id)--><ImageButton android:id="@+id/imageButton"android:layout_width="match_parent"android:layout_height="77dp"android:layout_weight="0.13"android:background="@drawable/doraemon1"android:src="@mipmap/ic_launcher" /></LinearLayout>

Button 可以设置文本内容的按钮

ImageButton 不可以设置文本内容,background以及src添加一个image,当前图片上可以做一个有文本的图片(何苦内“—”)

设置background图片会填充整个ImageButton,设置src图片会自适应

以上文件都是在修改activity_main.xml文件

5 Button和ImageButton的监听事件

1.onClick事件

(1)通过.setOnClickListener(OnClickListeber)方法添加点击事件

(2)所有的控件都有onClick事件

(3)通过点击事件的监听可以实现点击按钮之后要发生什么动作

监听事件实现的几种写法

1.匿名内部类的实现

2.独立类的实现

3.实现接口的方式来实现

package com.adb.li806.myapplication;import android.media.Image;import android.provider.Settings;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.util.Log;import android.view.View;import android.widget.Button;import android.widget.ImageButton;import android.widget.Toast;public class MainActivity extends AppCompatActivity implements View.OnClickListener{private Button loginButton;private Button bt2;private ImageButton ibt1;private Button bt3;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);//将布局xml文件引入到activity当中setContentView(R.layout.activity_main);/** 1.初始化当前所需要控件,如何初始化一个控件?* findViewById 返回的是一个View的对象,* View是所有控件的父类* findViewById是如何查找到对应view的id? R.java文件生成控件唯一的id** 2.设置Button的监听器,通过监听器实现我们点击Button要操作的事情**/loginButton = (Button) findViewById(R.id.button1);//(1)监听事件通过第一种方式实现(匿名内部类)//该方法需要一个OnClickListener对象loginButton.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {//在当前onClick方法中监听点击Button的动作System.out.println("我的Button被点击了");}});//(2)外部类监听点击事件(在开发中比较少见)bt2 = (Button) findViewById(R.id.button2);ibt1 = (ImageButton) findViewById(R.id.imageButton1);bt2.setOnClickListener(new MyOnClickListener(){//手动实现onClick方法@Overridepublic void onClick(View v) {super.onClick(v);//调用父类(MyOnClickListener)的onClick事件Toast.makeText(MainActivity.this,"bt2要执行的逻辑", Toast.LENGTH_SHORT ).show();//MainActivity.this 指明调用的是谁//Toast 是一个 View 视图,消息框,主要用于 一些帮助 / 提示。// 第一个参数:当前的上下文环境。可用getApplicationContext()或this// 第二个参数:要显示的字符串。也可是R.string中字符串ID// 第三个参数:显示的时间长短。Toast默认的有两个LENGTH_LONG(长)和LENGTH_SHORT(短),也可以使用毫秒如2000ms}});ibt1.setOnClickListener(new MyOnClickListener(){@Overridepublic void onClick(View v) {super.onClick(v);Toast toast=Toast.makeText(getApplicationContext(), "ibt1要执行的逻辑", Toast.LENGTH_SHORT);//显示toast信息toast.show();}});//(3)通过实现一个接口的方式实现监听事件bt3 = (Button) findViewById(R.id.button3);bt3.setOnClickListener(this);}//接口方式监听按钮点击事件@Overridepublic void onClick(View v) {Log.i("tag","第三种方式实现");//"tag":可以理解成标志,查看日志时快速找到自己设置的标志,以便查看text的内容,tag也恰恰是标志的意思}}/*外部类* OnClickListener是一个接口所以不能用继承 extends* 错误示范:class MyOnClickListener extends View.OnClickListener*外部类可以在com.adb.li806.myapplication包下重新写一个class*** 好处:多个按钮要输出同样一句话。(不仅要干自己的事情还要做同样的事情)* 也就是通过外部类的书写减少代码的冗余。**/class MyOnClickListener implements View.OnClickListener{@Overridepublic void onClick(View v) {Log.i("tag","父类的onClick事件");/** 1、Log.v 的调试颜色为黑色的,任何消息都会输出,这里的v代表verbose啰嗦的意思,平时使用就是Log.v("","");* 2、Log.d的输出颜色是蓝色的,仅输出debug调试的意思,但他会输出上层的信息,过滤起来可以通过DDMS的Logcat标签来选择.* 3、Log.i的输出为绿色,一般提示性的消息information,它不会输出Log.v和Log.d的信息,但会显示i、w和e的信息* 4、Log.w的意思为橙色,可以看作为warning警告,一般需要我们注意优化Android代码,同时选择它后还会输出Log.e的信息。* 5、Log.e为红色,可以想到error错误,这里仅显示红色的错误信息,这些错误就需要我们认真的分析,查看栈的信息了* *///让所有使用当前外部类的点击事件的按钮都要做出一个动作,改变button本身的透明 alpha 是0到1 0是不显式 1是显示v.getBackground().setAlpha(100);//透明度0~255透明度值 ,值越小越透明//错误:v.setAlpha(0.5f); // f 代表float类型 v点击按键的对象}}

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="/apk/res/android"xmlns:app="/apk/res-auto"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"android:weightSum="1"><Button android:id="@+id/button1"android:layout_width="match_parent"android:layout_height="wrap_content"android:text="登陆"android:layout_weight="0.09" /><Button android:id="@+id/button2"android:layout_width="match_parent"android:layout_height="wrap_content"android:text="@string/Button_name"android:layout_weight="0.13" /><!--@string 访问string下面的资源res下面的所有资源都会在gen更目录下的R.java文件中生成对应的资源id(唯一标识的id)--><ImageButton android:id="@+id/imageButton1"android:layout_width="match_parent"android:layout_height="77dp"android:layout_weight="0.13"android:background="@drawable/doraemon1"android:src="@mipmap/ic_launcher" /><Button android:id="@+id/button3"android:layout_width="match_parent"android:layout_height="wrap_content"android:text="第三人称"android:layout_weight="0.10" /></LinearLayout>

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。