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); } }); }}
- android中TextView、TextEdit和Button的用法以及draw9patch
- android中Button和TextView的关系
- Android中TextView与Button的区别
- Android中TextView与Button的区别
- Android TextView和Button的使用方法
- Android中Button的用法
- Android的控件以及属性(TextView,Button)
- android中的draw9patch用法
- android 中 TextView的用法
- Android学习--TextView和Button
- android draw9patch的制作和使用总结
- android draw9patch的制作和使用总结
- android draw9patch的制作和使用总结
- Android教程三:Android button 和 textview的学习
- Android中Button的基本用法
- android,draw9patch的使用
- Android 的 draw9patch背景
- Android 的 draw9patch背景
- ZOJ 3878 Convert QWERTY to Dvorak
- javascript深入理解js闭包
- 深入理解JavaScript系列(35):设计模式之迭代器模式
- java集合
- jQuery学习笔记(三)
- android中TextView、TextEdit和Button的用法以及draw9patch
- iPhone屏幕尺寸
- Sliding Window Maximum
- 多线程 - 06.GCD概述
- nyoj14会场安排问题【贪心】~
- Proxy 时空隧道
- linux下判断文件和目录是否存在[总结]
- 深入理解JavaScript系列(36):设计模式之中介者模式
- hash 冲突处理