EditText常用属性:EditText选取操作
Posted DreamerQL
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了EditText常用属性:EditText选取操作相关的知识,希望对你有一定的参考价值。
话不多说,直接上码:
activity_main.xml
- <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:tools="http://schemas.android.com/tools"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content" >
- <RelativeLayout
- android:layout_width="fill_parent"
- android:layout_height="wrap_content" >
- <EditText
- android:id="@+id/edit"
- android:layout_width="fill_parent"
- android:layout_height="40dp"
- android:hint="请在这里输入文本..."
- android:inputType="text" />
- <Button
- android:id="@+id/getAll"
- android:layout_width="fill_parent"
- android:layout_height="40dp"
- android:layout_below="@+id/edit"
- android:text="获取输入框中的值" />
- <Button
- android:id="@+id/getSelect"
- android:layout_width="fill_parent"
- android:layout_height="40dp"
- android:layout_below="@+id/getAll"
- android:text="获取被选中的文本" />
- <Button
- android:id="@+id/selectAll"
- android:layout_width="fill_parent"
- android:layout_height="40dp"
- android:layout_below="@+id/getSelect"
- android:text="全选" />
- <Button
- android:id="@+id/selectFrom"
- android:layout_width="wrap_content"
- android:layout_height="40dp"
- android:layout_below="@+id/selectAll"
- android:text="从第几个字符开始选?" />
- <EditText
- android:id="@+id/fromNumber"
- android:layout_width="fill_parent"
- android:layout_height="40dp"
- android:layout_below="@+id/selectAll"
- android:layout_toRightOf="@+id/selectFrom"
- android:inputType="date"
- android:hint="在这里输入.." />
- <TextView
- android:id="@+id/tip"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:layout_below="@+id/selectFrom"
- android:text="提示:焦点必须放在输入框才能够选中"
- />
- </RelativeLayout>
- </ScrollView>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<EditText
android:id="@+id/edit"
android:layout_width="fill_parent"
android:layout_height="40dp"
android:hint="请在这里输入文本..."
android:inputType="text" />
<Button
android:id="@+id/getAll"
android:layout_width="fill_parent"
android:layout_height="40dp"
android:layout_below="@+id/edit"
android:text="获取输入框中的值" />
<Button
android:id="@+id/getSelect"
android:layout_width="fill_parent"
android:layout_height="40dp"
android:layout_below="@+id/getAll"
android:text="获取被选中的文本" />
<Button
android:id="@+id/selectAll"
android:layout_width="fill_parent"
android:layout_height="40dp"
android:layout_below="@+id/getSelect"
android:text="全选" />
<Button
android:id="@+id/selectFrom"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:layout_below="@+id/selectAll"
android:text="从第几个字符开始选?" />
<EditText
android:id="@+id/fromNumber"
android:layout_width="fill_parent"
android:layout_height="40dp"
android:layout_below="@+id/selectAll"
android:layout_toRightOf="@+id/selectFrom"
android:inputType="date"
android:hint="在这里输入.." />
<TextView
android:id="@+id/tip"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/selectFrom"
android:text="提示:焦点必须放在输入框才能够选中"
/>
</RelativeLayout>
</ScrollView>
MainActivity.java
- package com.wirelessqa.edittext;
- import android.app.Activity;
- import android.os.Bundle;
- import android.text.Editable;
- import android.text.Selection;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.widget.Button;
- import android.widget.EditText;
- import android.widget.Toast;
- /**
- * EditText选取操作
- * From:http://www.csdn.net/blog/wirelessqa
- * @author bixiaopeng 2013-2-3 下午9:41:57
- */
- public class MainActivity extends Activity
- private EditText edit = null;
- private EditText edit_selectFrom = null;
- private Button btn_getEdit = null;
- private Button btn_getSelect = null;
- private Button btn_selectAll = null;
- private Button btn_selectFrom = null;
- /* (non-Javadoc)
- * @see android.app.Activity#onCreate(android.os.Bundle)
- */
- @Override
- protected void onCreate(Bundle savedInstanceState)
- // TODO Auto-generated method stub
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- edit = (EditText) findViewById(R.id.edit);
- edit_selectFrom = (EditText) findViewById(R.id.fromNumber);
- btn_getEdit = (Button) findViewById(R.id.getAll);
- btn_getSelect = (Button) findViewById(R.id.getSelect);
- btn_selectAll = (Button) findViewById(R.id.selectAll);
- btn_selectFrom = (Button) findViewById(R.id.selectFrom);
- edit.setText("老毕的博客:http://www.csdn.net/blog/wirelessqa");
- //监听获取输入框中的所有文本
- btn_getEdit.setOnClickListener(new OnClickListener()
- @Override
- public void onClick(View v)
- String editText = edit.getText().toString();
- Toast.makeText(MainActivity.this, editText, Toast.LENGTH_LONG).show();
- );
- //监听获取选中的文本
- btn_getSelect.setOnClickListener(new OnClickListener()
- @Override
- public void onClick(View v)
- int startSelect = edit.getSelectionStart();
- int endSelect = edit.getSelectionEnd();
- String selectText = edit.getText().subSequence(startSelect, endSelect).toString();
- Toast.makeText(MainActivity.this, selectText, Toast.LENGTH_LONG).show();
- );
- //全选
- btn_selectAll.setOnClickListener(new OnClickListener()
- @Override
- public void onClick(View v)
- setEditFocus(edit);
- edit.selectAll();
- );
- //从第几个字符开始选择
- btn_selectFrom.setOnClickListener(new OnClickListener()
- @Override
- public void onClick(View v)
- //从输入框中获取值
- int fromNumber = 0;
- try
- fromNumber = Integer.valueOf(edit_selectFrom.getText().toString());
- catch (Exception e)
- e.printStackTrace();
- fromNumber = 0;
- Toast.makeText(MainActivity.this, "请输入大于0的数字", Toast.LENGTH_SHORT).show();
- int length = edit.getText().length()-1;//输入框中文本的长度
- if(fromNumber !=0 && fromNumber<length)
- Editable editable = edit.getText();
- setEditFocus(edit);
- Selection.setSelection(editable,fromNumber,editable.length());
- else
- Toast.makeText(MainActivity.this, "输入的数字要小于"+length, Toast.LENGTH_SHORT).show();
- );
- /**
- * 将焦点放在输入框中
- * 如果想要选中输入框中的文本必须要将焦点放在输入框中
- * 如果想要焦点在输入框中必须设置下面三个方法
- * @param editText
- */
- private void setEditFocus(EditText editText)
- editText.setFocusable(true);
- editText.setFocusableInTouchMode(true);
- editText.requestFocus();
package com.wirelessqa.edittext;
import android.app.Activity;
import android.os.Bundle;
import android.text.Editable;
import android.text.Selection;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
/**
* EditText选取操作
* From:http://www.csdn.net/blog/wirelessqa
* @author bixiaopeng 2013-2-3 下午9:41:57
*/
public class MainActivity extends Activity
private EditText edit = null;
private EditText edit_selectFrom = null;
private Button btn_getEdit = null;
private Button btn_getSelect = null;
private Button btn_selectAll = null;
private Button btn_selectFrom = null;
/* (non-Javadoc)
* @see android.app.Activity#onCreate(android.os.Bundle)
*/
@Override
protected void onCreate(Bundle savedInstanceState)
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
edit = (EditText) findViewById(R.id.edit);
edit_selectFrom = (EditText) findViewById(R.id.fromNumber);
btn_getEdit = (Button) findViewById(R.id.getAll);
btn_getSelect = (Button) findViewById(R.id.getSelect);
btn_selectAll = (Button) findViewById(R.id.selectAll);
btn_selectFrom = (Button) findViewById(R.id.selectFrom);
edit.setText("老毕的博客:http://www.csdn.net/blog/wirelessqa");
//监听获取输入框中的所有文本
btn_getEdit.setOnClickListener(new OnClickListener()
@Override
public void onClick(View v)
String editText = edit.getText().toString();
Toast.makeText(MainActivity.this, editText, Toast.LENGTH_LONG).show();
);
//监听获取选中的文本
btn_getSelect.setOnClickListener(new OnClickListener()
@Override
public void onClick(View v)
int startSelect = edit.getSelectionStart();
int endSelect = edit.getSelectionEnd();
String selectText = edit.getText().subSequence(startSelect, endSelect).toString();
Toast.makeText(MainActivity.this, selectText, Toast.LENGTH_LONG).show();
);
//全选
btn_selectAll.setOnClickListener(new OnClickListener()
@Override
public void onClick(View v)
setEditFocus(edit);
edit.selectAll();
);
//从第几个字符开始选择
btn_selectFrom.setOnClickListener(new OnClickListener()
@Override
public void onClick(View v)
//从输入框中获取值
int fromNumber = 0;
try
fromNumber = Integer.valueOf(edit_selectFrom.getText().toString());
catch (Exception e)
e.printStackTrace();
fromNumber = 0;
Toast.makeText(MainActivity.this, "请输入大于0的数字", Toast.LENGTH_SHORT).show();
int length = edit.getText().length()-1;//输入框中文本的长度
if(fromNumber !=0 && fromNumber<length)
Editable editable = edit.getText();
setEditFocus(edit);
Selection.setSelection(editable,fromNumber,editable.length());
else
Toast.makeText(MainActivity.this, "输入的数字要小于"+length, Toast.LENGTH_SHORT).show();
);
/**
* 将焦点放在输入框中
* 如果想要选中输入框中的文本必须要将焦点放在输入框中
* 如果想要焦点在输入框中必须设置下面三个方法
* @param editText
*/
private void setEditFocus(EditText editText)
editText.setFocusable(true);
editText.setFocusableInTouchMode(true);
editText.requestFocus();
本文链接:http://blog.csdn.net/wirelessqa/article/details/8567702
转载声明:本站文章若无特别说明,皆为原创,转载请注明来源:http://blog.csdn.net/wirelessqa,谢谢!^^
以上是关于EditText常用属性:EditText选取操作的主要内容,如果未能解决你的问题,请参考以下文章
一起Talk Android吧(第三百零四回:EditText的其它常用属性)