如何从EditText上删除角色?

编程入门 行业动态 更新时间:2024-10-25 08:16:54
本文介绍了如何从EditText上删除角色?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我想知道有没有什么方法,通过它我可以从的EditText 删除特定的字符。我希望通过指定编辑文字的位置,以消除来自的EditText 包机 是有什么样 removeAt()为的EditText ?

I want to know is there any method by which i can remove the specific character from the EditText. I want to remove charter from EditText by specifying position in edit text is there something like removeAt() for EditText?

public class MainActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); final Button b1=(Button)findViewById(R.id.button1); final EditText ed1=(EditText)findViewById(R.id.editText1); b1.setOnClickListener(new View.OnClickListener() { public void onClick(View arg0) { // TODO Auto-generated method stub //i want something like this ed1.removeAt(5); } }); } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.activity_main, menu); return true; } }

附加信息

主要的问题是,我想使文本加粗内的EditText但不是所有的文字。这是设置在大胆的按钮后,输入的文本(粗体是我的切换按钮)。 确定这里是code

the main problem is i want to make text bold inside edittext but not all the text. The text that is typed after setting bold button on(bold is my toggle button). ok here is the code

ed1.setOnKeyListener(new OnKeyListener() { public boolean onKey(View v, int keyCode, KeyEvent event) { // TODO Auto-generated method stub int i = 0; switch(event.getAction()) { case KeyEvent.ACTION_UP: if(bold.isChecked()) { i=ed1.getSelectionStart(); ed1.append(Html.fromHtml("<b>"+ed1.getText().toString().charAt(i-1)+"</b>")); } return true; } return false; } });

问题是,我得到双重性格的一个是正常的字符,然后再大胆的,所以我想通过指定有位置,删除普通字符

the problem is that i get double character's one is normal character and then again the bold one so i want to remove that normal characters by specifying there position

推荐答案

您必须将其更改为字符串,首先是因为gettext的返回编辑,如:

You have to change it to string first because getText returns editable, e.g:

int charIndex; String text = ed1.getText().toString(); text = text.substring(0, charIndex) + text.substring(charIndex+1); ed1.setText(text);

要删除一个字符,并保持粗体字是相同的:

To remove a Character and keep the BOLD characters the same :

ed1.setText((Spanned)ed1.getText().delete(indexStart , indexEnd));

这将从指数indexStart删除字符为indexEnd -1。 例如如果你使用删除(0,1);它会删除第一个字符。

This will remove the Characters from index "indexStart" to "indexEnd -1". e.g. if you use delete(0,1); it will delete the first Character.

我希望帮助:)

更多推荐

如何从EditText上删除角色?

本文发布于:2023-11-25 08:55:26,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1629133.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:角色   EditText

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!