关于Android 中常见控件以及使用

TextView文本框

TextView类的结构

TextView是用于显示字符串的组件,对于用户来说就是屏幕中一块用于显示文本的区域。TextView类的层次关系如下:

java.lang.Object

↳android.view.View

↳android.widget.TextView

直接子类:

Button, CheckedTextView, Chronometer, DigitalClock, EditText

间接子类:

AutoCompleteTextView, CheckBox, CompoundButton, ExtractEditText,MultiAutoCompleteTextView, RadioButton, ToggleButton

TextView的使用

我们可以在XML布局文件中声明及设置TextView,也可以在代码中生成TextView组件。

在xml布局中:

//显示超链接:

"@+id/textView0"android:layout_width="fill_parent"android:layout_height="wrap_content"

android:textColor="#FF0000"

android:textSize="18dip"android:background="#FFFFFF"android:text="拨打手机:13888888888"

android:gravity="center_vertical|center_horizontal"android:autoLink="phone"/>

"@+id/textView1"android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:textColor="#FF0000"

android:textSize="18dip"android:background="#00FF00"

android:text="Google搜索:http://www.google.com.hk"android:gravity="center_vertical|center_horizontal"

android:autoLink="web"/>

"@+id/textView2"android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:textColor="#FF0000"

android:textSize="18dip"

android:background="#FFFF00"

android:text="发送邮件:1007857613@qq.com"

android:gravity="center_vertical|center_horizontal"android:autoLink="email"/>

//设置文字的滚屏

"@+id/textView3"android:layout_width="fill_parent"

android:layout_height="20dp"android:textSize="18dip"

android:ellipsize="marquee"android:focusable="true"

android:marqueeRepeatLimit="marquee_forever"

android:focusableInTouchMode="true"

android:scrollHorizontally="true"

android:text="文字滚屏文字跑马灯效果加长加长加长加长加长加长加长加长加长加长加长加长"

android:background="#FF0000"

android:textColor="#FFFFFF">

//设置字符阴影

"@+id/TextView4"android:layout_width="fill_parent"

android:layout_height="wrap_content"android:textSize="18dip"

android:background="#69541b"android:textColor="#FFFFFF"android:text="设置字符串阴影颜色为绿色"

android:shadowColor="#45b97c"android:shadowRadius="3.0"

android:gravity="center_vertical|center_horizontal"/>

//设置字符的外形

"fill_parent"

android:layout_height="wrap_content"android:textSize="18dip"

android:background="#FFFFFF"android:textColor="#FF0000"android:text="设置文字外形为italic"

android:textStyle="italic"android:gravity="center_vertical|center_horizontal"/>

在代码中:

/动态创建textView

linearLayout= (LinearLayout)findViewById(R.id.linearLayout01);

param=newLinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT,

LinearLayout.LayoutParams.WRAP_CONTENT);

LinearLayout layout1 =newLinearLayout(this);

layout1.setOrientation(LinearLayout.HORIZONTAL);

TextView tv =newTextView(this);

tv.setId(200);

tv.setText("动态创建TextView");

tv.setBackgroundColor(Color.GREEN);

tv.setTextColor(Color.RED);

tv.setTextSize(20);

layout1.addView(tv,param);

linearLayout.addView(layout1,param);

EditText编辑框

EditText类的结构

EditText和TextView的功能基本类似,他们之间的主要区别在于EditText提供了可编辑的文本框。

java.lang.Object

android.view.View

android.widget.TextView

android.widget.EditText

直接子类:

AutoCompleteTextView, ExtractEditText

间接子类:

MultiAutoCompleteTextView

EditText的使用

EditText和TextView一样,既可以在Xml中声明实现,也可以在代码中动态的生成,关于在代码动态生成和TextView的类似,这里就不在赘述。下面以一个实例来说明EditText的简单使用:

XML布局中:

"TextView"android:id="@+id/textView1"

android:layout_height="wrap_content"android:layout_width="fill_parent">

"@+id/editText1"android:inputType="date"

android:layout_width="fill_parent"android:layout_height="wrap_content"

android:maxLength="40"android:hint="输入电话号码"android:textColorHint="#FF000000"

android:phoneNumber="true"android:imeOptions="actionGo">

"@+id/editText2"android:inputType="date"

android:layout_width="fill_parent"android:layout_height="wrap_content"

android:maxLength="40"android:hint="输入密码"android:textColorHint="#FF000000"

android:password="true"android:imeOptions="actionSearch">

/rec/values/string.xml中:

stringname="RadioButton1">Windows

"RadioButton2">Linux

"RadioButton3">Mocos

"RadioButton4">Java

在Activity中让editText显示在屏幕上并实现监听:

/**

*获得TextView对象

*获得两个EditTextView对象

*/

textview= (TextView)findViewById(R.id.textView1);

edittext_num= (EditText)findViewById(R.id.editText1);

edittext_pass= (EditText)findViewById(R.id.editText2);

/**

*对EditText进行监听

*获得编辑框里面的内容并显示

*/

edittext_num.addTextChangedListener(newTextWatcher() {

@Override

publicvoidonTextChanged(CharSequence s,intstart,intbefore,intcount) {

//if(!(textview.getText().toString()==null))

textview.setText("edittext_num:"+edittext_num.getText().toString());

}

@Override

publicvoidbeforeTextChanged(CharSequence s,intstart,intcount,

intafter) {

//TODOAuto-generated method stub

}

publicvoidafterTextChanged(Editable s) {

//TODOAuto-generated method stub

}

});

edittext_pass.addTextChangedListener(newTextWatcher() {

@Override

publicvoidonTextChanged(CharSequence s,intstart,intbefore,intcount) {

//if(!(textview.getText().toString()==null))

textview.setText("你正在编辑edittext_pass");

}

publicvoidbeforeTextChanged(CharSequence s,intstart,intcount,

intafter) {

//TODOAuto-generated method stub

}

publicvoidafterTextChanged(Editable s) {

//TODOAuto-generated method stub

}

});

}

Button按钮

Button类的结构

Button类的层次关系如下:

java.lang.Object

↳android.view.View

↳android.widget.TextView

↳android.widget.Button

直接子类:

CompoundButton

间接子类:

CheckBox, RadioButton, ToggleButton

Button的使用

Button可以在xml中声明,也可以在代码中动态创建。

在xml中:

"vertical"

android:layout_height="wrap_content"android:id="@+id/button1"

android:layout_width="wrap_content"

android:textColor="#0000FF"

android:text="这是button1,可以点击"/>

"vertical"

android:layout_height="wrap_content"android:id="@+id/button2"

android:layout_width="wrap_content"

android:textColor="#CD0000"

android:text="这是button2,可以点击"/>

"vertical"

android:layout_height="wrap_content"android:id="@+id/button3"

android:layout_width="wrap_content"

android:textColor="#000000"

android:text="这是button3,可以点击"/>

在代码中创建Button:

button4=newButton(this);

button4.setId(100);

button4.setText("动态创建的Button");

button4.setTextColor(Color.GREEN);

创建好了Button后,我们就可以对其进行监听了,其中有两种方式:

一种是继承OnClickListenner接口:

publicclassButtonActivityextendsActivityimplementsOnClickListener{

button1= (Button)findViewById(R.id.button1);

button1.setOnClickListener(this);

publicvoidonClick(View v) {

switch(v.getId()){

caseR.id.button1:

.........

break;

}

}

}

另一种方式是:

publicclassButtonActivityextendsActivity{

button1= (Button)findViewById(R.id.button1);

button1.setOnClickListener(newButton.OnClickListener(){

publicvoidonClick(View v) {

//TODOAuto-generated method stub

..........

}

});

ImageView类的结构

ImageView显示任意图像,例如图标。ImageView类可以加载各种来源的图片(如资源或图片库),需要计算图像的尺寸,比便它可以在其他布局中使用,并提供例如缩放和着色(渲染)各种显示选项。

继承关系

public classView.OnClickListnerextendsView

java.lang.Object

android.view.View

android.widget.ImageView

直接子类

ImageButton, QuickContactBadge

间接子类

ZoomButton

ImageView的使用

我们首先要在XML文件中声明一个ImageView和一个TextView :

android:id="@+id/ImageView01"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

>

android:id="@+id/TextView01"

android:layout_below="@id/ImageView01"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

>

然后在Activity中设置显示ImageView:

isrung=true;

//获得ImageView的对象

imageview= (ImageView)this.findViewById(R.id.ImageView01);

textview= (TextView)this.findViewById(R.id.TextView01);

//设置imageview的图片资源。同样可以再xml布局中像下面这样写

//android:src="@drawable/logo"

imageview.setImageResource(R.drawable.imag2);

//设置imageview的Alpha值

imageview.setAlpha(image_alpha);

之后就需要用一个线程来实时更新alpha:

//开启一个线程来让Alpha值递减

newThread(newRunnable() {

publicvoidrun()

{

while(isrung)

{

try

{

Thread.sleep(200);

//更新Alpha值

updateAlpha();

}

catch(InterruptedException e)

{

e.printStackTrace();

}

}

}

}).start();

//接受消息之后更新imageview视图

mHandler=newHandler() {

@Override

publicvoidhandleMessage(Message msg)

{

super.handleMessage(msg);

imageview.setAlpha(image_alpha);

textview.setText("现在alpha值是:"+Integer.toString(image_alpha));

//更新

imageview.invalidate();

}

};

}

publicvoidupdateAlpha()

{

if(image_alpha- 7 >= 0)

{

image_alpha-= 7;

}

else

{

image_alpha= 0;

isrung=false;

}

//发送需要更新imageview视图的消息

mHandler.sendMessage(mHandler.obtainMessage());

}

ProgressBar进度条

ProgressBar类概述

progressBar进度条是一个显示进度的控件,Android提供了两大类进度条样式,长形进度条样式progress-BarStyleHorizontal和圆形进度条progressBarStyleLarge.ProgressBar类的层次关系如下:

java.lang.Object

android.view.View

android.widget.Progress

ProgressBar的使用

要实现一个简单的进度条的效果,我们可以首先在XML文件中声明一个长形进度条和一个圆形进度条:

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:text="@string/hello"

/>

android:id="@+id/ProgressBar01"

style="?android:attr/progressBarStyleHorizontal"//设置样式为长形进度条

android:layout_width="200dp"

android:layout_height="wrap_content"

android:visibility="gone"//设置当前不可见

/>

android:id="@+id/ProgressBar02"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

style="?android:attr/progressBarStyleLarge"//设置样式为圆形进度条

android:max="100"

android:progress="50"

android:secondaryProgress="70"

android:visibility="gone"//设置当前不可见

/>

android:id="@+id/Button01"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="开始"/>

然后在Activity中实现监听并通过线程来改变ProgressBar的值:

/设置窗口模式,,因为需要显示进度条在标题栏

requestWindowFeature(Window.FEATURE_PROGRESS);

setProgressBarVisibility(true);

setContentView(R.layout.main);

//取得ProgressBar

m_ProgressBar= (ProgressBar) findViewById(R.id.ProgressBar01);

m_ProgressBar2= (ProgressBar) findViewById(R.id.ProgressBar02);

mButton01= (Button)findViewById(R.id.Button01);

m_ProgressBar.setIndeterminate(false);

m_ProgressBar2.setIndeterminate(false);

//当按钮按下时开始执行,

mButton01.setOnClickListener(newButton.OnClickListener()

{

@Override

publicvoidonClick(View v)

{

//TODOAuto-generated method stub

//设置ProgressBar为可见状态

m_ProgressBar.setVisibility(View.VISIBLE);

m_ProgressBar2.setVisibility(View.VISIBLE);

//设置ProgressBar的最大值

m_ProgressBar.setMax(100);

//设置ProgressBar当前值

m_ProgressBar.setProgress(0);

m_ProgressBar2.setProgress(0);

//通过线程来改变ProgressBar的值

newThread(newRunnable() {

publicvoidrun()

{

for(inti = 0; i < 10; i++)

{

try

{

intCounter= (i + 1) * 20;

Thread.sleep(1000);

if(i == 4)

{

Message m =newMessage();

m.what= ProgressBarActivity.GUI_STOP_NOTIFIER;

ProgressBarActivity.this.myMessageHandler.sendMessage(m);

break;

}

else

{

Message m =newMessage();

m.what= ProgressBarActivity.GUI_THREADING_NOTIFIER;

ProgressBarActivity.this.myMessageHandler.sendMessage(m);

}

}

catch(Exception e)

{

e.printStackTrace();

}

}

}

}).start();

}

});

}

HandlermyMessageHandler=newHandler()

{

// @Override

publicvoidhandleMessage(Message msg)

{

switch(msg.what)

{

//ProgressBar已经是对大值

caseProgressBarActivity.GUI_STOP_NOTIFIER:

m_ProgressBar.setVisibility(View.GONE);

m_ProgressBar2.setVisibility(View.GONE);

Thread.currentThread().interrupt();

break;

caseProgressBarActivity.GUI_THREADING_NOTIFIER:

if(!Thread.currentThread().isInterrupted())

{

//改变ProgressBar的当前值

m_ProgressBar.setProgress(intCounter);

m_ProgressBar2.setProgress(intCounter);

//设置标题栏中前景的一个进度条进度值

setProgress(intCounter*100);

//设置标题栏中后面的一个进度条进度值

setSecondaryProgress(intCounter*100);//

}

break;

}

super.handleMessage(msg);

}

};

Dialog对话框

Dialog对话框

Android中实现对话框可以使用AlertDialog.类,还可以自定义对话框。如果对话框设置了按钮,那么就需要对其设置监听OnClickListener。

然后在Activity中使用AlertDialog创建对话框:

Dialog dialog =newAlertDialog.Builder(DialogActivity.this)

.setTitle("登陆提示")//设置标题

.setMessage("这里需要登录!")//设置内容

.setPositiveButton("确定",//设置确定按钮

newDialogInterface.OnClickListener()

{

publicvoidonClick(DialogInterface dialog,intwhichButton)

{

//点击“确定”转向登陆框

LayoutInflater factory = LayoutInflater.from(DialogActivity.this);

//得到自定义对话框

finalView DialogView = factory.inflate(R.layout.dialog,null);

//创建对话框

AlertDialog dlg =newAlertDialog.Builder(DialogActivity.this)

.setTitle("登录框")

.setView(DialogView)//设置自定义对话框的样式

.setPositiveButton("确定",//设置"确定"按钮

newDialogInterface.OnClickListener()//设置事件监听

{

publicvoidonClick(DialogInterface dialog,intwhichButton)

{

//输入完成后,点击“确定”开始登陆

m_Dialog= ProgressDialog.show

(

DialogActivity.this,

"请等待...",

"正在为你登录...",

true

);

newThread()

{

publicvoidrun()

{

try

{

sleep(3000);

}

catch(Exception e)

{

e.printStackTrace();

}

finally

{

//登录结束,取消m_Dialog对话框

m_Dialog.dismiss();

}

}

}.start();

}

})

.setNegativeButton("取消",//设置“取消”按钮

newDialogInterface.OnClickListener()

{

publicvoidonClick(DialogInterface dialog,intwhichButton)

{

//点击"取消"按钮之后退出程序

DialogActivity.this.finish();

}

})

.create();//创建

dlg.show();//显示

}

}).setNeutralButton("退出",

newDialogInterface.OnClickListener()

{

publicvoidonClick(DialogInterface dialog,intwhichButton)

{

//点击"退出"按钮之后推出程序

DialogActivity.this.finish();

}

}).create();//创建按钮

//显示对话框

dialog.show();

ProgressDialog对话框中的进度条

ProgressDialog类概述

前面,我们学习了对话框和进度条,现在将进度条加入到对话框,使得对话框更加的完善。我们知道进度条有长形进度条和圆形进度条,所以也就有对话框中的长形进度条和对话框中的圆形进度条。

ProgressDialog中常用的方法

setProgressStyle: 设置进度条风格,风格为圆形,旋转的或者风格为长形的;

setTitle: 设置ProgressDialog的标题

setMessage: 设置ProgressDialog的提示信息

setIcon:设置ProgressDialog的标题图标

setIndeterminate:设置ProgressDialog的进度条是否不明确

setCancelable:设置ProgressDialog是否可以按回退键取消

setButton:设置ProgressDialog的一个Button(需要监听Button事件)

show:显示ProgressDialog

29.3 ProgressDialog的使用

要实现29.1节中图形中所示的效果,我们首先在xml中声明两个Button:

android:id="@+id/yuan_button01"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="圆形进度条"/>

android:id="@+id/chang_button02"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="长形进度条"/>

在Activity中得到Button对象,并分别实现监听,在监听里面生成我们所需要的带进度条的对话框,对于长形进度条我们需要用线程来实时更新进度条的值。具体代码如下:

/得到按钮对象

mButton01= (Button)findViewById(R.id.yuan_button01);

mButton02= (Button)findViewById(R.id.chang_button02);

//设置mButton01的事件监听

mButton01.setOnClickListener(newButton.OnClickListener() {

@Override

publicvoidonClick(View v)

{

//TODOAuto-generated method stub

//创建ProgressDialog对象

m_pDialog=newProgressDialog(ProgressDialogActivity.this);

//设置进度条风格,风格为圆形,旋转的

m_pDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);

//设置ProgressDialog标题

m_pDialog.setTitle("提示");

//设置ProgressDialog提示信息

m_pDialog.setMessage("这是一个圆形进度条对话框");

//设置ProgressDialog标题图标

m_pDialog.setIcon(R.drawable.imag1);

//设置ProgressDialog的进度条是否不明确

m_pDialog.setIndeterminate(false);

//设置ProgressDialog是否可以按退回按键取消

m_pDialog.setCancelable(true);

//设置ProgressDialog的一个Button

m_pDialog.setButton("确定",newDialogInterface.OnClickListener() {

publicvoidonClick(DialogInterface dialog,inti)

{

//点击“确定按钮”取消对话框

dialog.cancel();

}

});

//让ProgressDialog显示

m_pDialog.show();

}

});

//设置mButton02的事件监听

mButton02.setOnClickListener(newButton.OnClickListener() {

@Override

publicvoidonClick(View v)

{

//TODOAuto-generated method stub

m_count= 0;

//创建ProgressDialog对象

m_pDialog=newProgressDialog(ProgressDialogActivity.this);

//设置进度条风格,风格为长形

m_pDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);

//设置ProgressDialog标题

m_pDialog.setTitle("提示");

//设置ProgressDialog提示信息

m_pDialog.setMessage("这是一个长形对话框进度条");

//设置ProgressDialog标题图标

m_pDialog.setIcon(R.drawable.imag2);

//设置ProgressDialog进度条进度

m_pDialog.setProgress(100);

//设置ProgressDialog的进度条是否不明确

m_pDialog.setIndeterminate(false);

//设置ProgressDialog是否可以按退回按键取消

m_pDialog.setCancelable(true);

//让ProgressDialog显示

m_pDialog.show();

newThread()

{

publicvoidrun()

{

try

{

while(m_count<= 100)

{

//由线程来控制进度。

m_pDialog.setProgress(m_count++);

Thread.sleep(100);

}

m_pDialog.cancel();

}

catch(InterruptedException e)

{

m_pDialog.cancel();

}

}

}.start();

}

});

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 158,736评论 4 362
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,167评论 1 291
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 108,442评论 0 243
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 43,902评论 0 204
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,302评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,573评论 1 216
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,847评论 2 312
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,562评论 0 197
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,260评论 1 241
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,531评论 2 245
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,021评论 1 258
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,367评论 2 253
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,016评论 3 235
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,068评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,827评论 0 194
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,610评论 2 274
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,514评论 2 269

推荐阅读更多精彩内容