EditView 是可编辑文本域 它在android api中位置如下
java.lang.Object | |||
↳ | android.view.View | ||
↳ | android.widget.TextView | ||
↳ | android.widget.EditText |
本篇概述:
1. 简单的EditView的示例 ,
EditText布局是在上篇的TextView布局上续写的
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:background="#4399f0" android:orientation="vertical" android:layout_height="match_parent" > <TextView android:id="@+id/text_1" android:layout_width="fill_parent" android:layout_height="wrap_content" android:drawableRight="@drawable/ic_action_search" android:shadowColor="#ff0000" android:shadowDx="2" android:shadowDy="2" android:shadowRadius="3" android:text="@string/hello_world" /> <TextView android:id="@+id/text_2" android:layout_width="fill_parent" android:layout_height="wrap_content" android:drawableRight="@drawable/ic_action_search" android:shadowColor="#456789" android:shadowDx="2" android:shadowDy="2" android:shadowRadius="3" android:text="@string/phone" /> <TextView android:id="@+id/text_3" android:layout_width="fill_parent" android:layout_height="wrap_content" android:drawableRight="@drawable/ic_action_search" android:autoLink="all" android:text="@string/myblogurl" /> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/myblogurl" android:textSize="18sp" android:autoLink="all" android:ellipsize="marquee" android:focusable="true" android:marqueeRepeatLimit="marquee_forever" android:focusableInTouchMode="true" android:scrollHorizontally="true" android:shadowColor="#ff0000" android:shadowRadius="3.0" /> <EditText android:layout_width="0dp" android:layout_height="0dp" android:inputType="text"/> <EditText android:layout_width="fill_parent" android:layout_height="wrap_content" android:drawableLeft="@drawable/ic_launcher" android:background="@layout/shape" android:hint="@string/phone" android:inputType="phone"/> </LinearLayout>
需要注意的是, 这边的
android:background = "@layout/shape"
EditView使用了背景布局 shape.xml
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android"> <gradient android:startColor="#ffffff" android:endColor="#000000" android:angle="270" /> <corners android:radius="30dp" /> </shape>
android:shape="rectangle" 设置为rectangle 矩形
android:gradient 设置渐变背景颜色
<corners android:radius="30dp"/> 圆角半径为 30dp
OK , 我们在Activity中使用setContentView(),
public class MainActivity extends Activity { String strs = ""; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.edittext); /* TextView*/ TextView text_1 = (TextView)findViewById(R.id.text_1); strs = text_1.getText().toString(); SpannableStringBuilder style = new SpannableStringBuilder(strs); style.setSpan(new BackgroundColorSpan(Color.RED),4,7,Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); style.setSpan(new ForegroundColorSpan(Color.BLUE),0,3,Spannable.SPAN_EXCLUSIVE_INCLUSIVE); text_1.setText(style); TextView text_2 = (TextView)findViewById(R.id.text_2); text_2.setText(Html.fromHtml("aaaaaaaaaaa<font color=blue>blue</font>aaaaaaaaa")); } }
一些Shape.xml布局
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android"> <gradient android:startColor="#3A3C39" android:endColor="#181818" android:angle="270" /> <corners android:radius="0dp" /> </shape> android:background="@drawable/shape_background_grey" One android: angle = "270" on behalf of direction, that from top to bottom 270, 180, said right to left, the default left <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle"> <gradient android:type="radial" android:gradientRadius="250" android:startColor="#E9E9E9" android:endColor="#D4D4D4" /> </shape> <shape xmlns:android="http://schemas.android.com/apk/res/android"> <gradient android:angle="0" android:startColor="#FFdaf3fc" android:centerColor="#FFd4e9a9" android:endColor="#FFdaf3fc"/> </shape> Here should pay attention to android: type = "radial" type of use will have different effects android: centerColor = "# FFd4e9a9" Usually this is not being used <?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle"> <gradient android:startColor="#509245" android:centerColor="#3e8532" android:endColor="#509245" android:type="linear" android:angle="90" android:centerX="0.5" android:centerY="0.5" /> <padding android:left="7dp" android:top="7dp" android:right="7dp" android:bottom="7dp" /> <corners android:radius="4dp" /> </shape>