如何将数据(INT)从片段类发送到视图类

  • 本文关键字:片段 视图 数据 INT android
  • 更新时间 :
  • 英文 :


我有一个片段类Frag2和一个视图类Rectangle,它将OnDraw发送到另一个片段类Frag1,我需要从Frag 2将一些数据发送到Rectangle。我该怎么办 ?:D

(对不起,它的杂乱无章的杂乱无章的我一直在尝试了过去24小时。)

frag 2

public class FragmentTwo extends Fragment {
TextView changingText;
Button changeTextButton;
Button XButton;
Button YButton;
private int SecondX;
NumberPicker Pick100 = null;
private int entered;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    // TODO Auto-generated method stub
    View myInflatedView = inflater.inflate(R.layout.fragment_two_layout, container, false);

    changingText = (TextView) myInflatedView.findViewById(R.id.textView);
    changeTextButton = (Button) myInflatedView.findViewById(R.id.button);

    XButton = (Button) myInflatedView.findViewById(R.id.buttonX);
    YButton = (Button) myInflatedView.findViewById(R.id.buttonY);
    Pick100 = (NumberPicker) myInflatedView.findViewById(R.id.secondD);

    changeTextViewValueRandomlyOnButtonClick();
    return myInflatedView;
}

private void changeTextViewValueRandomlyOnButtonClick() {
    final String[] text = {"A", "B", "C", "D"};

    changeTextButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Random rand = new Random();
            int random = rand.nextInt(100) + 1;
            if (random == 100) changingText.setText(text[3]);
            if (random > 90 && random <= 99) changingText.setText(text[2]);
            if (random < 60 && random >= 1) changingText.setText(text[0]);
            if (random >= 60 && random <= 90) changingText.setText(text[1]);
        }
    });
}

您可以真正忽略其中的大部分。我试图将Pick100发送到此类

public class Rectangle extends View {
//FragmentTwo F2 = new FragmentTwo();

public Rectangle(Context context) {
    super(context);
}
public Rectangle(Context context, @Nullable AttributeSet attrs) {
    super(context, attrs);
}
public Rectangle(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
}
@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    Log.d("X","RED DOT EUQALS TO "+x);

    Paint paint = new Paint();
    paint.setAntiAlias(true);
    paint.setColor(Color.RED);
    canvas.drawRect(x,100,x+60,100+60,paint);
}

}

矩形正常工作。

frag 1类:

public class FragmentOne extends Fragment {
RelativeLayout relativeLayout;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    View myInflatedView= inflater.inflate(R.layout.fragment_one_layout,container,false);

    return myInflatedView;
}

}

frag 1 xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:id="@+id/Frag1"
android:layout_height="match_parent"
android:background="#000">

<com.redot.puzzle1.Rectangle
    android:id="@+id/Rect"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    />

我很确定您可以简单地在矩形类中定义void setX(int x)

在您放置矩形视图的任何片段中,您都需要

之类的东西
// Make a field for this
rect = (Rectangle) myInflatedView.findViewById(R.id.Rect);
// Call your method whenever
rect.setX(x);

注意:看起来您正在尝试随机更改视图的大小,并期望onDraw方法可以更改。

我认为您应该使用 Canvas 对象,如果您想使用形状以及此类

在矩形类public static int pick100Value = 0中创建一个静态字段,然后从on Draw()

中从 x中减去 pick100Value
public class Rectangle extends View {
    public static int pick100Value = 0;
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        // ...
        canvas.drawRect(x - pick100Value , 100, (x - pick100Value)  + 60, 100 + 60, paint);
    }
}   

在fragmenttwo中听取onvaluechanged并设置 Rectangle.pick100Value = newVal

public class FragmentTwo extends Fragment implements OnValueChangeListener {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        // TODO Auto-generated method stub
        View myInflatedView = inflater.inflate(R.layout.fragment_two_layout, container, false);

        changingText = (TextView) myInflatedView.findViewById(R.id.textView);
        changeTextButton = (Button) myInflatedView.findViewById(R.id.button);

        XButton = (Button) myInflatedView.findViewById(R.id.buttonX);
        YButton = (Button) myInflatedView.findViewById(R.id.buttonY);
        Pick100 = (NumberPicker) myInflatedView.findViewById(R.id.secondD);
        Pick100.setOnValueChangedListener(this);
        changeTextViewValueRandomlyOnButtonClick();
        return myInflatedView;
    }
    @Override
    public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
        Rectangle.pick100Value = newVal;
    }
}

最新更新