大家好,请帮助一个刚开始学习android的Noob。 基本上我有一个具有TextView的活动,在每个Textview上我都设置onfocusListener,一切都很完美。 这是布局示例country UK的代码
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="50.0dip"
android:background="@drawable/button5_background">
<ImageView
android:layout_width="65dp"
android:layout_height="104dp"
android:id="@+id/afrflag"
android:layout_gravity="center_vertical"
android:src="@drawable/ukflag" />
<TextView
android:background="@drawable/custom_selector"
android:id="@+id/uk"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="@string/uk"
android:textColor="@color/white"
android:textSize="18.0sp"
android:focusable="true"
android:focusableInTouchMode="true" />
</LinearLayout>
这里是英国的代码OnFocusChangeListener
TextView btnuk = (TextView) findViewById(R.id.uk);
btnuk.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
// TODO Auto-generated method stub
if (hasFocus) {
videoList.clear();
fetchVideouk();
}
}
});
所以我决定玩游戏并制作arrays.xml并将所有国家放在那里,然后尝试使用listview而不是TextView,我必须创建这么多行作为按钮。
这是我的array.xml
<string-array
name="countryView">
<item>@string/uk</item>
<item>@string/frn</item>
<item>@string/ita</item>
<item>@string/ger</item>
</string-array>
</resources>
和listview的xml
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/custom_selector">
<ListView
android:background="@drawable/button5_background"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/country_names"
android:entries="@array/countryView"
/>
</LinearLayout>
使用上面的代码我看到国家名称加载在我的视图上,但我不知道如何在每个项目(国家/地区)上设置onfocus
ListView listView;
String[] uk;
uk = getResources().getStringArray(R.array.countryView);
listView = (ListView) findViewById(R.id.country_names);
ArrayAdapter<String> adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, uk);
listView.setAdapter(adapter);
listView.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
// TODO Auto-generated method stub
if (hasFocus) {
videoList.clear();
fetchVideosuk();
}
}
});
如何为下一个项目设置onfocus呢?例如ger,Ita等我确定这不正确,但我不明白怎么做?
答案 0 :(得分:0)
您必须为此创建自定义适配器。
<强> row_layout.xml 强>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/textView"
android:textSize="20sp"/>
</LinearLayout>
<强> MyAdapter.java 强>
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
public class MyAdapter extends ArrayAdapter<String> {
public MyAdapter(Context context, String[] values){
super(context, R.layout.row_layout, values);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = LayoutInflater.from(getContext());
View view = inflater.inflate(R.layout.row_layout, parent, false);
TextView textView = (TextView) view.findViewById(R.id.textView);
textView.setText(getItem(position));
return view;
}
}
您可以通过MainActivty.java中的以下代码执行此操作:
uk = getResources().getStringArray(R.array.countryView);
listView = (ListView) findViewById(R.id.country_names);
ArrayAdapter<String> adapter = new MyAdapter(this, uk);
listView.setAdapter(adapter);
for (int i=0; i<listView.getChildCount(); i++) {
TextView textView = (TextView) listView.getChildAt(i).findViewById(R.id.textView);
textView.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View view, boolean hasFocus) {
if (hasFocus) {
// do something //
}
}
});
}
我希望这有用。