0

你好傢伙請幫助一個Noob這裏,剛剛開始學習Android。 基本上我有一個活動,有TextView和每個Textview我設置onfocusListener和一切工作完美。 這裏是佈局例如國家代碼英國如何ListView OnFocusChangeListener對數組列表項xml

<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並把所有的國家,去嘗試列表視圖而不是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> 

,並與上面的代碼我看到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> 

國名上我的看法加載,但我不知道如何設置每個項目(國家)的焦點

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(); 
      } 
     } 

    }); 

然後如何設置下一個項目的焦點?例如ger,Ita等我相信這是不正確的,但我不明白如何?

+0

你可以把一些截圖定義你想要達到什麼? – EEJ

回答

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 // 
      } 
     } 
    }); 
} 

希望這很有幫助。

+0

感謝您的快速回復,但爲什麼編輯文本我問listview和焦點不點擊也可以顯示一個例子,請關注焦點爲英國和德國,所以我可以理解它是如何工作的,我沒有問題,當我做列表爲textView,但它的列表來自數組xml請看我的代碼和抱歉的不好解釋。 –

+0

親愛的感謝您的回覆只是粘貼,並遵循你的步驟,但你在這裏intertext edittext這是給我新的錯誤,因爲我沒有任何edittext也可以顯示下一個焦點項目的例子示例ger和frn(only)no需要點擊這裏,並沒有editText請。 –

+0

嗨,我還想說,我所有的工作都是public void onFocusChange {View v,boolean hasFocus} // TODO自動生成方法存根 if(hasFocus){ videoList.clear(); fetchalleduk(); } }所以我不使用editText或onclick這裏它只是提取videolist抱歉誤解,但我仍然是一個初學者。 –