引言

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. hdu5351

    题目名称:MZL's Border 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5351 题意:给出fib 1 = b,fib2 = a ; fib  ...

  2. kqueue演示样例

    网络server通常都使用epoll进行异步IO处理,而开发人员通常使用mac,为了方便开发.我把自己的handy库移植到了mac平台上. 移植过程中,网上竟然没有搜到kqueue的使用样例.让我吃惊 ...

  3. pip安装selenium时提示Unknown or unsupported command 'install'

    安装流程: 1.安装Python34 2.安装pip 下载setuptoos并安装,然后输入:easy_install pip 然后 配置path:C:\Python34\Scripts 3安装sel ...

  4. 玩转Android Camera开发(三):国内首发---使用GLSurfaceView预览Camera 基础拍照demo

    GLSurfaceView是OpenGL中的一个类,也是能够预览Camera的,并且在预览Camera上有其独到之处. 独到之处在哪?当使用Surfaceview无能为力.痛不欲生时就仅仅有使用GLS ...

  5. [linux]shell中,反引號(`)的应用

    反引號位 (`) 位于键盘的Tab键的上方.1键的左方.注意与单引號(')位于Enter键的左方的差别. 反引號位 (`)在Linux中起着命令替换的作用. 命令替换是指shell可以将一个命令的标准 ...

  6. 利用wget 抓取 网站网页 包括css背景图片

    利用wget 抓取 网站网页 包括css背景图片 wget是一款非常优秀的http/ftp下载工具,它功能强大,而且几乎所有的unix系统上都有.不过用它来dump比较现代的网站会有一个问题:不支持c ...

  7. [Android] Android开发优化之——从代码角度进行优化

    通常我们写程序,都是在项目计划的压力下完成的,此时完成的代码可以完成具体业务逻辑,但是性能不一定是最优化的.一般来说,优秀的程序员在写完代码之后都会不断的对代码进行重构.重构的好处有很多,其中一点,就 ...

  8. NOIP2017提高组模拟赛 7(总结)

    NOIP2017提高组模拟赛 7(总结) 第一题 斯诺克 考虑这样一个斯诺克球台,它只有四个袋口,分别在四个角上(如下图所示).我们把所有桌子边界上的整数点作为击球点(除了4个袋口),在每个击球点我们 ...

  9. ThinkPHP5.0框架开发--第1章 Tp5.0安装

    ThinkPHP5.0框架开发--第1章 Tp5.0安装 第1章 Tp5.0 安装 ======================================================== 今 ...

  10. 修改echarts环形图的牵引线及文字位置

    修改echarts环形图的牵引线及文字位置,下面代码及效果不仅如此,也包含了其它的效果哦.有问题可以留言. 根据echarts官方示例修改效果: 官方示例图: 修改效果图: 直接上代码:其它不多说. ...