android中TextView、TextEdit和Button的用法以及draw9patch

来源:互联网 发布:网上的淘宝运营教程 编辑:程序博客网 时间:2024/05/16 15:48

概述

知识点

TextView

TextView中的常用属性:
text不用赘述。
1、setTextColor(color):颜色的格式为#ARGB,其中ARGB分别都是十六进制数,A代表透明度,R、G、B分别为三原色中的红、绿、蓝。
在布局文件中,写作textColor=”“。
2、setTextSize()设置字体大小。布局文件中写作textSize=”“。
3、autoLink=”“:让text中的文本有特定功能。
在java代码中写作:setAutoLinkMask(Linkify.XXX),在java代码中要写在setText()之前才能生效。
4、drawableXXX=”@图片地址”:XXX可以使Left、Right、Top、Bottom。作用:在text文本的XXX方向插入图片。
5、paddingXXX=”“:离开某一边缘的距离。
6、ellipseze:none、start、middle、end、marquee,省略某一部分。
7、设置行数。
布局文件

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"                android:layout_width="match_parent"                android:layout_height="match_parent"                android:orientation="vertical">    <TextView        android:text="我要打电话18211190512"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:textSize="25sp"        android:textColor="@string/red"        android:autoLink="phone"        android:padding="25sp"/>    <LinearLayout        android:layout_width="match_parent"        android:layout_height="match_parent"        android:gravity="bottom"        android:orientation="horizontal">        <TextView            android:layout_width="0dp"            android:layout_height="wrap_content"            android:text="message"            android:gravity="center_horizontal"            android:drawableTop="@mipmap/ic_launcher"            android:paddingBottom="10dp"            android:layout_weight="1"/>        <TextView            android:layout_width="0dp"            android:layout_height="wrap_content"            android:text="phone"            android:gravity="center_horizontal"            android:drawableTop="@mipmap/ic_launcher"            android:paddingBottom="10dp"            android:layout_weight="1"/>        <TextView            android:layout_width="0dp"            android:layout_height="wrap_content"            android:text="game"            android:gravity="center_horizontal"            android:drawableTop="@mipmap/ic_launcher"            android:paddingBottom="10dp"            android:layout_weight="1"/>        <TextView            android:layout_width="0dp"            android:layout_height="wrap_content"            android:text="web"            android:gravity="center_horizontal"            android:drawableTop="@mipmap/ic_launcher"            android:paddingBottom="10dp"            android:layout_weight="1"/>    </LinearLayout></LinearLayout>

效果图:
这里写图片描述

EditText

EditText是TextView的子类,拥有TextView的所有方法。
其中常用的方法有:
1、inputType=”XXX”,XXX可以使password、number、digits等,他的作用是规定输入文本的范围。
2、hint:提示文本。相关属性:hintTextColor。
3、spanned:用于插入图片html文本。
4、setPaintFlags(Paint.XXX):给文本装饰,如添加中划线、下划线等。
5、textStyle=”“:设置文本风格(加粗、倾斜)。

用draw9patch修整图片的方法

在实际的开发中,当布局的背景被延伸过度是,会产生毛边。
利用draw9patch可以解决这些问题。
步骤:
1、在sdk文件夹下找到tools文件,其中可以看到draw9patch:
这里写图片描述
2、双击运行,可以看到如下界面:
这里写图片描述
3、将要处理的图片拖入或者ctrl+o选择该图片。会出现如下界面:
这里写图片描述
4、图片的每个角都有两条边可以拖拽,将它们拖拽至如图所示,深色覆盖部分代表可扩展部分,其他部分(边缘)不可扩展,这样就会解决过度放大图片产生的毛边问题。
5、最后将处理好的图片放入android studio你的工程中的minimap-xhdpi目录。如图:
这里写图片描述

Button的点击变化效果:

当一个button按钮按下时,将会产生一些渲染效果,变色或者边缘变化等,自己做的图片作为button背景不能默认产生该效果,于是:
在drawable目录下新建一个xml文件,命名最好和自己用的button产生联系。如下:
这里写图片描述
代码如下:

<?xml version="1.0" encoding="utf-8"?><selector xmlns:android="http://schemas.android.com/apk/res/android">    <item android:drawable="@mipmap/back_pressed" android:state_pressed="true"/><-将产生渐变要用到的图片放在上面->    <item android:drawable="@mipmap/back_blue"/></selector>

代码演示:

public class MainActivity extends Activity {    private Button btn;    private EditText passwordEdit;    private TextView imageText;    private TextView paintText;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        requestWindowFeature(Window.FEATURE_NO_TITLE);        setContentView(R.layout.layout_login);             imageText = (TextView)findViewById(R.id.image_text);        paintText = (TextView)findViewById(R.id.paint_text);        paintText.setText("968");        paintText.setPaintFlags(Paint.STRIKE_THRU_TEXT_FLAG);        Spanned spanned = Html.fromHtml("这是一个<font color='#ff0000'>附文本</font>,图片<img src='ic_launcher/'>", new Html.ImageGetter() {            @Override            public Drawable getDrawable(String source) {                int id = R.mipmap.ic_launcher;                Class clazz = R.mipmap.class;                try {                //通过映射方法得到图片id                    Field field = clazz.getDeclaredField(source);                    id = field.getInt(clazz);                } catch (NoSuchFieldException e) {                    e.printStackTrace();                } catch (IllegalAccessException e) {                    e.printStackTrace();                }                //插入图片                Drawable drawable = getResources().getDrawable(id);//设置图片大小                drawable.setBounds(0,0,drawable.getMinimumWidth(),drawable.getMinimumHeight());                return drawable;            }        }, null);        imageText.setText(spanned);        btn = (Button)findViewById(R.id.button);        passwordEdit = (EditText)findViewById(R.id.password_edit);        btn.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                passwordEdit.setTransformationMethod(null);                //让password的文本框中的文字可见(本来不可见)            }        });    }}

布局

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"              android:orientation="vertical"              android:layout_width="match_parent"              android:layout_height="match_parent">    <TextView        android:id="@+id/image_text"        android:layout_width="wrap_content"        android:layout_height="wrap_content"/>    <TextView        android:id="@+id/paint_text"        android:layout_width="wrap_content"        android:layout_height="wrap_content"/>    <LinearLayout        android:layout_width="match_parent"        android:layout_height="wrap_content">        <TextView            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="用户名"/>        <EditText            android:layout_width="0dp"            android:layout_height="wrap_content"            android:layout_weight="1"            android:inputType="number"            android:lines="1"            android:hint="您的手机号"/>    </LinearLayout>    <LinearLayout        android:layout_width="match_parent"        android:layout_height="wrap_content">        <TextView            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="密  码"/>        <EditText            android:id="@+id/password_edit"            android:layout_width="0dp"            android:layout_height="wrap_content"            android:layout_weight="1"            android:inputType="numberDecimal"            android:hint="请输入密码"            android:lines="1"            android:password="true"/>    </LinearLayout>    <LinearLayout        android:layout_width="match_parent"        android:layout_height="wrap_content">        <TextView            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="身份证"/>        <EditText            android:layout_width="0dp"            android:layout_height="wrap_content"            android:layout_weight="1"            android:hint="请输入身份证号"            android:lines="1"            android:digits="123456789x"/>    </LinearLayout>    <Button        android:id="@+id/button"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:text="密码可见"        android:textColor="#80ffff00"        android:background="@drawable/button_state"/></LinearLayout>

结果:
这里写图片描述

radioButton

radioButton的效果如下:
这里写图片描述
布局:

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"              android:orientation="vertical"              android:layout_width="match_parent"              android:layout_height="match_parent">        <RadioGroup            android:id="@+id/radio_button"            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:orientation="horizontal"            android:layout_margin="10dp"            android:checkedButton="@+id/man">            <TextView                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:text="性别:"/>            <RadioButton                android:id="@+id/man"                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:text="男"/>            <RadioButton                android:id="@+id/human"                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:text="女"/>            <RadioButton                android:id="@+id/extras"                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:text="其他"/>        </RadioGroup>    <Button        android:id="@+id/button_select"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="提 交 问 题"/></LinearLayout>

java代码

public class MainActivity extends Activity {    private RadioGroup mRadioGroup;    private Button mButtonSelect;    private RadioButton mRadioButton;    private String mSelectButtonName;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        requestWindowFeature(Window.FEATURE_NO_TITLE);        setContentView(R.layout.radio_button_layout);        mRadioGroup = (RadioGroup)findViewById(R.id.radio_button);        mButtonSelect = (Button)findViewById(R.id.button_select);    //RadioButton的点击事件写法,此处只作参考        mRadioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {            @Override            public void onCheckedChanged(RadioGroup group, int checkedId) {                mRadioButton = (RadioButton)findViewById(checkedId);                mSelectButtonName = mRadioButton.getText().toString();                Log.d("radioButton", mSelectButtonName + "被选中");            }        });        mButtonSelect.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                Log.d("selected", "提交" + mSelectButtonName);            }        });    }}
0 0
原创粉丝点击