我正在开发一个应用程序,在那里我制作了一个“自定义键盘”,你可以“输入”扑克牌。我把那个“键盘”变成了一个片段,我试图让它出现在我的主要活动中,但它不会显示出来。我到处寻找但找不到解决方案。你可能会说,我对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 :(得分:0)
您必须创建片段的实例 - 不仅是通用片段。 喜欢:
CardKeyboardFrag cardFrag = new CardKeyboardFrag();
和
transaction.add(R.id.fragment_container, cardFrag);