我想设计一个扩展Edittext的类,以便
1)拥有名为“realanswer”的新自定义属性,其中包含字符串值
2)在改变焦点后的这个类中,通过该类中的方法自动(在主活动中定义监听器)检测输入值是否等于“realanswer”!如果答案是真的,请将textcolor设为绿色,否则阅读。 这是我的新班级:
package com.faridi.myapplication;
import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.widget.EditText;
public class MFEditText extends EditText {
public Context mcontext;
public String realanswer;
public MFEditText(Context context, AttributeSet attrs) {
super(context, attrs);
this.mcontext=context;
TypedArray typedArray =
context.obtainStyledAttributes(attrs,R.styleable.answer);
String attr =
typedArray.getString(R.styleable.answer_answerAttribute);
setCustomAttribute(attr);
this.realanswer=attr;
Toast.makeText(MFEditText.this.mcontext,
attr,Toast.LENGTH_LONG).show();
typedArray.recycle();
this.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int
count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before,
int count) {
if(MFEditText.this.getText().toString().
equals(MFEditText.this.realanswer)){
MFEditText.this.setTextColor(Color.GREEN);
}else {
MFEditText.this.setTextColor(Color.RED);
}
Toast.makeText(MFEditText.this.mcontext,
MFEditText.this.realanswer+
" "+MFEditText.this.getText(),Toast.LENGTH_LONG).show();
}
@Override
public void afterTextChanged(Editable s) {
}
});
}
private void setCustomAttribute(String attr) {
realanswer=attr;
}
private String getCustomAttribute() {
return realanswer;
}
}
这是我的attr.xml文件
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="answer">
<attr name="answerAttribute" format="string"/>
</declare-styleable>
</resources>
这就是我在mainlayout中使用它的方式
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:custom="http://schemas.android.com/apk/res-auto"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_margin="25dp"
android:layout_height="match_parent">
<com.faridi.myapplication.MFEditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
custom:answerAttribute="33"
android:id="@+id/mfet"
/>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
它现在有效。问题解决了。
答案 0 :(得分:0)
您可以TextWatcher
EditText
使用TextWatcher addremark_text_watcher = new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
// write your logic here
}
@Override
public void afterTextChanged(Editable s) {
}
};
来检查答案是否重新启用,或者不是这样的
public MyActivity extends AppCompatActivity {
...
}
public class FragmentA extends Fragment implements OnClickListener {
...
private SharedViewModel mSharedViewModel;
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mSharedViewModel = ViewModelProviders.of(getActivity()).get(SharedViewModel.class);
mSharedViewModel.getData().observe(this, value -> {
updateUIValue(value);
});
}
@Override
public void OnClick(View v) {
if (v == dialogButton) {
showDialog();
}
}
@Override
public void OnClick(View v) {
if (v == dialogButton) {
showDialog();
}
}
public void showDialog() {
String diagName = getResources().getString(R.string.dialog_title);
MyDialog myDialog = MyDialog.newInstance(getFragmentAValue());
myDialog.show(getFragmentManager(), diagName);
}
}
public class MyDialog extends DialogFragment implements OnClickListener {
...
private SharedViewModel mSharedViewModel;
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mSharedViewModel = ViewModelProviders.of(getActivity()).get(SharedViewModel.class);
mSharedViewModel.getData().observe(this, value -> {
updateUIValue(value);
});
}
@Override
public void onClick(View view) {
if (view == acceptButton) {
...
mSharedViewModel.setValue(newValue);
}
else if (view == cancelButton) {
dismiss();
}
}
}
public class SharedViewModel extends ViewModel {
final MutableLiveData<Resource<Value>> data = new MutableLiveData<>();
public MutableLiveData<Value> getData() {
return data;
}
public void setValue(Value newValue) {
this.data.setValue(newValue)
}
}