使用的layout文件内容如下:
<EditText
android:id="@+id/edt_url"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="20dip"
android:layout_marginRight="20dip"
android:text=""
android:singleLine="true"
android:ellipsize="none"
android:editable="false"
android:inputType="none"
/>
其中,属性android:ellipsize默认为“end”(Google的文档中未说明),即省略掉内容的后半部分;把它置为“none”,且置属性android:singleline为true,不用设置横向滚动,即可实现文字在只读EdiText中的滚动。

另,对于只读EditText是不需要显示软键盘的。以下代码实现隐藏软键盘:

private void hideIM(View edt){
// try to hide input_method:
try {
InputMethodManager im = (InputMethodManager) getSystemService(Activity.INPUT_METHOD_SERVICE);
IBinder windowToken = edt.getWindowToken();
if(windowToken != null) {
// always de-activate IM
im.hideSoftInputFromWindow(windowToken, 0);
}
} catch (Exception e) {
Log.e("HideInputMethod", "failed:"+e.getMessage());
}
}

private OnFocusChangeListener focus_listener_noIM = new OnFocusChangeListener(){
@Override
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus==true) {
hideIM(v);
}
}
};

private OnTouchListener touch_listener_noIM = new OnTouchListener(){
@Override
public boolean onTouch(View v, MotionEvent event) {
if(event.getAction()==MotionEvent.ACTION_DOWN) {
hideIM(v);
}
return false; // dispatch the event further!
}
};

// 以下是Activity的onCreate()函数的片断:
public void onCreate(Bundle savedInstanceState) {
...
EditText edt_url = (EditText)findViewById(R.id.edt_url);
edt_url.setOnFocusChangeListener(focus_listener_noIM);
edt_url.setOnTouchListener(touch_listener_noIM);
...
}

更多相关文章

  1. android中GridView关于间距的属性值介绍
  2. Android AndroidManifest.xml文件的android:supportsRtl属性详解
  3. 安卓开发38:安卓的滚动条相关属性
  4. RelativeLayout的16种特有属性
  5. Android 属性动画简介
  6. EditText属性简介
  7. android之View属性
  8. android scaleType的属性

随机推荐

  1. 数据库界的 Swagger,一键自动生成 Java 实
  2. 为什么 String 类是不可变的?
  3. 我终于决定要放弃 Postman,选择了牛逼的神
  4. 使用后台进程和 Shutdown Hook 友好地关
  5. ConcurrentHashMap 和 HashTable 的区别
  6. 船新 IDEA 2020.3 插件大合集,个个都是精
  7. 恕我直言,我怀疑你没怎么用过枚举
  8. 昨天你用的 YYYY-MM-dd 被 CTO 捶了吗?
  9. 如何摆脱工具类
  10. Linux性能优化(七)——网络流量监控工具