android studio常用控件
1.Button设置不同的样式
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"> <!--android:drawable="@drawable/ic_launcher"-->
<item android:state_pressed="true" > <shape> <solid android:color="#ff1234"></solid>
<corners android:topLeftRadius="5dp"
android:topRightRadius="5dp"
android:bottomRightRadius="5dp"
android:bottomLeftRadius="5dp"
/> </shape> </item>
<item android:state_focused="true"
android:drawable="@drawable/ic_launcher" > <shape> <solid android:color="#ffaeff5d"></solid>
<corners android:topLeftRadius="5dp"
android:topRightRadius="5dp"
android:bottomRightRadius="5dp"
android:bottomLeftRadius="5dp"
/> </shape> </item>
<item>
<shape> <solid android:color="#ffffb757"></solid>
<corners android:topLeftRadius="5dp"
android:topRightRadius="5dp"
android:bottomRightRadius="5dp"
android:bottomLeftRadius="5dp"
/> </shape> </item> </selector>
如果是加载图片,item代码更改
<item android:state_pressed="true" android:drawable="@drawable/ic_launcher">
2.EditText xml: android:hint="type something here"提示语句 获取text:oast.makeText(this,inputText,Toast.LENGTH_SHORT).show();如果edittext被键盘挡住,需要在androidmanifest中加一句:android:windowSoftInputMode="adjustPan|stateHidden"
<activity
android:name="******"
android:configChanges="keyboardHidden|orientation|screenSize"
android:screenOrientation="portrait"
android:theme="@style/AppTheme.NoActionBar">
android:windowSoftInputMode="adjustPan|stateHidden"
</activity>
editText显示多行
textMultiLine显示多行 minLines最小行数 gravity="left|top"光标置顶
<EditText
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="10dp"
android:background="null"
android:inputType="textMultiLine"
android:minLines="6"
android:gravity="left|top"
android:hint="变更事项"/>
3.ImageView:xml需要:android:src="@drawable/ic_launcher" 代码:imgView.setImageResource(R.drawable.ic_launcher);
4.ProgressBar 加载进度提示,就是进度条
<ProgressBar
android:id="@+id/progressBar1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true" />
progressBar.setVisibility(View.GONE);
5.TextView:跑马灯效果
android:ellipsize="marquee"
android:focusable="true"
android:focusableInTouchMode="true"
android:singleLine="true"
android:marqueeRepeatLimit="marquee_forever"
如果有文本框焦点将会没有,跑马灯效果就会停止,所以需要增加一句
<requestFocus/>
5.AlertDialog,样式很多,写一个
dialog.setPositiveButton方法是确定按钮的点击事件
dialog.setNegativeButton取消按钮的点击事件
dialog.setCancelable(false);
dialog弹出后会点击屏幕或物理返回键,dialog不消失
dialog.setCanceledOnTouchOutside(false);
dialog弹出后会点击屏幕,dialog不消失;点击物理返回键dialog消失
AlertDialog.Builder dialog = new AlertDialog.Builder(main.this);
dialog.setTitle("this is a dialog");
dialog.setMessage("something important");
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();
使用alertview创建一个简单的选择列表
第一步:在xml中创建一个Button 和一个TextView
第二步:创建选择数组的数据源
private String []fruits = new String[]{"苹果","香蕉","橘子","凤梨","芒果"};
第三步:展示
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setTitle("请选择你喜欢吃的水果");
builder.setItems(fruits,new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
textView.setText(fruits[which]);
}
});
builder.show();
6.ProgressDialog
ProgressDialog progressDialog = new ProgressDialog(main.this);
progressDialog.setTitle("this is a progressDialog");
progressDialog.setMessage("load...");
progressDialog.setCancelable(true);
progressDialog.show();; 停止显示progressDialog.dismiss();
7.ImageView加载网络图片
package com.android.antking.imageview; import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL; import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView; public class MainActivity extends Activity {
//定义一个图片显示控件
private ImageView imageView;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//图片资源
String url = "http://s16.sinaimg.cn/orignal/89429f6dhb99b4903ebcf&690";
//得到可用的图片
Bitmap bitmap = getHttpBitmap(url);
imageView = (ImageView)this.findViewById(R.id.imageViewId);
//显示
imageView.setImageBitmap(bitmap); }
/**
* 获取网落图片资源
* @param url
* @return
*/
public static Bitmap getHttpBitmap(String url){
URL myFileURL;
Bitmap bitmap=null;
try{
myFileURL = new URL(url);
//获得连接
HttpURLConnection conn=(HttpURLConnection)myFileURL.openConnection();
//设置超时时间为6000毫秒,conn.setConnectionTiem(0);表示没有时间限制
conn.setConnectTimeout(6000);
//连接设置获得数据流
conn.setDoInput(true);
//不使用缓存
conn.setUseCaches(false);
//这句可有可无,没有影响
//conn.connect();
//得到数据流
InputStream is = conn.getInputStream();
//解析得到图片
bitmap = BitmapFactory.decodeStream(is);
//关闭数据流
is.close();
}catch(Exception e){
e.printStackTrace();
} return bitmap; }
}
第二种方式:使用ImageLoader库文件,第一步gitup下载 https://github.com/nostra13/Android-Universal-Image-Loader,导入jar包文件
androidmanifest ,注册
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
dependencies声明
compile 'com.nostra13.universalimageloader:universal-image-loader:1.9.5'
使用代码
DisplayImageOptions options;
options = new DisplayImageOptions.Builder().showImageOnLoading(R.drawable.ic_launcher) // 设置图片在下载期间显示的图片
.showImageForEmptyUri(R.drawable.ic_launcher)// 设置图片Uri为空或是错误的时候显示的图片
.showImageOnFail(R.drawable.ic_launcher) // 设置图片加载/解码过程中错误时候显示的图片
.cacheInMemory(true)// 设置下载的图片是否缓存在内存中
.cacheOnDisk(true)// 设置下载的图片是否缓存在SD卡中
.imageScaleType(ImageScaleType.EXACTLY_STRETCHED)// 设置图片以如何的编码方式显示
.bitmapConfig(Bitmap.Config.RGB_565)// 设置图片的解码类型//
.resetViewBeforeLoading(true)// 设置图片在下载前是否重置,复位
.build();// 构建完成
String uri = this.list1.get(position);
ImageLoader imageLoader;
imageLoader = ImageLoader.getInstance();
imageLoader.init(ImageLoaderConfiguration.createDefault(this.context));
// imageLoader.init(ImageLoaderConfiguration.createDefault(this));
imageLoader.displayImage(uri, holder.imageView, options);
8.progressdialog
final ProgressDialog dialog = new ProgressDialog(this);
dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);// 设置进度条的形式为圆形转动的进度条
dialog.setCancelable(true);// 设置是否可以通过点击Back键取消
dialog.setCanceledOnTouchOutside(false);// 设置在点击Dialog外是否取消Dialog进度条
dialog.setIcon(R.drawable.ic_launcher);//
// 设置提示的title的图标,默认是没有的,如果没有设置title的话只设置Icon是不会显示图标的
dialog.setTitle("提示");
// dismiss监听
dialog.setOnDismissListener(new DialogInterface.OnDismissListener() { @Override
public void onDismiss(DialogInterface dialog) {
// TODO Auto-generated method stub }
});
// 监听Key事件被传递给dialog
dialog.setOnKeyListener(new DialogInterface.OnKeyListener() { @Override
public boolean onKey(DialogInterface dialog, int keyCode,
KeyEvent event) {
// TODO Auto-generated method stub
return false;
}
});
// 监听cancel事件
dialog.setOnCancelListener(new DialogInterface.OnCancelListener() { @Override
public void onCancel(DialogInterface dialog) {
// TODO Auto-generated method stub }
});
//设置可点击的按钮,最多有三个(默认情况下)
dialog.setButton(DialogInterface.BUTTON_POSITIVE, "确定",
new DialogInterface.OnClickListener() { @Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub }
});
dialog.setButton(DialogInterface.BUTTON_NEGATIVE, "取消",
new DialogInterface.OnClickListener() { @Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub }
});
dialog.setButton(DialogInterface.BUTTON_NEUTRAL, "中立",
new DialogInterface.OnClickListener() { @Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub }
});
dialog.setMessage("这是一个圆形进度条");
dialog.show();
new Thread(new Runnable() { @Override
public void run() {
// TODO Auto-generated method stub
try {
Thread.sleep(5000);
// cancel和dismiss方法本质都是一样的,都是从屏幕中删除Dialog,唯一的区别是
// 调用cancel方法会回调DialogInterface.OnCancelListener如果注册的话,dismiss方法不会回掉
dialog.cancel();
// dialog.dismiss();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} }
}).start();
dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);// 设置进度条的形式为圆形转动的进度条
dialog.setCancelable(true);// 设置是否可以通过点击Back键取消
dialog.setCanceledOnTouchOutside(false);// 设置在点击Dialog外是否取消Dialog进度条
dialog.setIcon(R.drawable.ic_launcher);//
- // 设置提示的title的图标,默认是没有的,如果没有设置title的话只设置Icon是不会显示图标的
- dialog.setTitle("提示");

android studio常用控件的更多相关文章
- 五、Android学习第四天补充——Android的常用控件(转)
(转自:http://wenku.baidu.com/view/af39b3164431b90d6c85c72f.html) 五.Android学习第四天补充——Android的常用控件 熟悉常用的A ...
- Android笔记---常用控件以及用法
这篇文章主要记录下Android的常用控件以及使用的方法,Android 给我们提供了大量的UI控件,合理地使用这些控件就可以非常轻松地编写出相当不错的界面,这些是Android学习的基础,没有什么业 ...
- Android中常用控件及属性
在之前的博客为大家带来了很多关于Android和jsp的介绍,本篇将为大家带来,关于Andriod中常用控件及属性的使用方法,目的方便大家遗忘时,及时复习参考.好了废话不多讲,现在开始我们本篇内容的介 ...
- android xml 常用控件介绍
android常用控件介绍 ------文本框(TextView) ------列表(ListView) ------提示(Toast) ------编辑框(EditText) ...
- Android Studio 之 控件基础知识
1. TextView 和 EditText 控件常用属性 android:layout_width="match_parent" 宽度与父控件一样宽 android:layou ...
- Android Studio gridview 控件使用自定义Adapter, 九宫格items自适应全屏显示
先看效果图,类似于支付宝首页的效果.由于九宫格显示的帖子网上已经很多,但是像这样九宫格全屏显示的例子还不是太多.本实例的需求是九宫格全屏显示,每个子view的高度是根据全屏高度三等分之后自适应高度,每 ...
- android测试--常用控件测试及测试经验(常见)
1.图片选择器 ================测试中遇到的问题记录(除表中记录的)================================================== ①.曾出现,断 ...
- Android Studio 基础控件使用
TextView android:gravity="center" //文字对其方式 top bottom left right center android:textColor= ...
- Android App常用控件
随机推荐
- LINK : warning LNK4098: 默认库“LIBCMT”与其他库的使用冲突;请使用 /NODEFAULTLIB:library
解决方法 属性=>配置属性=>输入=>忽略特定库LIBCMT
- MFC框架程序解析
MFC的 程序框架: WinMain函数:程序首先到达全局变量theApp,再到达theAPP的构造函数,最后到达WinMain函数处. 问:为何要定义一个全局对象theAPP,让其在WinMain函 ...
- ios设备唯一标识获取策略
In iOS 7 and later, if you ask for the MAC address of an iOS device, the system returns the value 02 ...
- html css float left与 float right的使用说明(转)
点评: CSS中很多时候会用到浮动来布局,也就是经常见到的float:left或者float:right,简单点来说,前者是左浮动(往左侧向前边的非浮动元素飘,全是飘得元素的话,就按照流式来浮动从左到 ...
- 五、K3 WISE 开发插件《K3 Wise 群发短信配置开发(二)之短信群发配置》
开发环境:K/3 Wise 13.0.Sql Server 2005 目录 一.开启Sql Server Agent代理服务 二.短信发送原理 三.编写存储过程 四.开启Sql Server作业 一. ...
- 原生js--客户端存储的种类
客户端存储遵循同源策略,不同的站点页面之间不可以相互读取对方的数据,但同一站点的不同页面之间可以共享存储的数据 客户端存储的种类: 1.web存储 localStorage.sessionStorag ...
- 第二步 (仅供参考) sencha touch + PhoneGap(cordova 2.9 及其以下版本) 使用 adt eclipse进行打包
首先你得安装一个adt-eclipse 参考资料 http://www.crifan.com/android_eclipse_offline_install_adt/ 然后就可以运行adt-eclip ...
- tcp连接的状态变迁以及如何调整tcp连接中处于time_wait的时间
一.状态变迁图 二.time_wait状态 针对time_wait和close_wait有个简单的描述帮助理解: Due to the way TCP/IP works, connections ca ...
- [HTML5]移动平台的HTML5开发框架
jQuery Mobile http://jquerymobile.com/ jQTouch http://jqtouch.com/ DHTMLX Touch http://dhtmlx.com/to ...
- CentOS6 防火墙配置
清空现有的规则 iptables -F iptables -P INPUT DROP iptables -I INPUT -m state --state RELATED , ESTABLISHED ...