我是android新手。我试图在焦点上更改edittext的边框。 Edittext存在于非活动类中。我使用了drawable资源和setOnFocusChange方法来设置边框。但它不起作用。
这是我的代码。 ViewMap.java
public class ViewMap extends Fragment {
Context mContext;
public ViewMap(Context mContext)
{
this.mContext = mContext;
}
MapView mapView;
GoogleMap map;
@Override
public View onCreateView(LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.view_map, container, false);
EditText txt = (EditText) v.findViewById(R.id.search);
txt.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View view, boolean hasFocus) {
if(hasFocus){
view.setBackgroundResource(R.drawable.focus_border_style);
}
else
{
view.setBackgroundResource(R.drawable.lost_focus_border_style);
}
}
});
}
@Override
public void onResume() {
super.onResume();
}
@Override
public void onDestroy() {
super.onDestroy();
}
@Override
public void onLowMemory() {
super.onLowMemory();
}
}
view_map.xml
<EditText android:id="@+id/search"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="Search location"/>
<Button android:id="@+id/searchButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Search"/>