通过按钮创建可见和不可见的imageView

时间:2016-12-30 15:06:40

标签: java android

我有三个活动,每个活动都有一个按钮。 Act1 btn1 Act2 btn2 Act3 btn3 。我有另一个Activity作为MainActivity,有三个imageViews: ImageView1 imageView2 imageView3 ,所有这些最初都是不可见的。我想要它,所以当我点击 act1 中的 btn1 时,MainActivity中的 imageView1 将会可见,当再次点击btn1时, imageView1 将再次隐身。对于 imageView2 imageView3 也是如此。 到目前为止我有这个代码:

Activity1

public class Activity1 extends AppCompatActivity{

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity1);

Button btn1 = (Button) findViewById(R.id.btn1);
btn1.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        String visibilityStr = PublicSharedPreferences.getDefaults("keyVisibility", getApplicationContext());

        if (visibilityStr != null) {
            if (visibilityStr.equals("0")) {
                Toast.makeText(act1.this, "it visibled", Toast.LENGTH_SHORT).show();
                visibilityStr = "1";
btn1.setImageResource(R.mipmap.img1);

            } else {
                visibilityStr = "0";
                Toast.makeText(act1.this, "it invisibled", Toast.LENGTH_SHORT).show();
btn1.setImageResource(R.mipmap.img2);

            }
        } else {
            visibilityStr = "1";
            Toast.makeText(act1.this, "it visibled", Toast.LENGTH_SHORT).show();
btn1.setImageResource(R.mipmap.img1);

        }

        PublicSharedPreferences.setDefaults("keyVisibility", visibilityStr, getApplicationContext());

    }
});
}





Activity2

public class Activity2 extends AppCompatActivity{

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity2);


Button btn1 = (Button) findViewById(R.id.btn2);
btn1.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        String visibilityStr = PublicSharedPreferences.getDefaults("keyVisibility", getApplicationContext());

        if (visibilityStr != null) {
            if (visibilityStr.equals("0")) {
                Toast.makeText(act2.this, "it visibled", Toast.LENGTH_SHORT).show();
btn2.setImageResource(R.mipmap.img1);

                visibilityStr = "1";
            } else {
                visibilityStr = "0";
                Toast.makeText(act2.this, "it invisibled", Toast.LENGTH_SHORT).show();
btn2.setImageResource(R.mipmap.img2);

            }
        } else {
            visibilityStr = "1";
            Toast.makeText(act2.this, "it visibled", Toast.LENGTH_SHORT).show();
btn2.setImageResource(R.mipmap.img1);

        }

        PublicSharedPreferences.setDefaults("keyVisibility", visibilityStr, getApplicationContext());

    }
});
}



Activity3

public class Activity3 extends AppCompatActivity{

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity3);

Button btn1 = (Button) findViewById(R.id.btn3);
btn1.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        String visibilityStr = PublicSharedPreferences.getDefaults("keyVisibility", getApplicationContext());

        if (visibilityStr != null) {
            if (visibilityStr.equals("0")) {
                Toast.makeText(act3.this, "it visibled", Toast.LENGTH_SHORT).show();
Btn3.setImageResource(R.mipmap.img1);

                visibilityStr = "1";
            } else {
                visibilityStr = "0";
                Toast.makeText(act3.this, "it invisibled", Toast.LENGTH_SHORT).show();
btn3.setImageResource(R.mipmap.img2);

            }
        } else {
            visibilityStr = "1";
            Toast.makeText(act3.this, "it visibled", Toast.LENGTH_SHORT).show();
btn3.setImageResource(R.mipmap.img1);

        }

        PublicSharedPreferences.setDefaults("keyVisibility", visibilityStr, getApplicationContext());
    }
});
}

MainActivity有三个imageViews:

    ImageView imgView1 = (ImageView) findViewById(R.id.imgView1);
String visibilityStr= PublicSharedPreferences.getDefaults("keyVisibility", getApplicationContext());

if (visibilityStr.equals("0"))
    imgView1.setVisibility(View.INVISIBLE);
else
    imgView1.setVisibility(View.VISIBLE);


ImageView imgView2 = (ImageView) findViewById(R.id.imgView2);
String visibilityStr= PublicSharedPreferences.getDefaults("keyVisibility", getApplicationContext());

if (visibilityStr.equals("0"))
    imgView2.setVisibility(View.INVISIBLE);
else
    imgView2.setVisibility(View.VISIBLE);



ImageView imgView3 = (ImageView) findViewById(R.id.imgView3);
String visibilityStr= PublicSharedPreferences.getDefaults("keyVisibility", getApplicationContext());

if (visibilityStr.equals("0"))
    imgView3.setVisibility(View.INVISIBLE);
else
    imgView3.setVisibility(View.VISIBLE);

他们运作良好。但问题是当我点击 btn1 时,MainActivity中的所有imageViews都会发生变化(变得可见或不可见),或者当我点击 btn3 时,所有的imageView都会发生变化。我想要它,所以 btn1 只需更改 imageView1 btn2 只需更改 imageView2 btn3 只需更改 imageView3 ,而不是更改所有imageViews的其中一个按钮。我怎样才能做到这一点?哪部分代码错了?

3 个答案:

答案 0 :(得分:1)

问题是你只有1key Preference它的所有keyVisibility所以当你点击任何一个按钮时,这个键会根据你的代码变为1或0

这里

ImageView imgView1 = (ImageView) findViewById(R.id.imgView1);
String visibilityStr1= PublicSharedPreferences.getDefaults("keyVisibility1", getApplicationContext());

if (visibilityStr1.equals("0"))
    imgView1.setVisibility(View.INVISIBLE);
else
    imgView1.setVisibility(View.VISIBLE);


ImageView imgView2 = (ImageView) findViewById(R.id.imgView2);
String visibilityStr2= PublicSharedPreferences.getDefaults("keyVisibility2", getApplicationContext());

if (visibilityStr2.equals("0"))
    imgView2.setVisibility(View.INVISIBLE);
else
imgView2.setVisibility(View.VISIBLE);



ImageView imgView3 = (ImageView) findViewById(R.id.imgView3);
`String visibilityStr3= PublicSharedPreferences.getDefaults("keyVisibility3",` `getApplicationContext());`

if (visibilityStr3.equals("0"))
    imgView3.setVisibility(View.INVISIBLE);
else
    imgView3.setVisibility(View.VISIBLE);

并且您应该更改act1,act2和act3

上的keyVisibility

答案 1 :(得分:0)

首先,请对Android SharedPreferences (如何在本地存储中正确保存/获取共享偏好值)进行更多研究。

完成研究后,这里有一些暗示。希望你能够自己实现这个。

保存值 - 单击按钮

时保存布尔值
Context mContext = getApplicationContext();
SharedPreferences mPrefs = mContext.getSharedPreferences("MySharedPrefs", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = mPrefs.edit();
editor.putBoolean("IsImageViewVisible", true); 
// use different keys for different imageviews; 
//ex: for ImageView1, use IsImageView_1_Visible; for ImageView2, use IsImageView_2_Visible etc
editor.commit();
在MainActivity中

获取值

Context mContext = getApplicationContext();
SharedPreferences mPrefs = mContext.getSharedPreferences("MySharedPrefs", Context.MODE_PRIVATE);
Boolean isImageViewVisible = mPrefs.getBoolean("IsImageViewVisible", false); // here,false is default value

然后,检查MainActivity中的所有imageview的可见性

if(isImageViewVisible){
  // image is visible
} else{
  // image is invisible
}

答案 2 :(得分:0)

这很容易,你可以使用这种方法之一

1:共享偏好设置:

在您的活动中(Act1,Act2,Act3)

onClick事件中写下此代码:

 // on Act1
 final SharedPreferences sharedPreferences=getSharedPreferences("mainconf",MODE_PRIVATE);

    buttonOnAct1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            // check if isVisable and change value
            if (sharedPreferences.getBoolean("isImg1Visable",true)){
                sharedPreferences.edit().putBoolean("isImg1Visable",false).apply();
            }else {
                // show it again 
                sharedPreferences.edit().putBoolean("isImg1Visable",true).apply();
            }
        }
    });

// on Act2
 final SharedPreferences sharedPreferences=getSharedPreferences("mainconf",MODE_PRIVATE);

    buttonOnAct2.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            // check if isVisable and change value
            if (sharedPreferences.getBoolean("isImg2Visable",true)){
                sharedPreferences.edit().putBoolean("isImg2Visable",false).apply();
            }else {
                // show it again 
                sharedPreferences.edit().putBoolean("isImg2Visable",true).apply();
            }
        }
    });

// on Act3
 final SharedPreferences sharedPreferences=getSharedPreferences("mainconf",MODE_PRIVATE);

    buttonOnAct3.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            // check if isVisable and change value
            if (sharedPreferences.getBoolean("isImg3Visable",true)){
                sharedPreferences.edit().putBoolean("isImg3Visable",false).apply();
            }else {
                // show it again 
                sharedPreferences.edit().putBoolean("isImg3Visable",true).apply();
            }
        }
    });

MainActivity的代码:

public class MainActivity extends AppCompatActivity {
private ImageView imageView1;
private ImageView imageView2;
private ImageView imageView3;
private SharedPreferences sharedPreferences;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    imageView1=(ImageView)findViewById(R.id.img1);
    imageView2=(ImageView)findViewById(R.id.img2);
    imageView3=(ImageView)findViewById(R.id.img3);
    sharedPreferences=getSharedPreferences("mainconf",MODE_PRIVATE);
}


//  important PART OF CODE 
@Override
protected void onResume() {
    super.onResume();
    if (sharedPreferences.getBoolean("isImg1Visable",true)){
        imageView1.setVisibility(View.VISIBLE);
    }else {
        imageView1.setVisibility(View.GONE);
    }
    if (sharedPreferences.getBoolean("isImg2Visable",true)){
        imageView2.setVisibility(View.VISIBLE);
    }else {
        imageView2.setVisibility(View.GONE);
    }
    if (sharedPreferences.getBoolean("isImg3Visable",true)){
        imageView3.setVisibility(View.VISIBLE);
    }else {
        imageView3.setVisibility(View.GONE);
    }
}

}

2:public static Variables:

将变量写为静态 例如:

// in Your MainActivty under class
public static ImageView imageView1;
public static ImageView imageView2;
public static ImageView imageView3;


 // in your Act1 Act2 Act3

 // for hiding from mainactivity
 MainActivity.imageView1.setVisibility(View.GONE); // or View.INVISIBLE
 // for showing on main activity
 MainActivity.imageView1.setVisibility(View.VISIBLE);