Android 通过按下按钮更改同一Java活动的内容

Android 通过按下按钮更改同一Java活动的内容,android,Android,当我运行下面的代码时,首先我看到一个红色的矩形 但是我想先看到一个绿色的矩形,然后当我按下按钮时,接下来在同一活动中看到一个红色的矩形 解决办法是什么 MainActivity.java public class MainActivity extends Activity { Button button; Draw draw; Draw2 draw2; RelativeLayout linearLayout; public void onCreate(Bundle

当我运行下面的代码时,首先我看到一个红色的矩形

但是我想先看到一个绿色的矩形,然后当我按下按钮时,接下来在同一活动中看到一个红色的矩形

解决办法是什么

MainActivity.java

public class MainActivity extends Activity {
Button button;
    Draw draw;
    Draw2 draw2;
    RelativeLayout linearLayout;
    public void onCreate(Bundle s) {
        super.onCreate(s);
        setContentView(R.layout.activity_main);

        linearLayout = (RelativeLayout) findViewById(R.id.t);
button = (Button) findViewById(R.id.button);

 draw = new Draw(this);

        ViewGroup.LayoutParams layoutParams = new ViewGroup.LayoutParams(200, 200);
        draw.setLayoutParams(layoutParams);

        linearLayout.addView(draw);

        button.setOnClickListener(new View.OnClickListener() {


            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this,Draw2.class);
                startActivity(intent);
            }
        });
        draw2 = new Draw2(this);
        draw2.setLayoutParams(layoutParams);
        linearLayout.addView(draw2);
    }

}
Draw.java

public class Draw extends View {
    Paint paint;

    public Draw(Context context) {
        super(context);
        paint = new Paint(Paint.ANTI_ALIAS_FLAG);
    }

@Override
    protected  void onDraw(Canvas canvas) {
    paint.setColor(Color.GREEN);
    canvas.drawRect(50,30,100,120,paint);
    super.onDraw(canvas);
    }}
public class Draw2 extends View {
    Paint paint;
    Draw2(Context context){
        super(context);
      paint = new Paint();
    }
@Override
    protected void onDraw(Canvas canvas){
    paint.setColor(Color.RED);
    canvas.drawRect(50,30,100,120,paint);
super.onDraw(canvas);
}
}
Draw2.java

public class Draw extends View {
    Paint paint;

    public Draw(Context context) {
        super(context);
        paint = new Paint(Paint.ANTI_ALIAS_FLAG);
    }

@Override
    protected  void onDraw(Canvas canvas) {
    paint.setColor(Color.GREEN);
    canvas.drawRect(50,30,100,120,paint);
    super.onDraw(canvas);
    }}
public class Draw2 extends View {
    Paint paint;
    Draw2(Context context){
        super(context);
      paint = new Paint();
    }
@Override
    protected void onDraw(Canvas canvas){
    paint.setColor(Color.RED);
    canvas.drawRect(50,30,100,120,paint);
super.onDraw(canvas);
}
}

Draw2只是一个视图而不是一个活动,所以像这样更改MainActivity

public class MainActivity extends Activity {
     Button button;
     Draw draw;
     Draw2 draw2;
     RelativeLayout linearLayout;
     ViewGroup.LayoutParams layoutParams;

public void onCreate(Bundle s) {
    super.onCreate(s);
    setContentView(R.layout.activity_main);

    linearLayout = (RelativeLayout) findViewById(R.id.container);
    button = (Button) findViewById(R.id.button1);

    draw = new Draw(this);

    layoutParams = new ViewGroup.LayoutParams(200, 200);
    draw.setLayoutParams(layoutParams);

    linearLayout.addView(draw);

    button.setOnClickListener(new View.OnClickListener() {


        public void onClick(View v) {
            draw2 = new Draw2(getApplicationContext());
            draw2.setLayoutParams(layoutParams);
            linearLayout.addView(draw2);
        }
    });

}

}

我在MainActivity中使用了您的代码,但在onclick函数的一部分和此参数的一部分,android studio为我在com.exampledle.www.Draw2中表示“Draw2(android.content.Context)”不能应用于“(android.view.view.OnClickListener)”,在同一onclick函数的layoutParams参数部分,android studio说,对我来说,“变量”layoutParams是从内部类访问的。需要声明为最终。我做了一些更改,请尝试。谢谢。但我也使用了此代码,但当我在设备中运行APK文件时,我看到意外停止。请重试。该代码在您的设备中运行良好吗?是的,它在我的设备上运行良好,我所做的唯一更改是将按钮名称从一个按钮更改为另一个按钮1。您可以在模拟器上运行它,并从logcat中找到错误