EditText常用屬性【三】:EditText選取操作
阿新 • • 發佈:2019-02-18
話不多說,直接上碼:
activity_main.xml
[html] view plain copy print?- <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
- android:layout_height="wrap_content" >
- <EditText
- android:id="@+id/edit"
- android:layout_width="fill_parent"
- android:layout_height="40dp"
- android:hint="請在這裡輸入文字..."
- android:inputType="text" />
- <
- 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
- 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
[java] view plain copy print?- 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,謝謝!^^