引言

xml很强大

TextView

<TextView
android:id="@+id/text_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:textSize="24sp"
android:textColor="#00ff00"
android:text="This is textview"
/>

Button

语法

<!-- textAllCaps避免全部变为大写 -->
<Button
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:textSize="24sp"
android:textColor="#00ff00"
android:text="This is textview"
android:textAllCaps="false"
/>

添加点击处理

1.第一种

package com.example.diandodo.helloworld;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log; // 引入可以使用日志
import android.view.View;
import android.widget.Button; public class HelloWorldActivity extends AppCompatActivity {
private static final String TAG = "HelloWorldActivity";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.hello_world_layout);
Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.d(TAG,"Click");
}
});
}
}

2.第二种

package com.example.diandodo.helloworld;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log; // 引入可以使用日志
import android.view.View;
import android.widget.Button; public class HelloWorldActivity extends AppCompatActivity implements View.OnClickListener{
private static final String TAG = "HelloWorldActivity";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.hello_world_layout);
Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(this);
} @Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.button:
Log.d(TAG,"Click");
break;
} }
}

EditText

允许用户输入和编辑内容

<EditText
android:id="@+id/edit_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="left"
android:hint="Type something here"
/>

小案例,点击获取内容

1.布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"> <!-- textAllCaps避免全部变为大写 -->
<Button
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="点击获取"
android:textAllCaps="false"
/> <EditText
android:id="@+id/edit_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="left"
android:hint="Type something here"
android:maxLines="2"
/> </LinearLayout>

2.业务

package com.example.diandodo.helloworld;

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.EditText;
import android.widget.Toast; public class HelloWorldActivity extends AppCompatActivity implements View.OnClickListener{
private static final String TAG = "HelloWorldActivity";
private EditText editText; // 定义一个全局变量
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.hello_world_layout);
Button button = (Button) findViewById(R.id.button);
editText = (EditText) findViewById(R.id.edit_text);
button.setOnClickListener(this);
} @Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.button:
String inputText = editText.getText().toString();
Toast.makeText(HelloWorldActivity.this,inputText,Toast.LENGTH_SHORT).show();
break;
} }
}

ImageView

图片展示的控件。

图片通常放在以“drawable”开头的目录下。

<ImageView
android:id="@+id/image_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:src="@drawable/img_1"
/>

小案例,点击更改图片

1.布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"> <!-- textAllCaps避免全部变为大写 -->
<Button
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="点击获取"
android:textAllCaps="false"
/> <EditText
android:id="@+id/edit_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="left"
android:hint="Type something here"
android:maxLines="2"
/> <Button
android:id="@+id/button_change_img"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="点击改变图片"
android:textAllCaps="false"
/> <ImageView
android:id="@+id/image_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:src="@drawable/img_1"
/> </LinearLayout>

2.业务

package com.example.diandodo.helloworld;

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.EditText;
import android.widget.ImageView;
import android.widget.Toast; public class HelloWorldActivity extends AppCompatActivity implements View.OnClickListener{
private static final String TAG = "HelloWorldActivity";
private EditText editText; // 定义一个全局变量
private ImageView imageView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.hello_world_layout);
editText = (EditText) findViewById(R.id.edit_text);
imageView= (ImageView) findViewById(R.id.image_view);
Button button = (Button) findViewById(R.id.button);
Button button_change_img = (Button) findViewById(R.id.button_change_img);
button.setOnClickListener(this);
button_change_img.setOnClickListener(this);
} @Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.button:
String inputText = editText.getText().toString();
Toast.makeText(HelloWorldActivity.this,inputText,Toast.LENGTH_SHORT).show();
break;
case R.id.button_change_img:
imageView.setImageResource(R.drawable.img_2);
break;
} }
}

ProgressBar

进度条

<ProgressBar
android:id="@+id/progress_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>

小案例,点击隐藏显示进度条

1.布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"> <!-- textAllCaps避免全部变为大写 -->
<Button
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="点击获取"
android:textAllCaps="false"
/> <EditText
android:id="@+id/edit_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="left"
android:hint="Type something here"
android:maxLines="2"
/> <Button
android:id="@+id/button_change_img"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="点击改变图片"
android:textAllCaps="false"
/> <ImageView
android:id="@+id/image_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:src="@drawable/img_1"
/> <Button
android:id="@+id/button_set_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="点击显示隐藏"
android:textAllCaps="false"
/> <ProgressBar
android:id="@+id/progress_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/> </LinearLayout>

2.业务

package com.example.diandodo.helloworld;

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.EditText;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.Toast; public class HelloWorldActivity extends AppCompatActivity implements View.OnClickListener{
private static final String TAG = "HelloWorldActivity";
private EditText editText; // 定义一个全局变量
private ImageView imageView;
private ProgressBar progressBar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.hello_world_layout);
editText = (EditText) findViewById(R.id.edit_text);
imageView= (ImageView) findViewById(R.id.image_view);
progressBar = (ProgressBar) findViewById(R.id.progress_bar);
Button button = (Button) findViewById(R.id.button);
Button button_change_img = (Button) findViewById(R.id.button_change_img);
Button button_set_view = (Button) findViewById(R.id.button_set_view); button.setOnClickListener(this);
button_change_img.setOnClickListener(this);
button_set_view.setOnClickListener(this);
} @Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.button:
String inputText = editText.getText().toString();
Toast.makeText(HelloWorldActivity.this,inputText,Toast.LENGTH_SHORT).show();
break;
case R.id.button_change_img:
imageView.setImageResource(R.drawable.img_2);
break; case R.id.button_set_view:
if (progressBar.getVisibility() == View.GONE) {
progressBar.setVisibility(View.VISIBLE); // INVISIBLE
} else {
progressBar.setVisibility(View.GONE);
}
break;
} }
}

设为长的进度条

1.布局调整

<ProgressBar
android:id="@+id/progress_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:max="100"
style="?android:attr/progressBarStyleHorizontal"
/>

2.业务处理

@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.button:
String inputText = editText.getText().toString();
Toast.makeText(HelloWorldActivity.this,inputText,Toast.LENGTH_SHORT).show();
break;
case R.id.button_change_img:
imageView.setImageResource(R.drawable.img_2);
break; case R.id.button_set_view:
// if (progressBar.getVisibility() == View.GONE) {
// progressBar.setVisibility(View.VISIBLE); // INVISIBLE
// } else {
// progressBar.setVisibility(View.GONE);
// }
int progress = progressBar.getProgress();
progress = progress + 10;
progressBar.setProgress(progress);
break;
} }

这样每次点击,都会增加进度。

AlertDialog

弹出对话框,置顶于所有界面元素之上。

			AlertDialog.Builder dialog = new AlertDialog.Builder(HelloWorldActivity.this);
dialog.setTitle("这是Dialog");
dialog.setMessage("一些重要的事情");
dialog.setCancelable(false);
dialog.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog,int which) { }
}); dialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog,int which) { }
});
dialog.show();

ProgressDialog

会在对话框中显示一个进度条,一般用于表示当前操作比较耗时,让用户耐心等待。

ProgressDialog progressDialog = new ProgressDialog(HelloWorldActivity.this);
progressDialog.setTitle("这是progressDialog");
progressDialog.setMessage("Loading...");
progressDialog.setCancelable(true);
progressDialog.show();

android常用控件的使用方法的更多相关文章

  1. C# 常用控件属性及方法介绍

      C#常用控件属性及方法介绍                                               目录 1.窗体(Form) 2.Label (标签)控件 3.TextBox ...

  2. 【转载】C#常用控件属性及方法介绍

    C#常用控件属性及方法介绍                                               目录 1.窗体(Form) 2.Label (标签)控件 3.TextBox(文 ...

  3. Android常用控件及对应Robotium API

    最近发现Android控件不熟悉,看Robotium的API都费劲. 常用Android控件: 控件类型 描述 相关类 Button 按钮,可以被用户按下或点击,以执行⼀个动作 Button Text ...

  4. winFrom 常用控件属性及方法介绍

    目录 1.窗体(Form) 2.Label (标签)控件 3.TextBox(文本框)控件 4.RichTextBox控件 5.NumericUpDown控件 6.Button(按钮)控件 7.Gro ...

  5. android 测量控件视图的方法

    在实际项目中经常要用到 测量一个控件或者视图的高,宽.然后根据这个高宽进行一些逻辑. 计算视图宽高有几种方式先简单的了解下android 视图的绘制过程会促进理解. 一.android View绘制过 ...

  6. Android常用控件之GridView使用BaseAdapter

    我们可以为GridView添加自定义的Adapter,首先看下用自定义Adapter的显示效果 在布局文件main.xml文件中定义一个GridView控件 <RelativeLayout xm ...

  7. 常用的基本控件 android常用控件

    1.TextView:(文本框):不能编辑    android:textColor="@color/tv_show_color" 字体颜色    android:textSize ...

  8. 家庭版记账本app之常用控件的使用方法

    现在先介绍在android开发的时候会用的相关的控件,做一个基本的了解方便我们之后对其进行相关具体的操作.下面是相应额详细情况: TextView android:layout_width 和 and ...

  9. Android常用控件

     Android 中使用各种控件(View) DatePicker - 日期选择控件 TimePicker - 时间选择控件 ToggleButton - 双状态按钮控件 EditText - 可编辑 ...

随机推荐

  1. Eclipse配置Maven私服

    Eclipse配置Maven私服 前言: 搭建Maven私有仓库的主要目的,是为了在团队多人开发时,只要内网的私有仓库有下载过依赖的jar包,就直接从私有仓库获取,不再通过外网的中央仓库.如果私服上面 ...

  2. 菜鸟学Struts——I18N对国际化的支持

    大家肯定都喜欢玩游戏吧. 对于是一个游戏迷的话,肯定玩过不少很棒的经典单机游戏.比方说,国产的<古墓丽影>.<刺客信条>.<鬼泣>国产的仙剑.古剑等.在众多游戏系列 ...

  3. sqlite学习笔记11:C语言中使用sqlite之删除记录

    最后一节,这里记录下怎样删除数据. 前面全部的代码都继承在这里了,在Ubuntu14.04和Mac10.9上亲測通过. #include <stdio.h> #include <st ...

  4. linux /proc/cpuinfo 文件描写叙述

    processor :系统中逻辑处理核的编号.对于单核处理器.则课觉得是其CPU编号,对于多核处理器则能够是物理核.或者使用超线程技术虚拟的逻辑核 vendor_id :CPU制造商 cpu fami ...

  5. po层和vo层中po和vo是什么意思

    POJO(Plain Ordinary Java Object)简单的Java对象,实际就是普通JavaBeans,是为了避免和EJB混淆所创造的简称. pojo 是Plain Old Java Ob ...

  6. 第十七章_Web注解

    1.HandlesTypes 这个注解类型用来声明ServletContainerInitializer能够处理哪些类型的类.它有一个属性.一个值.用来声明类的类型.比如,以下的ServletCont ...

  7. 单片机: EEPROM和串口通信

    名称:IIC协议 EEPROM24c02 通过串口通信存数读取数据 内容:此程序用于检測EEPROM性能,測试方法例如以下:写入24c02一个数据,然后在内存中改变这些数据. 掉电后主内存将失去这些信 ...

  8. bzoj3262: 陌上花开(cdq分治+树状数组)

    3262: 陌上花开 题目:传送门 题解: %%%cdq分治 很强大的一个暴力...感觉比分块高级多了 这道题目就是一个十分经典的三维偏序的例题: 一维直接暴力排序x 二维用csq维护y 三维用树状数 ...

  9. Process Explorer

    https://docs.microsoft.com/en-us/sysinternals/downloads/process-explorer Ever wondered which program ...

  10. centos6.0 配置SVN

    基本步骤: 1.安装必需的subversion 2.创建版本库 3.配置用户和权限 4.钩子和svn常用命令说明 一.安装subversion 在这里我们使用yum来安装subversion,使用以下 ...