由$ P $更改同一个Java活动内容pssing按钮 [英] Changing the contents of the same Java Activity by pressing the button

查看:352
本文介绍了由$ P $更改同一个Java活动内容pssing按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我运行下面的code,首先我看到一个红色矩形。

但我想先看到一个绿色的长方形,然后当我preSS按钮,旁边看到同样的活动一个红色矩形。

如何解决?

MainActivity.java

 公共类MainActivity延伸活动{
Button按钮;
    绘制画;
    DRAW2 DRAW2;
    RelativeLayout的LinearLayout中;
    公共无效的onCreate(捆绑S){
        super.onCreate(多个);
        的setContentView(R.layout.activity_main);        的LinearLayout =(RelativeLayout的)findViewById(R.id.t);
按钮=(按钮)findViewById(R.id.button); 画=新的绘图(本);        ViewGroup.LayoutParams的LayoutParams =新ViewGroup.LayoutParams(200,200);
        draw.setLayoutParams(的LayoutParams);        linearLayout.addView(画);        button.setOnClickListener(新View.OnClickListener(){
            公共无效的onClick(视图v){
                意向意图=新意图(MainActivity.this,Draw2.class);
                startActivity(意向);
            }
        });
        DRAW2 =新DRAW2(本);
        draw2.setLayoutParams(的LayoutParams);
        linearLayout.addView(DRAW2);
    }}

Draw.java

 公共类抽奖扩展视图{
    涂料粉刷;    公开抽奖(上下文的背景下){
        超级(上下文);
        油漆=新的油漆(Paint.ANTI_ALIAS_FLAG);
    }@覆盖
    保护无效的onDraw(帆布油画){
    paint.setColor(Color.GREEN);
    canvas.drawRect(50,30,100,120,油漆);
    super.onDraw(画布);
    }}

Draw2.java

 公共类DRAW2扩展视图{
    涂料粉刷;
    DRAW2(上下文的背景下){
        超级(上下文);
      油漆=新的油漆();
    }
@覆盖
    保护无效的onDraw(帆布油画){
    paint.setColor(Color.RED);
    canvas.drawRect(50,30,100,120,油漆);
super.onDraw(画布);
}
}


解决方案

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

 公共类MainActivity延伸活动{
     Button按钮;
     绘制画;
     DRAW2 DRAW2;
     RelativeLayout的LinearLayout中;
     ViewGroup.LayoutParams的LayoutParams;公共无效的onCreate(捆绑S){
    super.onCreate(多个);
    的setContentView(R.layout.activity_main);    的LinearLayout =(RelativeLayout的)findViewById(R.id.container);
    按钮=(按钮)findViewById(R.id.button1);    画=新的绘图(本);    的LayoutParams =新ViewGroup.LayoutParams(200,200);
    draw.setLayoutParams(的LayoutParams);    linearLayout.addView(画);    button.setOnClickListener(新View.OnClickListener(){
        公共无效的onClick(视图v){
            DRAW2 =新DRAW2(getApplicationContext());
            draw2.setLayoutParams(的LayoutParams);
            linearLayout.addView(DRAW2);
        }
    });}

}

When I run the following code, first I see a red rectangle.

But I want to see a green rectangle first, and then when I press the button, next see a red rectangle in the same activity.

What is the solution?

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);
    }}

Draw2.java

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 is just a view not an activity,so Change MainActivity like this

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);
        }
    });

}

}

这篇关于由$ P $更改同一个Java活动内容pssing按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆