android常用控件的使用方法
引言
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常用控件的使用方法的更多相关文章
- C# 常用控件属性及方法介绍
C#常用控件属性及方法介绍 目录 1.窗体(Form) 2.Label (标签)控件 3.TextBox ...
- 【转载】C#常用控件属性及方法介绍
C#常用控件属性及方法介绍 目录 1.窗体(Form) 2.Label (标签)控件 3.TextBox(文 ...
- Android常用控件及对应Robotium API
最近发现Android控件不熟悉,看Robotium的API都费劲. 常用Android控件: 控件类型 描述 相关类 Button 按钮,可以被用户按下或点击,以执行⼀个动作 Button Text ...
- winFrom 常用控件属性及方法介绍
目录 1.窗体(Form) 2.Label (标签)控件 3.TextBox(文本框)控件 4.RichTextBox控件 5.NumericUpDown控件 6.Button(按钮)控件 7.Gro ...
- android 测量控件视图的方法
在实际项目中经常要用到 测量一个控件或者视图的高,宽.然后根据这个高宽进行一些逻辑. 计算视图宽高有几种方式先简单的了解下android 视图的绘制过程会促进理解. 一.android View绘制过 ...
- Android常用控件之GridView使用BaseAdapter
我们可以为GridView添加自定义的Adapter,首先看下用自定义Adapter的显示效果 在布局文件main.xml文件中定义一个GridView控件 <RelativeLayout xm ...
- 常用的基本控件 android常用控件
1.TextView:(文本框):不能编辑 android:textColor="@color/tv_show_color" 字体颜色 android:textSize ...
- 家庭版记账本app之常用控件的使用方法
现在先介绍在android开发的时候会用的相关的控件,做一个基本的了解方便我们之后对其进行相关具体的操作.下面是相应额详细情况: TextView android:layout_width 和 and ...
- Android常用控件
Android 中使用各种控件(View) DatePicker - 日期选择控件 TimePicker - 时间选择控件 ToggleButton - 双状态按钮控件 EditText - 可编辑 ...
随机推荐
- ASP.NET-HTML.Helper常用方法
Html.ActionLink方法 Html.ActionLink("linkText","actionName") Html.ActionLink(" ...
- Eureka Server添加用户认证
Eureka Server添加用户认证 学习了:http://blog.csdn.net/liuchuanhong1/article/details/54729556 注意:1,需要使用 defaul ...
- ffmpeg在android上输出滑屏问题处理
ffmpeg部分机器上有花屏的问题 原代码例如以下: while(av_read_frame(formatCtx, &packet)>=0 && !_stop & ...
- 实战c++中的vector系列--再谈vector的insert()方法(都是make_move_iterator惹的祸)
之前说过了关于vector的insert()方法,把vector B的元素插入到vector A中.vector A中的结果我们可想而知,可是vector B中的元素还会怎样? 看看之前写过的程序: ...
- vue 路由demo2
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- android 极细线
最后找到一个还算好用的方法:伪类 + transform 原理是把原先元素的 border 去掉,然后利用:before或者:after重做 border ,并 transform 的 scale 缩 ...
- php+mysql时报错:Unknown column '' in 'field list'解决方案
答案来源:http://jingyan.baidu.com/article/9f7e7ec05c5ad76f281554ab.html 很多人在用php+MySQL做网站往数据库插入数据时发现如下错误 ...
- NOIP2017提高组 模拟赛15(总结)
NOIP2017提高组 模拟赛15(总结) 第一题 讨厌整除的小明 [题目描述] 小明作为一个数学迷,总会出于数字的一些性质喜欢上某个数字,然而当他喜欢数字k的时候,却十分讨厌那些能够整除k而比k小的 ...
- caffe中LetNet-5卷积神经网络模型文件lenet.prototxt理解
caffe在 .\examples\mnist文件夹下有一个 lenet.prototxt文件,这个文件定义了一个广义的LetNet-5模型,对这个模型文件逐段分解一下. name: "Le ...
- 那些年尝试过的效率工具之Total Commander
昨天电脑文件很乱,想整理一下发现移动.复制文件要来回目录切换很麻烦,突然就又想起了用Total Commander——简称TC,很久之前尝试过但没坚持使用的工具. 借此机会总结一下自己对TC的认识,后 ...