gpt4 book ai didi

Android Studio,根据另一个 Activity 的 int 值设置文本可见性

转载 作者:行者123 更新时间:2023-11-30 01:53:42 25 4
gpt4 key购买 nike

我正在尝试根据另一个 Activity 的整数值在一个 Activity 中设置 TextView 可见性。理想情况下,我试图将 imgID 的值设置为一个 int,并且根据整数值,第二个 Activity 设置 10 个 TextView 的可见性。每当在主要 Activity (图片)中单击 ImageView 时,imgID 值都应该设置为一个数字。但是,当我运行该程序时,无论我单击哪张图片,所有文本都会出现。这是我在一门课上的第二个实验,我有点挣扎,所以如果我错过了一些明显的东西,我深表歉意。此外,感谢任何其他提示或关于我错过的其他事情的信息。感谢您提前帮助!

代码如下:

主要 Activity :

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.content.Intent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;

public class Pictures extends AppCompatActivity {
static int imgID;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_pictures);

//register click event listener to image view(I hope :)
ImageView imgB = (ImageView) findViewById(R.id.imageViewB);
ImageView imgH = (ImageView) findViewById(R.id.imageViewH);
ImageView imgM = (ImageView) findViewById(R.id.imageViewM);
ImageView imgD = (ImageView) findViewById(R.id.imageViewD);
ImageView imgS = (ImageView) findViewById(R.id.imageViewS);
imgB.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
//if imgB clicked, set imgID to 1 and so on...
imgID = 1;
startActivity(new Intent(Pictures.this, Lab2App2.class));
}
});


imgD.setOnClickListener(new OnClickListener() {


@Override
public void onClick(View v) {
imgID = 2;
startActivity(new Intent(Pictures.this, Lab2App2.class));
}
});



imgH.setOnClickListener(new OnClickListener() {


@Override
public void onClick(View v) {
imgID = 3;
startActivity(new Intent(Pictures.this, Lab2App2.class));
}
});


imgM.setOnClickListener(new OnClickListener() {


@Override
public void onClick(View v) {
imgID = 4;
startActivity(new Intent(Pictures.this, Lab2App2.class));
}
});
imgS.setOnClickListener(new OnClickListener() {


@Override
public void onClick(View v) {
imgID = 5;
startActivity(new Intent(Pictures.this, Lab2App2.class));
}
});

}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_pictures, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();

//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}

return super.onOptionsItemSelected(item);

}




}

第二个 Activity :

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;

public class Lab2App2 extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_lab2_app2);
TextView campusB = (TextView)findViewById(R.id.text_campusB);
TextView campusD = (TextView)findViewById(R.id.text_campusD);
TextView campusH = (TextView)findViewById(R.id.text_campusH);
TextView campusM = (TextView)findViewById(R.id.text_campusM);
TextView campusS = (TextView)findViewById(R.id.text_campusH);
if(Pictures.imgID == 1) {

campusB.setVisibility(View.VISIBLE);
campusD.setVisibility(View.GONE);
campusH.setVisibility(View.GONE);
campusM.setVisibility(View.GONE);
campusS.setVisibility(View.GONE);

}
else if(Pictures.imgID == 2) {

campusB.setVisibility(View.GONE);
campusD.setVisibility(View.VISIBLE);
campusH.setVisibility(View.GONE);
campusM.setVisibility(View.GONE);
campusS.setVisibility(View.GONE);

}
else if(Pictures.imgID == 3) {

campusB.setVisibility(View.GONE);
campusD.setVisibility(View.GONE);
campusH.setVisibility(View.VISIBLE);
campusM.setVisibility(View.GONE);
campusS.setVisibility(View.GONE);

}
else if(Pictures.imgID == 4) {

campusB.setVisibility(View.GONE);
campusD.setVisibility(View.GONE);
campusH.setVisibility(View.GONE);
campusM.setVisibility(View.VISIBLE);
campusS.setVisibility(View.GONE);

}
else if(Pictures.imgID == 5) {

campusB.setVisibility(View.GONE);
campusD.setVisibility(View.GONE);
campusH.setVisibility(View.GONE);
campusM.setVisibility(View.GONE);
campusS.setVisibility(View.VISIBLE);

}
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_lab2_app2, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();

//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}

return super.onOptionsItemSelected(item);
}
}

最佳答案

在 android 中,通常您应该将 imgID 等参数传递给 Intent ,然后从新的 startet Activity 中获取这些值。

这里是一个简短的代码示例:

  Intent intent = new Intent(Pictures.this, Lab2App2.class);
intent.putExtra("imgID", imgID);

//startActivityForResult(intent, YOUR_RETURN_CODE);
startActivity(intent);

如果您使用 startActivitiyForResult() 调用 Activity ,则需要 RETURN_CODE,以便您知道哪个 Activity 返回了您的结果。

在 Lab2App2 Activity 中,您可以使用以下方法获取 int:

getIntent().getIntExtra("imgID",DEFAULT_VALUE);

直接访问静态变量不是一个好习惯。有很多不同的方法可以做到这一点。但这是最安全和最简单的解决方案......例如用于恢复实例状态等。始终建议使用 Intent 。

希望对您有所帮助。干杯

关于Android Studio,根据另一个 Activity 的 int 值设置文本可见性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32596164/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com