我有一个片段类Frag2
和一个视图类Rectangle
,它将onDraw发送到另一个片段类Frag1
,我需要从Rectangle
向Frag 2
发送一些数据{1}}。
我该怎么办 ? :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);
}
}
矩形工作正常。它向Frag1发送一个矩形我只想在这里发送数字并将其放在x(int rectangle)中
Frag 1 class:
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"
/>
答案 0 :(得分:0)
我很确定你可以在Rectangle类中定义一个void setX(int x)
。
在您放置矩形视图的任何片段中,您需要类似
的内容// Make a field for this
rect = (Rectangle) myInflatedView.findViewById(R.id.Rect);
// Call your method whenever
rect.setX(x);
注意:看起来您正在尝试随机更改视图的大小并期望onDraw
方法执行此更改。
如果您想使用Shapes等,我认为您应该使用a Canvas
object
答案 1 :(得分:0)
在Rectangle类public static int pick100Value = 0
中创建一个静态字段,并在onDraw()
pick100Value
减去x
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;
}
}