我正在为一个博客应用程序编写代码,该应用程序将仅发布文本或同时发布文本和其下方的图像。我在布局中定义了 editText 和 imageView。我想要这样,除非我单击图库按钮,否则 ImageView 不会出现。
我有意尝试这样做,但添加 Activity 会使应用程序变得太重。
private ImageButton newPostImgbtn;
private ImageView newPostImage;
private EditText newPostDesc;
private Button newPostBtn;
@Override
protected void onCreate(Bundle savedInstanceState) {
setTheme(R.style.HomeTheme);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_post);
newPostImage = findViewById(R.id.imageView);
newPostImgbtn = findViewById(R.id.imageButton2);
newPostDesc = findViewById(R.id.editText);
newPostBtn = findViewById(R.id.button2);
}
我创建了新的 Activity imageactivity,只是通过在画廊 newimagebtn 的按钮上放置一个 onclick 监听器来添加图像,但我不想在另一个 Activity 中执行此操作。
public void post(View v) { Intent intent = new Intent(postActivity.this,
imageActivity.class); startActivity(intent);}
此外,如果用户单击图库按钮,但未选择任何图像,则单击发布按钮时,只会显示文本,而不会出现空 ImageView 区域或错误,因为该字段为空。请帮助我。
最佳答案
首先,您必须获得用户的许可并打开图库 Intent 。当您找到图像 url 时,您需要使用图像加载器库(如 glide)显示它。单击按钮时,您需要做的最后一件事是检查图像 url 是否为空。如果您需要进一步的帮助,请告诉我。
关于java - 显示 imageview onclick 并发布,如果为空则不显示 imageview,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57304063/