Android——文本类控件
【注】
TextView tv1=(TextView)this.findViewById(R.id.textView1);//使用findViewById的方法在JAVA中建立控件对象并与界面连接tv1.setBackgroundColor(android.graphics.Color.parseColor("#fd8d8d"));//使用android.graphics.color.parseColor("#000000")获取颜色int
Toast.makeText(MainActivity."登录成功!",Toast.LENGTH_SHORT).show();小提示框,其中length_short表示停留时间,短
使用//和// 标签可以折叠指定代码块
android:singleLine="true"//设置文本框单行,不能换行输入
【TextView】
常用XML属性及说明
XML属性 | 相关方法 | 描述 |
Android:autoLink | setAutoLinkMask(int) | 是否将符合指定格式的文本转换为可单击的链接 |
Android:cursorVisible | setCursorVisible(Boolean) | 设置该文本框的光标是否可见 |
Android:editable |
| 是否可编辑 |
Android:gravity | setGravity(int) | 设置文本对齐方式 |
Android:hint | setHint(int) | 内容为空时,默认显示提示文本 |
Android:password | setTransformationMethod(…) | 设置为密码框 |
Android:selectAllOnFocus | setSelectAllOnFocus(Boolean) | 获得焦点时,是否全选 |
Android:singleLine | setTransformationMethod() | 是否为单行 |
Android:text | setText(CharSequence) | 文本内容 |
Android:textColor | setTextColor(ColorStateList) | 文本颜色 |
Android:textSize | setTextSize(float) | 文本大小 |
Android:textStyle | setTypeface(Typeface) | 文本字体风格 |
Android:textface | setTypeface(Typeface) | 文本字体 |
【示例1】
<?xml version="1.0" encoding="utf-8"?>
protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); TextView tv=(TextView)this.findViewById(R.id.textview); tv.setText("我是TextView"); }
【例2】
TextView tv1=(TextView)this.findViewById(R.id.textView1);//使用findViewById的方法在JAVA中建立控件对象并与界面连接 tv1.setText("设置文字背景色"); tv1.setBackgroundColor(android.graphics.Color.parseColor("#fd8d8d"));//使用android.graphics.color.parseColor("#000000")获取颜色int TextView tv2=(TextView)this.findViewById(R.id.textView2); tv2.setText("设置文字颜色"); tv2.setTextColor(Color.BLUE);
【EditText类】
继承自TextView,其属性方法与TextView相似。
//EditText部分Button btnlog=(Button)findViewById(R.id.button);Button btnclear=(Button)findViewById(R.id.button2);final EditText euid=(EditText)findViewById(R.id.editText);final EditText emima=(EditText)findViewById(R.id.editText2);btnlog.setOnClickListener( new View.OnClickListener() { @Override public void onClick(View view) { String struid=euid.getText().toString().trim(); String strmima=emima.getText().toString().trim(); if(struid.equals("admin")&&strmima.equals("123"))//字符串判断,要用equals { Toast.makeText(MainActivity.this,"登录成功!",Toast.LENGTH_SHORT).show(); //SHORT和LONG表示提示框停留时间长短 } else { Toast.makeText(MainActivity.this,"用户名admin,密码123!",Toast.LENGTH_LONG) .show(); } } });btnclear.setOnClickListener( new View.OnClickListener() { @Override public void onClick(View view) { euid.setText(""); emima.setText(""); } });//
【AutoCompleteTextView类】
自动跳还是文本框,继承自EditText类,另外支持的属性主要有。另外还可以设置下拉菜单的宽度、背景灯。
Android:completionHint | setCompletionHint(CharSequence) | 出现在下拉菜单中的提示标题 |
Android: | setThreshold(int) | 至少输入几个字符才会显示 |
Android:dropDownHeight | setDropDownHeight(int) | 设置下拉菜单的高度 |
//AutoCompleteTextView部分String[] COUNTRIES={"China","Russia","Germany","Ukraine","Belarus","China1","China2","USA"};ArrayAdapter adapter=new ArrayAdapter(this,R.layout.support_simple_spinner_dropdown_item,COUNTRIES);AutoCompleteTextView autoComplete=(AutoCompleteTextView)findViewById(R.id.autoCompleteTextView);autoComplete.setAdapter(adapter);autoComplete.setThreshold(1);//
更多相关文章
- TextView如何高亮显示文本
- android最简单的发送文本短信的demo
- Design各控件的搭配使用2
- 基于ViewFlipper实现的自定义新手指引控件.
- 分享Android仿刮奖效果控件
- Android 属性动画实现的扇形菜单效果
- Android用属性动画拖动view到任意位置
- android 控件跟随手指移动
- android 自定义控件实现波浪效果(八)