DSl*_*r64 5 java android lost-focus
有没有聪明的方法来避免将第一块代码重复十几次?第二个块与第一个块的形式相同,我还有几个具有相同形式的块。我正在考虑一系列EditText字段(好主意?坏 [为什么?]?)但是有没有一种全局方法可以让一个块捕获所有焦点变化?
txtExclude.setOnFocusChangeListener(new View.OnFocusChangeListener() {
public void onFocusChange(View v, boolean hasFocus)
{
if (!hasFocus)
if (txtExclude.getText().length() == 0)
txtExclude.setBackgroundColor(Color.WHITE);
}});
txtRequired.setOnFocusChangeListener(new View.OnFocusChangeListener() {
public void onFocusChange(View v, boolean hasFocus)
{
if (!hasFocus)
if(txtRequired.getText().length() == 0)
txtRequired.setBackgroundColor(Color.WHITE);
}});
Run Code Online (Sandbox Code Playgroud)
编辑
第一个答案的非工作实现:
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends Activity implements View.OnFocusChangeListener
{
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState); // call superclass's version
setContentView(R.layout.activity_main); // inflate the GUI
final EditText txtPattern = (EditText) findViewById(R.id.txtPattern);
final EditText txtLegal = (EditText) findViewById(R.id.txtLegal);
final EditText txtExclude = (EditText) findViewById(R.id.txtExclude);
final EditText txtRequired = (EditText) findViewById(R.id.txtRequired);
EditText txtLetters = (EditText) findViewById(R.id.txtLetters);
} // end method onCreate
@Override
public void onFocusChange(View v, boolean hasFocus) {
return; // breakpoint here **********************
}
} // end class MainActivity
Run Code Online (Sandbox Code Playgroud)
无论EditText在调试过程中获得或失去焦点,都没有到达断点。
(在此处放置带有 8 个空格的行将格式化代码的第一行。)
findViewById(R.id.yourRootContainer).getViewTreeObserver().addOnGlobalFocusChangeListener
(
new ViewTreeObserver.OnGlobalFocusChangeListener()
{
@Override
public void onGlobalFocusChanged(View oldFocus, View newFocus)
{
whatever();
}
}
);
Run Code Online (Sandbox Code Playgroud)
您可以使用自定义焦点侦听器,使用视图 ID 进行区分。以下是示例代码:
private class MyFocusChangeListener implements
View.OnFocusChangeListener
{
@Override
public void onFocusChange(View view, boolean hasFocus)
{
if (view.getId() == R.id.my_view_id)
{
doSomethingHere();
}
}
}
Run Code Online (Sandbox Code Playgroud)并将其用作:
myView1.setOnFocusChangeListener(new MyFocusChangeListener());
Run Code Online (Sandbox Code Playgroud)
这是另一种方法。
通过对 EditText 进行子类化,在包中创建一个 CustomEditext 类,如下所示
让你的包裹是com.android.app
public class CustomEditText extends EditText {
public CustomEditText(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
protected void onFocusChanged(boolean focused, int direction,
Rect previouslyFocusedRect) {
if (!focused)
if (getText().length() == 0)
setBackgroundColor(Color.WHITE);
super.onFocusChanged(focused, direction, previouslyFocusedRect);
}
}
Run Code Online (Sandbox Code Playgroud)
您可以在布局中使用此 customEditText,如下所示
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<com.android.app.CustomEditText
android:id="@+id/com.example.customedittext1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:hint="Name"
android:inputType="textPersonName" >
</com.android.app.CustomEditText>
<com.android.app.CustomEditText
android:id="@+id/edEmail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:hint="Email"
android:inputType="textPersonName" >
</com.android.app.CustomEditText>
<com.android.app.CustomEditText
android:id="@+id/edContact"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:hint="Contact"
android:inputType="textPersonName" >
</com.android.app.CustomEditText>
Run Code Online (Sandbox Code Playgroud)
您不需要在每个编辑字段上设置 onFocusChangedListner()。
希望这可以帮助
| 归档时间: |
|
| 查看次数: |
3908 次 |
| 最近记录: |