[英]Change TextSize of focussed EditText in android
Can any of you show me the best way to change the TextSize attribute of an EditText just when the user has it selected (is writing)? 你们中的任何人都可以向我展示更改EditText的TextSize属性的最佳方法吗,仅当用户选择它(正在编写)时?
This is what i accomplished so far: 这是我到目前为止完成的工作:
I've two EditText in a linear layout. 我有两个线性布局的EditText。
xml: xml:
<EditText
android:textSize="20dp"
android:onClick="makeTextViewFocusedBigger"/>
<EditText
android:textSize="20dp"
android:onClick="makeTextViewFocusedBigger"/>
activity: 活动:
public void makeTextViewFocusedBigger(View view) {
((EditText) view).setTextSize(TypedValue.COMPLEX_UNIT_DIP, 25);
}
When the user select it it does become bigger, but obviously it does not go back to the original size when the user stop using it. 当用户选择它时,它确实会变大,但是当用户停止使用它时,显然它不会返回到原始大小。
What would you do to realize this function? 您将如何实现此功能?
Use View.OnFocusChangedListener
使用
View.OnFocusChangedListener
myEditText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
// Change size based on hasFocus boolean
}
});
Use setOnFocusChangeListener
使用
setOnFocusChangeListener
Like This : 像这样 :
Your_Edittext.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
if(hasFocus)
{
Your_Edittext.setTextSize(25); //increased size
}
else
{
Your_Edittext.setTextSize(15); //normal size
}
}
});
Do following : 请执行以下操作:
XML: XML:
<EditText
android:textSize="20dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/edit_text_first"/>
<EditText
android:textSize="20dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/edit_text_second"/>
Activity OnCreate
: 活动
OnCreate
:
EditText editFirst=findViewById(R.id.edit_first)
EditText editSecond=findViewById(R.id.edit_second)
editFirst.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
// Change size of text inside 1st EditText
}
});
editSecond.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
// Change size of text inside 2nd EditText
}
});
To implement it more general : 为了更通用地实现它:
public class MainActivity extends AppCompatActivity implements View.OnFocusChangeListener{
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editFirst.setOnFocusChangeListener(this);
editSecond.setOnFocusChangeListener(this);
}
@Override
public void onFocusChange(View v, boolean hasFocus)
{
// Change text size of every EditText or for selective EditText. As You want
}
}
You need to implement View.OnFocusChangeListener(). 您需要实现View.OnFocusChangeListener()。 This method is called when the focus state of a view has changed.
视图的焦点状态已更改时,将调用此方法。
editText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
if(hasFocus)
{
// Change size when editText has focus
editText.setTextSize(30);
}else
{
// Change size when editText doesn't has focus
editText.setTextSize(15);
}
}
});
You need to addFocusChangedListener
to your EditTexts
. 您需要将
addFocusChangedListener
到您的EditTexts
。 In them you can see if they are focused or not. 在其中,您可以查看他们是否专注。
You have to implement View.OnFocusChangeListener()
method , set the textview size according you want in onFocusChange()
您必须实现
View.OnFocusChangeListener()
方法,根据需要在onFocusChange()
中设置onFocusChange()
大小
myEditText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
// set the text size here
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.