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 - 可编辑 ...
随机推荐
- JSP简单练习-上传文件
注意:在编写上传文件的代码时,需确保"WEB-INF/lib"下含有jspsmartupload.jar包.否则会出错. jspSmartupload.jar下载 <!-- ...
- 如何从 Datagrid 中获得单元格的内容与 使用值转换器进行绑定数据的转换IValueConverter
一.如何从 Datagrid 中获得单元格的内容 DataGrid 属于一种 ItemsControl, 因此,它有 Items 属性并且用ItemContainer 封装它的 items. 但是,W ...
- python+caffe训练自己的图片数据流程
1. 准备自己的图片数据 选用部分的Caltech数据库作为训练和测试样本.Caltech是加州理工学院的图像数据库,包含Caltech101和Caltech256两个数据集.该数据集是由Fei-Fe ...
- JSP中动态include与静态include的区别介绍
转自:https://m.jb51.net/article/43304.htm 动态INCLUDE 用法:<jsp:include page="included.jsp" f ...
- java布局管理
FlowLayout :组件在一行中按加入的先后顺序从左至右水平排列,排满后折行,每行中的组件都居中排列.BorderLayout:把容器空间划分为北.南.西.东.中五个区,每加入一个组件都应说明把这 ...
- Kettle的改名由来
不多说,直接上干货! 当时啊,因为很多开源项目到最后都成了无人管的项目,为了避免这种情况的发生,要尽快为Kettle项目构建一个社区.这就意味着,在随后的几年可能需要回答上千封的电子邮件和论坛帖子.幸 ...
- nginx的location 匹配的规则问题
正则解释: ~ #匹配一个正则匹配,区分大小写~* #匹配一个正则,不区分大小写^~ #普通字符匹配,如果该选择匹配不匹配别的选项,一般用来匹配目录= #精确匹配 匹配案例:location = / ...
- 前端压缩图片,前端压缩图片后转换为base64.
今天利用一上午研究了一下前端如何将5m左右的照片转换base64大小为 100k以内! 有两个链接:https://www.cnblogs.com/007sx/p/7583202.html :http ...
- ftp 一个账号多个家目录的解决方案
通常,配置ftp时,一个ftp账号只对应一个家目录,不能有多个家目录的情况. 但是,根据公司开发项目的需求,需要做到一个ftp对应多个开发目录.有想过创建软链接的,可是发现通过ftp是访问不了的. 举 ...
- “.”开头,以"}"结尾,中间是任意字符的正则
"."开头,以"}"结尾,中间是任意字符的正则 /^\..+\{$/