我需要在未聚焦时动态更改提示颜色属性。 是否可以动态更改提示颜色(不处于聚焦状态)。
实际设计
和我的xml
当它没有聚焦时,我无法以编程方式更改提示颜色属性。 我被卡住了。 有可能吗? 请帮我。 这是我的代码 -
btnSignUp.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
final String email = emailEditText.getText().toString();
final String pass = passEditText.getText().toString();
if (email.isEmpty()) {
inputLayoutEmail.setHint("email is required to login");
inputLayoutEmail.setHintTextAppearance(R.style.EditTextLayout);
inputLayoutEmail.setError(" ");
emailEditText.setHintTextColor(getResources().getColor(R.color.primary_dark));
}
if (pass.isEmpty()) {
inputLayoutPassword.setHint("password is required to login");
passEditText.setHintTextColor(getResources().getColor(R.color.primary_dark));
inputLayoutPassword.setHintTextAppearance(R.style.EditTextLayout);
inputLayoutPassword.setError(" ");
inputLayoutPassword.clearFocus();
passEditText.setHint("Password is required to login");
passEditText.setHintTextColor(Color.RED);
}
答案 0 :(得分:0)
使用此代码可能会对您有所帮助。
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (editText1.getText().toString().length() == 0) {
editText1.setHintTextColor(Color.RED);
}
if (editText2.getText().toString().length() == 0) {
editText2.setHintTextColor(Color.RED);
}
}
});
答案 1 :(得分:0)
editText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View view, boolean b) {
if (!b) {
editText.setHintTextColor(getResources().getColor(R.color.red));
} else {
// set which color you want when edittext is focused
}
});
或者您也可以使用主题来设置颜色
<style name="InputLayoutTheme" parent="Widget.Design.TextInputLayout">
// Whenever input layout is un-focused, below is the color for label
<item name="android:textColorHint">@color/dark_gray</item>
// Whenever input layout is un-focused, below is the underline color
<item name="colorControlNormal">@color/dark_gray</item>
<!-- Whenever input layout is focused, below is the color for label, cursor & underline-->
<item name="colorControlActivated">@color/button_text_blue2</item>
</style>
答案 2 :(得分:0)
请尝试此代码。 当您单击“登录”并且如果该字段为空时,它会将提示文本颜色更改为“红色”。 当您单击以在edittext中编写任何内容时,它会将提示文本颜色重新更改为BLACK。 希望有所帮助..
这是我的活动 -
public class Main2Activity extends AppCompatActivity implements View.OnFocusChangeListener{
EditText emailEditText, passEditText;
Button btnSignUp;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.amin);
emailEditText = (EditText) findViewById(R.id.edt1);
passEditText = (EditText) findViewById(R.id.edt2);
btnSignUp = (Button) findViewById(R.id.btnSignUp);
emailEditText.setOnFocusChangeListener(this);
passEditText.setOnFocusChangeListener(this);
btnSignUp.setOnFocusChangeListener(this);
btnSignUp.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
final String email = emailEditText.getText().toString();
final String pass = passEditText.getText().toString();
if (email.isEmpty()) {
emailEditText.setHintTextColor(Color.RED);
}
if (pass.isEmpty()) {
passEditText.setHintTextColor(Color.RED);
passEditText.setHint("Password is required to login");
passEditText.setHintTextColor(Color.RED);
}
}
});
}
@Override
public void onFocusChange(View v, boolean hasFocus) {
if(v instanceof EditText)
{
((EditText)v).setHintTextColor(Color.BLACK);
v.requestLayout();
v.refreshDrawableState();
}
}
}
和我的XML -
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
android:id="@+id/edt1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="PLease enter Id"
android:focusable="true"
android:focusableInTouchMode="true"
android:textColor="@color/black"/>
<EditText
android:id="@+id/edt2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="PLease enter Password"
android:focusable="true"
android:focusableInTouchMode="true"
android:textColor="@color/black" />
<Button
android:id="@+id/btnSignUp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Login"
/>
</LinearLayout>