2014-02-26 38 views
0

介紹獲取從另一個佈局信息到活動

我與Android設備上的畫布圖形練習。爲此我創建了一個應用程序,我可以用手指寫一行,然後將其擦除。

當我點擊調色板時,會出現一些對話窗口,並顯示一些顏色。所以,我有2個佈局:

1-主要佈局

2-調色板佈局

enter image description here

問題

首先我的主要活動調色板,併爲每個顏色按鈕我有一個onClick調用主要活動的方法。

事情是,現在我無法做到這一點onClick功能。我認爲它有什麼用,這樣做,現在我開始這個佈局視圖,而不是作爲一個佈局,所以每個按鈕的onclick功能不起作用

這是palette.xml佈局的按鈕,例如:

<ImageButton 
     android:layout_width="@dimen/large_brush" 
     android:layout_height="@dimen/large_brush" 
     android:background="#FF660000" 
     android:onClick="paintClicked" 
     android:tag="#FF660000" /> 

當我點擊每個按鈕時,它會啓動paintClicked方法並傳遞顏色標籤。

那麼,我如何啓動調色板佈局,當我點擊調色板按鈕,以便能夠將信息從此佈局傳遞到主要活動?

更新 - 當前方法調用palette.xml

final Dialog paletteDialog = new Dialog(this); 
paletteDialog.setTitle("Colores:"); 
paletteDialog.setContentView(R.layout.palette); 

LinearLayout paletteLayout = (LinearLayout) paletteDialog.findViewById(R.id.paint_colors); 
bnColor = (ImageButton) paletteLayout.getChildAt(0); 
bnColor.setImageDrawable(getResources().getDrawable(R.drawable.button_pressed)); 

paletteDialog.show(); 

的logcat:

java.lang.NoSuchMethodException: paintClicked [class android.view.View] 

這是paintClicked方法參考:

public void paintClicked(View view){ 
+0

嘗試從代碼中設置的onClick – simoneL

+0

@simoneL我可以設置的onClick與主活動的佈局」項目的代碼,但我怎麼這樣對處理像調色板佈局的第二佈局的項目嗎? – masmic

+0

對話框對象有一個findViewById方法,可以幫助檢索內部視圖 – simoneL

回答

1

我建議你看看DialogFragment(如果你沒有使用它)。您可以創建一個簡單的回調界面,以便在單擊某個顏色時使用此信息回撥到附帶的Activity。使封閉的Activity實現接口(由下面的onAttach生命週期方法實施),並在拾取顏色時讓對話框片段調用它。

public class ColorPickerDialog extends DialogFragment implements View.OnClickListener { 

    // Define a callback interface 
    public interface OnColorSelectedListener { 
     public void onColorSelected(int color); 
    } 

    private OnColorSelectedListener listener; 

    @Override 
    public void onAttach(Activity activity) { 
     if (!(activity instanceof OnColorSelectedListener)) { 
      throw new IllegalStateException("Activity must implement OnColorSelectedListener!"); 
     } 
     listener = (OnColorSelectedListener) activity; 
    } 

    @Override 
    public void onDetach() { 
     listener = null; 
    } 

    @Override 
    public Dialog onCreateDialog(Bundle savedInstanceState) { 
     LayoutInflater inflater = LayoutInflater.from(getActivity()); 
     View view = inflater.inflate(R.layout.palette, container, false); 

     // call view.findViewById(...) for all your color buttons and 
     // set the OnClickListener 
     view.findViewById(...).setOnClickListener(this); 
     ... 

     AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); 
     builder.setTitle("Colores:"); 
     builder.setView(view); 
     return builder.create(); 
    } 

    @Override 
    public void onClick(View v) { 
     /* 
     * Determine the color from the view that was clicked. You can use a 
     * switch statement on v.getId() if they all have IDs, but there are 
     * other possibilities as well. 
     */ 
     int color = ...; 
     listener.onColorSelected(color); 
     dismiss(); 
    } 
} 
+0

當我點擊調色板按鈕時,如何從主Activity中啓動dialogFragment?你能提供一個例子嗎? – masmic

+0

您使用FragmentManager和/或FragmentTransaction。 DialogFragment的文檔解釋瞭如何做到這一點。 – Karakuri

0

安卓:只的onClick如果擴展視圖的上下文包含該函數,則工作(這裏是「paintClicked」)。 所以,如果你沒有擴展你的主要活動這個佈局,它可能是它不起作用的原因。

您可能會更好地將Ids設置爲ImageButtons並使用一些findViewById(R.id.btnX).setOnClickListener(myListener);顯示按鈕後。

+0

我使用該方法更新了帖子。當我開始對話時,我通過'this'上下文,所以這不會是足夠的? – masmic

相關問題