EditText如何在Android项目中使用?很多新手对此不是很清楚,为了帮助大家解决这个难题,下面小编将为大家详细讲解,有这方面需求的人可以来学习下,希望你能有所收获。
1.获取光标选中的文字
EditText view = (EditText)findViewById(R.id.edt);
int start = view.getSelectionStart();
int end = view.getSelectionEnd();
//由于选择的位置和你开始选择文字的顺序有关,所以最好重新判断整理一下顺序,免得出错
if (start>end) {
start = start + end ;
end = start - end;
start = start - end;
}
String sub = view .getText() .toString().substring(start, end);
2.设置光标在输入框的位置
在编写应用的时候,如果我们要将光标定位到某个位置,可以采用下面的方法:
CharSequence text = editText.getText();
//Debug.asserts(text instanceof Spannable);
if (text instanceof Spannable) {
Spannable spanText = (Spannable)text;
Selection.setSelection(spanText, text.length());
}
其中的代码text.length()
为你想要设置的位置,此处是设置到文本末尾。
3 在指定位置插入字符串
public class EditTextCopyActivity extends Activity {
/** Called when the activity is first created. */
private EditText et1 ,et2,et3,et4,et5,et6,et7;
private Button bt1,bt2,bt3;
private ClipboardManager clip ;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
initViews();
bt1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
View view = getCurrentFocus();
if (view instanceof EditText) {
EditText et = (EditText)view;
int start = et.getSelectionStart();
int end = et.getSelectionEnd();
if (start>end) {
start = start + end ;
end = start - end;
start = start - end;
}
String sub = et.getText().toString().substring(start, end);
clip.setText(sub);
}
}
});
bt2.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
View view = getCurrentFocus();
if (view instanceof EditText) {
EditText et = (EditText)view;
int start = et.getSelectionStart();
System.out.println(start+"--------------");
String s = clip.getText().toString();
et.getText().insert(start,s);
}
}
});
bt3.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
View view = getCurrentFocus();
if (view instanceof EditText) {
EditText et = (EditText)view;
int start = et.getSelectionStart();
int end = et.getSelectionEnd();
if (start>end) {
start = start + end ;
end = start - end;
start = start - end;
}
String sub = et.getText().toString().substring(start, end);
et.getText().delete(start, end);
clip.setText(sub);
}
}
});
}
private void initViews(){
et1 = (EditText)this.findViewById(R.id.editText1);
et2 =(EditText)this.findViewById(R.id.editText2);
et3 =(EditText)this.findViewById(R.id.editText3);
et4 =(EditText)this.findViewById(R.id.editText4);
et5 =(EditText)this.findViewById(R.id.editText5);
et6 =(EditText)this.findViewById(R.id.editText6);
clip = (ClipboardManager)getSystemService(CLIPBOARD_SERVICE);
bt1=(Button)this.findViewById(R.id.button1);
bt2 = (Button)this.findViewById(R.id.button2);
bt3 = (Button)this.findViewById(R.id.button3);
}
}
看完上述内容是否对您有帮助呢?如果还想对相关知识有进一步的了解或阅读更多相关文章,请关注亿速云行业资讯频道,感谢您对亿速云的支持。
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。