2017-09-14 74 views
0

我有一個ExpandableListView他們的孩子是四LinearLayoutRelativeLayout A S包含TextViews這將拉動回答問題,並分配這個問題從1-4的得分(壞,平庸的,好的和世界一流的),最後還有一個EditText讓用戶留下他們爲什麼對評分問題進行評分的評論。當用戶單擊樂譜框時,背景會更改顏色以突出顯示所選樂譜,並將其他樂譜框顏色設置恢復爲其默認顏色以顯示不再選擇它們。下面是我的Java來說明其工作原理:點擊EditText上導致按鈕的onClick()函數不行

// ChildView views 
    badScoreView = convertView.findViewById(R.id.qwedit_main_badView); 
    mediocreScoreView = convertView.findViewById(R.id.qwedit_main_mediocreView); 
    goodScoreView = convertView.findViewById(R.id.qwedit_main_goodView); 
    worldclassScoreView = convertView.findViewById(R.id.qwedit_main_wcView); 
    badScoreTextView = (TextView) convertView.findViewById(R.id.qwedit_main_badTextView); 
    mediocreScoreTextView = (TextView) convertView.findViewById(R.id.qwedit_main_mediumTextView); 
    goodScoreTextView = (TextView) convertView.findViewById(R.id.qwedit_main_goodTextView); 
    worldClassScoreTextView = (TextView) convertView.findViewById(R.id.qwedit_main_worldclassTextView); 
    questionCommentEditText = (EditText) convertView.findViewById(R.id.qwedit_main_commentEditText); 

    // TODO: TextView score subcategory setters 

    badScoreView.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      // TODO: add logic to change score 
      subCategoryScore = 1; 
      badScoreView.setBackgroundColor(Color.parseColor("#eca9a7")); 
      mediocreScoreView.setBackgroundColor(Color.parseColor("#f0ad4e")); 
      goodScoreView.setBackgroundColor(Color.parseColor("#5cb85c")); 
      worldclassScoreView.setBackgroundColor(Color.parseColor("#0275d8")); 
      questionCommentEditText.clearFocus(); 
     } 
    }); 

    mediocreScoreView.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      // TODO: add logic to change score 
      subCategoryScore = 2; 
      badScoreView.setBackgroundColor(Color.parseColor("#d9534f")); 
      mediocreScoreView.setBackgroundColor(Color.parseColor("#f7d6a6")); 
      goodScoreView.setBackgroundColor(Color.parseColor("#5cb85c")); 
      worldclassScoreView.setBackgroundColor(Color.parseColor("#0275d8")); 
      questionCommentEditText.clearFocus(); 
     } 
    }); 

    goodScoreView.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      // TODO: add logic to change score 
      subCategoryScore = 3; 
      badScoreView.setBackgroundColor(Color.parseColor("#d9534f")); 
      mediocreScoreView.setBackgroundColor(Color.parseColor("#f0ad4e")); 
      goodScoreView.setBackgroundColor(Color.parseColor("#addbad")); 
      worldclassScoreView.setBackgroundColor(Color.parseColor("#0275d8")); 
      questionCommentEditText.clearFocus(); 
     } 
    }); 

    worldclassScoreView.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      // TODO: add logic to change score 
      subCategoryScore = 4; 
      badScoreView.setBackgroundColor(Color.parseColor("#d9534f")); 
      mediocreScoreView.setBackgroundColor(Color.parseColor("#f0ad4e")); 
      goodScoreView.setBackgroundColor(Color.parseColor("#5cb85c")); 
      worldclassScoreView.setBackgroundColor(Color.parseColor("#80baeb")); 
      questionCommentEditText.clearFocus(); 
     } 
    }); 

而且佈局,只是在相關情況:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout 
xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="wrap_content"> 
<LinearLayout 
    android:id="@+id/qwedit_elv_item_score_container" 
    android:orientation="horizontal" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content"> 
    <LinearLayout 
     android:id="@+id/qwedit_main_badView" 
     android:layout_width="232.5dp" 
     android:layout_height="145.3dp" 
     android:background="@color/badScore"> 
     <TextView 
      android:id="@+id/qwedit_main_badTextView" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:gravity="center" 
      android:textColor="@color/white" 
      android:textSize="20sp" 
      android:text="Bad"/> 
    </LinearLayout> 
    <LinearLayout 
     android:id="@+id/qwedit_main_mediocreView" 
     android:layout_width="232.5dp" 
     android:layout_height="145.3dp" 
     android:background="@color/mediocreScore"> 
     <TextView 
      android:id="@+id/qwedit_main_mediumTextView" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:gravity="center" 
      android:textColor="@color/white" 
      android:textSize="20sp" 
      android:text="Mediocre"/> 
    </LinearLayout> 
    <LinearLayout 
     android:id="@+id/qwedit_main_goodView" 
     android:layout_width="232.5dp" 
     android:layout_height="145.3dp" 
     android:background="@color/goodScore"> 
     <TextView 
      android:id="@+id/qwedit_main_goodTextView" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:gravity="center" 
      android:textColor="@color/white" 
      android:textSize="20sp" 
      android:text="Good"/> 
    </LinearLayout> 
    <LinearLayout 
     android:id="@+id/qwedit_main_wcView" 
     android:layout_width="232.5dp" 
     android:layout_height="145.3dp" 
     android:background="@color/worldclassScore"> 
     <TextView 
      android:id="@+id/qwedit_main_worldclassTextView" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:gravity="center" 
      android:textColor="@color/white" 
      android:textSize="20sp" 
      android:text="World Class"/> 
    </LinearLayout> 
</LinearLayout> 
<EditText 
    android:id="@+id/qwedit_main_commentEditText" 
    android:layout_width="930dp" 
    android:layout_height="wrap_content" 
    android:layout_below="@+id/qwedit_elv_item_score_container" 
    android:cursorVisible="false" 
    android:focusable="true" 
    android:focusableInTouchMode="true" 
    android:inputType="text" 
    android:hint="Comments"/> 

</RelativeLayout> 

出於某種原因,當用戶按下EditText,要麼添加文字或點擊它,按鈕onClick()功能不再起作用(即它們不再改變顏色)。

這是由於某些焦點? javacode onClick函數的格式爲CustomListViewAdapter,以便能夠爲相同的ExpandableListView增加相同數量的問題(約200個左右)。

+0

刪除此行:android:focusableInTouchMode =「true」 – amacf

+0

嗯,這似乎沒有它。問題繼續存在,無論那條線是否存在或不存在 – Tadhg

回答

0

據我瞭解問題是與視圖的焦點。您可能需要使用OnFocusChangeListener,然後檢查屏幕處於觸摸模式時是否發生焦點事件。

或者你可以嘗試添加的每個的onClick(查看視圖)內,這些線路的方法:

view.setFocusableInTouchMode(true); 
view.requestFocus(); 
view.setFocusableInTouchMode(false); 

這種方式,視圖將獲得焦點,你可以對這些行之後做的onClick()方法裏面的東西代碼後,視圖又被設置爲setFocusableInTouchMode(false);

+0

嗯,你會如何建議實現OnFocusChangeListener?我嘗試了將三行添加到每個'onClick()'方法的簡單建議,但這並沒有解決問題。 – Tadhg

+0

您是否嘗試將這些行添加到包含按鈕的LinearLayout中:android:focusable =「true」, android:focusableInTouchMode =「true」? –