EditText

EditText的一些属性:

textColor

Capitalize  sentences每一句话第一个字母大写  words单词首字母大写  characters每一个字符都大写

Hint 没有字符输入时,默认显示字符

textColorHint   hint字符的颜色

cursorVisible  光标是否显示,true显示,false不显示

Enable  是否可正常使用

Editable 是否可获得光标(但是仍然不可用)

main_xml中的代码:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">

    <ScrollView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:id="@+id/scrollView"
        android:layout_alignParentTop="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true" >

        <LinearLayout
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <EditText
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:id="@+id/editText"
                android:hint="任意字符"/>

            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="确定"
                android:id="@+id/button"
                android:onClick="Sure"/>

            <EditText
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:inputType="textPassword"
                android:ems="10"
                android:id="@+id/editText2"
                android:layout_gravity="center_horizontal"
                android:hint="Please input your password"
                android:textColorHint="@color/colorAccent"/>

            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="确定"
                android:id="@+id/button2"
                android:onClick="surePassword"/>

            <EditText
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:inputType="numberSigned"
                android:ems="10"
                android:id="@+id/editText3"
                android:hint="SignedNumber"/>

            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="确定"
                android:id="@+id/button3"
                android:onClick="SignedNumber"/>

            <EditText
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:inputType="numberDecimal"
                android:numeric="signed"
                android:ems="10"
                android:id="@+id/editText4"
                android:hint="Decimal"/>

            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="确定"
                android:id="@+id/button4"
                android:onClick="Decimal"/>

            <EditText
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:inputType="number"
                android:ems="10"
                android:id="@+id/editText5"
                android:hint="Number"/>

            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="确定"
                android:id="@+id/button5"
                android:onClick="Number"/>
        </LinearLayout>
    </ScrollView>

</RelativeLayout>
activity中的代码:

package com.example.hailang.edittext;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    EditText editText;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


    }

    public void Sure(View v)
    {
        editText = (EditText)findViewById(R.id.editText);
        Toast.makeText(this,"The string:" + editText.getText(), Toast.LENGTH_SHORT).show(); //在屏幕中显示出来
    }

    public void surePassword(View v)
    {
        editText = (EditText)findViewById(R.id.editText2);
        Toast.makeText(this,"Password:" + editText.getText(), Toast.LENGTH_SHORT).show();
    }

    public void SignedNumber(View v)
    {
        editText = (EditText)findViewById(R.id.editText3);
        int x = Integer.parseInt("" + editText.getText());
        x += 10;
        Toast.makeText(this, "Number + 10 = " + x, Toast.LENGTH_SHORT).show();
    }

    public void Decimal(View v)
    {
        editText = (EditText)findViewById(R.id.editText4);
        float x = Float.parseFloat("" + editText.getText());
        x /= 10;
        Toast.makeText(this, "Float / 10 = " + x, Toast.LENGTH_SHORT).show();
    }

    public void Number(View v)
    {
        editText = (EditText)findViewById(R.id.editText5);
        float x = Float.parseFloat("" + editText.getText());
        Toast.makeText(this,"Number:" + x, Toast.LENGTH_SHORT).show();
    }
}



你可能感兴趣的:(EditText)