2015-08-31 36 views
-1

我有一个editText,其背景设置为透明,使其看起来像一个textView。 initial editText如何在改变editText的背景之后将文本移回旧位置?

在Onfocuschanged方法,在重点我设置的背景资源为

ph.setBackgroundResource(android.R.drawable.edit_text); 

after focus

当焦点被删除我想使它看起来像一个TextView了,所以我设置背景为

ph.setBackgroundColor(Color.TRANSPARENT); 

moved text

但editText中的文本保留在前一个背景的位置,并且不会返回到其原始位置。

编辑: 为EDITTEXT

<EditText 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:background="@android:color/transparent" 
     android:text="Medium Text" 
     android:id="@+id/ph" 
     android:layout_alignBottom="@+id/textView12" 
     android:layout_toRightOf="@+id/textView12" 
     android:layout_toEndOf="@+id/textView12" /> 
+0

我该怎么做到这一点? – Isj

回答

0

所以最后用

android:layout_alignBaseline="@+id/textView12" 

代替

android:layout_alignBottom="@+id/textView12" 

基线allignment解决了这个问题实际上alligns 2争夺文本到文本。所以当我将背景从透明改为 android.R.drawable.edit_text时,它会根据文本的位置而不是背景将其分配给texView12。

0

尝试添加XML代码替换

ph.setBackgroundColor(Color.TRANSPARENT);

ph.setBackground(null);

编辑:

将背景设置为透明后,添加以下几行。

RelativeLayout.LayoutParams params= new RelativeLayout.LayoutParams(
      ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT); 
params.addRule(RelativeLayout.ALIGN_BASELINE, R.id.textView12); 
ph.setLayoutParams(params); 
+0

试过了,没有用。 – Isj

+0

用'setBackground(null)'尝试一次。如果它不起作用,请将您的xml布局添加到原始帖子中。 –

+0

嘿谢谢你的回复,它不工作。我已经添加了xml代码。 – Isj

0

设置ph.setBackgroundResource(android.R.drawable.edit_text);您更改EditText的img src字段。 尝试仅使用Drawable进行操作。 变化 ph.setBackgroundResource(android.R.drawable.edit_text)

ph.setBackgroundDrawable(getResources().getDrawable(android.R.drawable.edit_text)); 

然后替换

ph.setBackgroundColor(Color.TRANSPARENT);

ph.setBackgroundDrawable(null);

+0

谢谢,但它没有奏效。 – Isj

+0

好的。看看http://stackoverflow.com/questions/9017903/edittext-that-looks-like-textview-inside-a-listview – Slampy

+0

雅我看到了,我想这将是我的后备,如果我无法弄清楚上面一个。 – Isj

相关问题