2017-06-17 79 views
0

我正在製作一個應用程序,在該應用程序中,我製作了一個「自定義鍵盤」,可以在其中「輸入」紙牌。我使那個「鍵盤」成爲一個片段,我試圖讓它出現在我的主要活動中,但它不會顯示出來。我到處尋找,但找不到解決方案。正如你可以告訴的那樣,我對android和特別是碎片很陌生。任何幫助,將不勝感激。未出現片段

代碼:

MainActivity.java:

package com.example.cribbage2; 

import android.support.v4.app.FragmentTransaction; 
import android.support.v4.app.Fragment; 
import android.os.Bundle; 
import android.support.v4.app.FragmentActivity; 

public class MainActivity extends FragmentActivity implements 
CardKeyboardFrag.CardTransfer{ 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    Fragment newFragment = new Fragment(); 
    FragmentTransaction transaction = getSupportFragmentManager().beginTransaction(); 

    transaction.add(R.id.fragment_container, newFragment); 
    transaction.addToBackStack(null); 

    transaction.commit(); 
} 

@Override 
public void onCardSelected(int card) { 
    //do stuff with int here 
} 
} 

activity_main.xml中:

<?xml version="1.0" encoding="utf-8"?> 
<android.support.constraint.ConstraintLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    tools:context="com.example.cribbage2.MainActivity"> 

<FrameLayout 
    android:id="@+id/fragment_container" 
    android:layout_width="368dp" 
    android:layout_height="495dp" 
    app:layout_constraintTop_toTopOf="parent" 
    android:layout_marginTop="8dp" 
    app:layout_constraintBottom_toBottomOf="parent" 
    android:layout_marginBottom="8dp" 
    android:layout_marginLeft="8dp" 
    app:layout_constraintLeft_toLeftOf="parent" 
    android:layout_marginRight="8dp" 
    app:layout_constraintRight_toRightOf="parent"> 

</FrameLayout> 

CardKeyboardFrag.java

package com.example.cribbage2; 

import android.app.Activity; 
import android.app.Fragment; 
import android.content.Context; 
import android.os.Bundle; 
import android.support.annotation.Nullable; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.Button; 


public class CardKeyboardFrag extends android.support.v4.app.Fragment implements View.OnClickListener { 

CardTransfer mCallback; 

public interface CardTransfer { 
    void onCardSelected(int card); 
} 

@Override 
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { 
    View view = inflater.inflate(R.layout.card_keyboard_frag, container, false); 

    //region buttonAssignments 
    Button one = (Button) getView().findViewById(R.id.buttonA); 
    one.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View arg0) { 
      buttonClicked(1); 
     } 
    }); 
    Button two = (Button) getView().findViewById(R.id.button2); 
    two.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View arg0) { 
      buttonClicked(2); 
     } 
    }); 
    Button three = (Button) getView().findViewById(R.id.button3); 
    three.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View arg0) { 
      buttonClicked(3); 
     } 
    }); 
    Button four = (Button) getView().findViewById(R.id.button4); 
    four.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View arg0) { 
      buttonClicked(4); 
     } 
    }); 
    Button five = (Button) getView().findViewById(R.id.button5); 
    five.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View arg0) { 
      buttonClicked(5); 
     } 
    }); 
    Button six = (Button) getView().findViewById(R.id.button6); 
    six.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View arg0) { 
      buttonClicked(6); 
     } 
    }); 
    Button seven = (Button) getView().findViewById(R.id.button7); 
    seven.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View arg0) { 
      buttonClicked(7); 
     } 
    }); 
    Button eight = (Button) getView().findViewById(R.id.button8); 
    eight.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View arg0) { 
      buttonClicked(8); 
     } 
    }); 
    Button nine = (Button) getView().findViewById(R.id.button9); 
    nine.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View arg0) { 
      buttonClicked(9); 
     } 
    }); 
    Button ten = (Button) getView().findViewById(R.id.button10); 
    ten.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View arg0) { 
      buttonClicked(10); 
     } 
    }); 
    Button eleven = (Button) getView().findViewById(R.id.buttonJ); 
    eleven.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View arg0) { 
      buttonClicked(11); 
     } 
    }); 
    Button twelve = (Button) getView().findViewById(R.id.buttonQ); 
    twelve.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View arg0) { 
      buttonClicked(12); 
     } 
    }); 
    Button thirteen = (Button) getView().findViewById(R.id.buttonK); 
    thirteen.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View arg0) { 
      buttonClicked(13); 
     } 
    }); 
    Button delete = (Button) getView().findViewById(R.id.buttonD); 
    delete.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View arg0) { 
      buttonClicked(14); 
     } 
    }); 
    Button buttonGo = (Button) getView().findViewById(R.id.buttonG); 
    buttonGo.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View arg0) { 
      buttonClicked(15); 
     } 
    }); 
    //endregion 

    return view; 
} 

@Override 
public void onAttach(Context context) { 
    super.onAttach(context); 

    try { 
     mCallback = (CardTransfer) context; 
    } catch (ClassCastException e) { 
     throw new ClassCastException(context.toString() + " must implement CardTransfer"); 
    } 
} 


@Override 
public void onClick(View v) { 
    switch (v.getId()) { 
     case R.id.buttonA: 
      buttonClicked(1); 
      break; 

     case R.id.button2: 
      buttonClicked(2); 
      break; 

     case R.id.button3: 
      buttonClicked(3); 
      break; 

     case R.id.button4: 
      buttonClicked(4); 
      break; 

     case R.id.button5: 
      buttonClicked(5); 
      break; 

     case R.id.button6: 
      buttonClicked(6); 
      break; 

     case R.id.button7: 
      buttonClicked(7); 
      break; 

     case R.id.button8: 
      buttonClicked(8); 
      break; 

     case R.id.button9: 
      buttonClicked(9); 
      break; 

     case R.id.button10: 
      buttonClicked(10); 
      break; 

     case R.id.buttonJ: 
      buttonClicked(11); 
      break; 

     case R.id.buttonQ: 
      buttonClicked(12); 
      break; 

     case R.id.buttonK: 
      buttonClicked(13); 
      break; 

     case R.id.buttonD: 
      buttonClicked(14); 
      break; 

     case R.id.buttonG: 
      buttonClicked(15); 
      break; 
    } 
} 

public void buttonClicked(int num){ 
    mCallback.onCardSelected(num); 
} 
} 

card_keyboard_frag.xml

再次
<?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="match_parent" 
       xmlns:app="http://schemas.android.com/apk/res-auto"> 


<TextView 
    android:id="@+id/textView" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_centerHorizontal="true" 
    android:layout_marginTop="16dp" 
    android:text="Cards" 
    android:textAlignment="center" 
    android:textSize="30sp"/> 

<GridLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_alignParentBottom="true" 
    android:layout_alignParentStart="true" 
    android:layout_marginBottom="8dp" 
    android:columnCount="3" 
    android:rowCount="5"> 

    <Button 
     android:id="@+id/buttonK" 
     android:layout_width="120dp" 
     android:layout_height="50dp" 
     android:text="King"/> 

    <Button 
     android:id="@+id/buttonD" 
     android:layout_width="120dp" 
     android:layout_height="50dp" 
     android:text="Delete"/> 

    <Button 
     android:id="@+id/buttonG" 
     android:layout_width="120dp" 
     android:layout_height="50dp" 
     android:text="Go!"/> 

    <Button 
     android:id="@+id/button10" 
     android:layout_width="120dp" 
     android:layout_height="50dp" 
     android:text="10"/> 

    <Button 
     android:id="@+id/buttonJ" 
     android:layout_width="120dp" 
     android:layout_height="50dp" 
     android:text="Jack"/> 

    <Button 
     android:id="@+id/buttonQ" 
     android:layout_width="120dp" 
     android:layout_height="50dp" 
     android:text="Queen"/> 

    <Button 
     android:id="@+id/button7" 
     android:layout_width="120dp" 
     android:layout_height="50dp" 
     android:text="7"/> 

    <Button 
     android:id="@+id/button8" 
     android:layout_width="120dp" 
     android:layout_height="50dp" 
     android:text="8"/> 

    <Button 
     android:id="@+id/button9" 
     android:layout_width="120dp" 
     android:layout_height="50dp" 
     android:text="9"/> 

    <Button 
     android:id="@+id/button4" 
     android:layout_width="120dp" 
     android:layout_height="50dp" 
     android:text="4"/> 

    <Button 
     android:id="@+id/button5" 
     android:layout_width="120dp" 
     android:layout_height="50dp" 
     android:text="5"/> 

    <Button 
     android:id="@+id/button6" 
     android:layout_width="120dp" 
     android:layout_height="50dp" 
     android:text="6"/> 

    <Button 
     android:id="@+id/buttonA" 
     android:layout_width="120dp" 
     android:layout_height="50dp" 
     android:text="Ace"/> 

    <Button 
     android:id="@+id/button2" 
     android:layout_width="120dp" 
     android:layout_height="50dp" 
     android:text="2"/> 

    <Button 
     android:id="@+id/button3" 
     android:layout_width="120dp" 
     android:layout_height="50dp" 
     android:text="3"/> 
</GridLayout> 

</RelativeLayout> 

提前感謝!

回答

0

您必須創建片段的實例 - 不僅是一個通用片段。 像:

CardKeyboardFrag cardFrag = new CardKeyboardFrag(); 

transaction.add(R.id.fragment_container, cardFrag); 
+0

太謝謝你了!我知道這將是一些明顯的答案,就像那個大聲笑 –

+0

:) np - 每個人都從Android開始。但其他代碼在開始時有點複雜,特別是你已經在使用接口。 –