2.Android基础:常见控件----->EditText、Button
2016-09-08 13:34
387 查看
1.EditText控件常用与输入数据,Button控件常用于响应用户点击操作
[b]2.EditText类是TextView的子类,继承了在设置XML文件中的属性和方法。
[/b]
3.在XML布局文件中,可以设置一些属性,下面有一个不同于TextView中的一个属性:inputType
inputType属性设置是输入时启动虚拟键盘的风格
android:inputType="none":不作为任何设置风格
android:inputType="text":输入普通字符
android:inputType="number" 输入数字格式
android:inputType="textPassword" 输入密码格式
android:inputType="phone" 输入拨号键盘
4.[b]EditText控件重要的方法[/b]
getText方法
功能:获取输入框中输入的内容
返回值:输入的字符串内容
5.Button控件重要的方法
setOnClickListener方法
功能:实时监听点击,回应点击事件操作
以下是一个简单的例子
这个是xml文件配置
这个类是一个抽象类,把要实现的方法抽象出来,好处在于可以实现多次复用
实现OncliListener接口用于事件的侦听
这个类是具体执行显示内容
6.EidText输入框中限制输入几个字符问题
1.第一种方式:在xml文件中添加EditText属性maxLength
2.[b]第一种方式:使用InputFilter中LengthFilter方法 [/b]
以下举个例子,进行阐述
这个是xml布局文件
<LinearLayout 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"
tools:context=".MainActivity"
android:orientation="vertical"
>
<!-- 在xml文件中直接设置限制输入几个字符 -->
<EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:maxLength="10"
android:inputType="phone"/>
<EditText
android:id="@+id/editText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="phone"/>
</LinearLayout>
这个类是测试类
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
EditText editText = (EditText) findViewById(R.id.editText2);
editText.setFilters(new InputFilter[]{new InputFilter.LengthFilter(10)});
}
}
第三种方式:实现对TextWatcher接口
这个xml布局文件
<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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<EditText
android:id="@+id/editText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="phone" />
</RelativeLayout>这个类是实现对TextWatcher接口
public class MaxLengthWatcher implements TextWatcher {
private int maxLen = 0;
private EditText editText = null;
public MaxLengthWatcher(int maxLen, EditText editText) {
this.maxLen = maxLen;
this.editText = editText;
<span style="white-space:pre"> </span>}
//文本更改之前调用
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub
}
//文本被更改时调用
@Override
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
}
//文本改变时调用
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
// TODO Auto-generated method stub
Editable editable = editText.getText();
int len = editable.length();
if(len > maxLen)
{
int selEndIndex = Selection.getSelectionEnd(editable);
String str = editable.toString();
//截取新字符串
String newStr = str.substring(0,maxLen);
editText.setText(newStr);
editable = editText.getText();
//新字符串的长度
int newLen = editable.length();
//旧光标位置超过字符串长度
if(selEndIndex > newLen)
{
selEndIndex = editable.length();
}
//设置新光标所在的位置
Selection.setSelection(editable, selEndIndex);
}
}
}这个类是测试类
[b]2.EditText类是TextView的子类,继承了在设置XML文件中的属性和方法。
[/b]
3.在XML布局文件中,可以设置一些属性,下面有一个不同于TextView中的一个属性:inputType
inputType属性设置是输入时启动虚拟键盘的风格
android:inputType="none":不作为任何设置风格
android:inputType="text":输入普通字符
android:inputType="number" 输入数字格式
android:inputType="textPassword" 输入密码格式
android:inputType="phone" 输入拨号键盘
4.[b]EditText控件重要的方法[/b]
getText方法
功能:获取输入框中输入的内容
返回值:输入的字符串内容
5.Button控件重要的方法
setOnClickListener方法
功能:实时监听点击,回应点击事件操作
以下是一个简单的例子
这个是xml文件配置
<LinearLayout 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" tools:context=".MainActivity" android:orientation="vertical" > <EditText android:id="@+id/editText1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/hello_world" android:inputType="phone" /> <LinearLayout android:id="@+id/ll_conversation_edit_menu" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal" android:paddingLeft="30dp" android:paddingRight="30dp" android:paddingTop="5dp" > <Button android:id="@+id/btn1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/editview1" /> <Button android:id="@+id/btn2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/editview2" /> </LinearLayout> <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout>
这个类是一个抽象类,把要实现的方法抽象出来,好处在于可以实现多次复用
实现OncliListener接口用于事件的侦听
public abstract class BaseActivity extends Activity implements OnClickListener { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initView(); initListener(); initData(); } public abstract View initView(); //填充布局 public abstract void initListener(); //侦听 public abstract void initData(); //显示内容 public abstract void processClick(View v); //实现侦听 //按钮监听 @Override public void onClick(View v) { processClick(v); } }
这个类是具体执行显示内容
public class MainActivity extends BaseActivity { private EditText edittext; private TextView textview; private Button btn1; private Button btn2; //填充布局 @Override public View initView() { //使用资源ID edittext = (EditText) findViewById(R.id.editText1); btn1 = (Button) findViewById(R.id.btn1); btn2 = (Button) findViewById(R.id.btn2); textview = (TextView) findViewById(R.id.textView1); return textview; } //侦听 @Override public void initListener() { btn1.setOnClickListener(this); btn2.setOnClickListener(this); } //内容显示 @Override public void initData() { String str = edittext.getText().toString(); textview.setText(str); //实现输入框文本显示 } //实现侦听 @Override public void processClick(View v) { switch (v.getId()) { case R.id.btn1: initData(); break; case R.id.btn2: System.exit(0); //退出界面 break; } } }
6.EidText输入框中限制输入几个字符问题
1.第一种方式:在xml文件中添加EditText属性maxLength
2.[b]第一种方式:使用InputFilter中LengthFilter方法 [/b]
以下举个例子,进行阐述
这个是xml布局文件
<LinearLayout 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"
tools:context=".MainActivity"
android:orientation="vertical"
>
<!-- 在xml文件中直接设置限制输入几个字符 -->
<EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:maxLength="10"
android:inputType="phone"/>
<EditText
android:id="@+id/editText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="phone"/>
</LinearLayout>
这个类是测试类
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
EditText editText = (EditText) findViewById(R.id.editText2);
editText.setFilters(new InputFilter[]{new InputFilter.LengthFilter(10)});
}
}
第三种方式:实现对TextWatcher接口
这个xml布局文件
<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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<EditText
android:id="@+id/editText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="phone" />
</RelativeLayout>这个类是实现对TextWatcher接口
public class MaxLengthWatcher implements TextWatcher {
private int maxLen = 0;
private EditText editText = null;
public MaxLengthWatcher(int maxLen, EditText editText) {
this.maxLen = maxLen;
this.editText = editText;
<span style="white-space:pre"> </span>}
//文本更改之前调用
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub
}
//文本被更改时调用
@Override
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
}
//文本改变时调用
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
// TODO Auto-generated method stub
Editable editable = editText.getText();
int len = editable.length();
if(len > maxLen)
{
int selEndIndex = Selection.getSelectionEnd(editable);
String str = editable.toString();
//截取新字符串
String newStr = str.substring(0,maxLen);
editText.setText(newStr);
editable = editText.getText();
//新字符串的长度
int newLen = editable.length();
//旧光标位置超过字符串长度
if(selEndIndex > newLen)
{
selEndIndex = editable.length();
}
//设置新光标所在的位置
Selection.setSelection(editable, selEndIndex);
}
}
}这个类是测试类
public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); EditText editText = (EditText) findViewById(R.id.editText); editText.addTextChangedListener(new MaxLengthWatcher(10, editText)); } }
相关文章推荐
- Android常见控件之TextView、EditView、Button、Menu
- Android - VGA,ADB,DDMS,安卓开发环境目录结构,安卓目录结构,常见布局,点击事件的几种写法,控件(Button/TextView/ImageView/EditText)
- Android常见控件之TextView、EditView、Button、Menu
- Android基础学习 - 简单控件,EditText 、 Button :
- 1.Android基础:常见控件----->TextView
- Android基本常用控件的介绍TextView EditText Button Menu
- Android常用控件-TextView、EditView、Button、Menu
- <Android 基础(二十二)> EditText 无法显示完全以及尝鲜Android N
- Android02--Android之UI控件TextView+Button+EditText
- Android的常见控件(TextView、EditText、Button、Menu)使用
- Android学习-常见的UI控件 TextView、EditText和ImageView
- Android 开发基础篇——UI——基础控件(TextView、Button、EditText)
- Android自定义组合控件--EditText和Button组合成带有清空EditText内容功能的复合控件
- 4.Android基础:常见控件----->ProgressBar
- UI基础----常见控件的使用之EditText
- 3.Android基础:常见控件----->ImageView
- 5.Android基础:常见控件----->ListView
- <Android 基础(二十二)> EditText 无法显示完全以及尝鲜Android N
- Android 第五课 常用控件的使用方法(TextView、Button、EditView、 ImageView、 ProgressBar、 ProgressDialog等)
- 6.Android基础:常见控件----->SeekBar