Android开发之路七----UI组件2

来源:互联网 发布:达尔文进化论假说知乎 编辑:程序博客网 时间:2024/06/11 17:08

                    Android开发之路七----UI组件2

今天我们继续学习UI组件,主要是学习了TextViewEditText这两种组件

 TextView组件介绍:

  直接的子类:

  ButtonCheckdTextViewCheronometerDigitalClockEditText

  间接的子类:

  AutoCompleteTextViewCheckBoxCommpoundButtonMultiAutoCompleteTextView

  在其xml文件中有许多的属性今天讲到例如:

  AndroidautoLink:这是设置文本为URL链接/email/电话号码时,文本显示为可点击的链接,可选的值为(none/web/email/phone/map这属性今天没讲/all

  Android:autoText:这是如果设置,将自动执行输入值的拼写纠正,在显示输入法并输入的时候起作用。   

  Android:drawableButton:这是在text中的下方输入一个drawable,图片,如果指定一个颜色的话会把text的背景颜色设为该颜色,并且同时和background使用时覆盖后者。

   Android:gravity:设置文本位置,可以设置成“left”“center”“right”对应的分别是左,居中,右。

  Android:inputType 设置文本的类型

  ..................................   

下面就介绍一下用TextView组件来实现程序这是三个不同的Helloword,不同之处就是利用了TextView中的标签

 

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="fill_parent"

    android:layout_height="fill_parent"

    android:orientation="vertical" >

    

     <TextView

        android:layout_width="fill_parent"

        android:layout_height="wrap_content"

        android:text="@string/hello" />

      //原始的helloword

    <TextView

        android:layout_width="fill_parent"

        android:layout_height="wrap_content"

        android:textColor="#00FF00"

        android:padding="30dp"

        android:textSize="30dp"

        android:layout_margin="50dp"

        android:text="@string/hello" />

    //加上颜色--textcolor和大小--textSize

   

    <TextView

        android:layout_width="fill_parent"

        android:layout_height="wrap_content"

        android:text="@string/hello" 

        android:gravity="center"

        />

      //设置了文本的居中格式

         android:gravity="center"

</LinearLayout>

运行后形成的界面就是这个样子的


这就是利用标签形成的文本的不同效果。

为文本添加背景图片

    <TextView 

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:padding="10dp"

        android:layout_margin="10dp"

        android:textColor="#cccccc"

        android:text="@string/hello"

        android:background="@drawable/ic_launcher"

      

        />

       利用上面的这段代码就可以实现为你的文本添加背景图片,虽然这是添加背景图片但是这可能会使图片失真,随意的拉伸图片。这就要求我们很好的掌握photoshop的功夫,要把图片p好,那样就不会失真。

 

为文本加链接

        <TextView 

        android:layout_width="fill_parent"

        android:layout_height="wrap_content"

        android:autoLink="web"

        android:text="@string/webUrl"

        />

       

    <TextView 

        android:layout_width="fill_parent"

        android:layout_height="wrap_content"

         android:autoLink="email"

        android:text="@string/email"

        />

    

    

    <TextView 

        android:layout_width="fill_parent"

        android:layout_height="wrap_content"

         android:autoLink="phone"

        android:text="@string/phoneNumber"

        />

    

    

    <TextView 

        android:layout_width="fill_parent"

        android:layout_height="wrap_content"

         android:autoLink="all"

        android:text="@string/autoAll"

        />

     

    <TextView 

        android:layout_width="fill_parent"

        android:layout_height="wrap_content"

         android:id="@+id/textview"

        

        /> 

 

  所形成的界面是这个样子的

   

所有的链接就加上了。

自定义带边框的TextView

public class BorderTextView extends TextView {

public BorderTextView(Context context, AttributeSet attr) {

super(context,attr);

}

public void onDraw(Canvas canvas) {

super.onDraw(canvas);

Paint paint = new Paint();

paint.setColor(android.graphics.Color.GREEN);

canvas.drawLine(0, 0, this.getWidth() - 1, 0, paint);

canvas.drawLine(0, 0, 0, this.getHeight() - 1, paint);

canvas.drawLine(this.getWidth() - 1, 0, this.getWidth() - 1,

this.getHeight() - 1, paint);

canvas.drawLine(0, this.getHeight() - 1, this.getWidth() - 1,

this.getHeight() - 1, paint);

}

}

<cn.class3g.activity.BorderTextView

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:gravity="center"

        android:padding="30dp"

        android:text="xxxxxxxxxxxxx"

        />

     EditText组件的应用

  EditTextTextView的子类,继承了TextView的大部分xml属性,所以用法与TextView的用法大致一致。

 其中一些特定的字符串

 <EditText

        android:layout_width="fill_parent"

        android:layout_height="wrap_content"

        android:password="true"

        android:digits="01234" />

  //这是设置文本可以允许那些字符可以输入,上面的命令就是就可以在01234中的字符可以输进文本编辑中

    <EditText

        android:layout_width="fill_parent"

        android:layout_height="wrap_content"

        android:digits="abcd" />

//这是设置文本可以允许那些字符可以输入,上面的命令就是就可以在abcd中的字符可以输进文本编辑中

      

    <EditText

        android:layout_width="fill_parent"

        android:layout_height="wrap_content"

        android:inputType="number" />

   <EditText

        android:layout_width="fill_parent"

        android:layout_height="wrap_content"

          android:password="true"

         />

//这是在文本框中密码的设置,只要输上字符,会以原点的形式显示在界面

      <EditText

        android:layout_width="fill_parent"

        android:layout_height="wrap_content"

        android:inputType="textEmailAddress"

         />

//邮箱的设置,在打上字符,会有@在编辑的地方来提示

       <EditText

        android:layout_width="fill_parent"

        android:layout_height="wrap_content"

        android:inputType="number"

         />

   //必须是数字的字符

    <EditText

        android:layout_width="fill_parent"

        android:layout_height="wrap_content"

        android:numeric="decimal|signed" />

//显示文本框

EditText对象的注册OnKeyListener事件,实现onKey()方法

<EditText

        android:id="@+id/text1"

        android:layout_width="fill_parent"

        android:layout_height="wrap_content"

        android:text="text1" />

    <Button

        android:id="@+id/button1"

        android:layout_width="fill_parent"

        android:layout_height="wrap_content"

        android:visibility="gone"

        android:text="Button" />

public boolean onKey(View view, int keyCode, KeyEvent event) {

if (keyCode == KeyEvent.KEYCODE_ENTER) {

btn.setText(et.getText());

et.setVisibility(View.GONE);

btn.setVisibility(View.VISIBLE);

}

return true;

}


按下回车键之后就会成这样


原创粉丝点击