成都创新互联网站制作重庆分公司

EditText如何在Android项目中使用

EditText如何在Android项目中使用?很多新手对此不是很清楚,为了帮助大家解决这个难题,下面小编将为大家详细讲解,有这方面需求的人可以来学习下,希望你能有所收获。

创新互联是一家以网络技术公司,为中小企业提供网站维护、网站制作、成都网站建设、网站备案、服务器租用、申请域名、软件开发、重庆小程序开发公司等企业互联网相关业务,是一家有着丰富的互联网运营推广经验的科技公司,有着多年的网站建站经验,致力于帮助中小企业在互联网让打出自已的品牌和口碑,让企业在互联网上打开一个面向全国乃至全球的业务窗口:建站联系电话:028-86922220

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);
  }
}

看完上述内容是否对您有帮助呢?如果还想对相关知识有进一步的了解或阅读更多相关文章,请关注创新互联行业资讯频道,感谢您对创新互联的支持。


新闻标题:EditText如何在Android项目中使用
链接地址:http://cxhlcq.com/article/jesijj.html

其他资讯

在线咨询

微信咨询

电话咨询

028-86922220(工作日)

18980820575(7×24)

提交需求

返回顶部