我已經很少有Android開發經驗(幸好有stackoverflow,一直在幫我很多)。我想要做的是在點擊某個特定菜單選項時顯示一個imageview。我有菜單/子菜單正常工作,我知道我可以使圖像看不見android:visibility="invisible"
,我想我可以使它在Java代碼中可見imageView.visiblity="visible"
我認爲(如果不是,請更正我)。我被告知我可以使用「onClickListener事件」來做到這一點,但我讀過的文檔對我沒有任何幫助。 如果任何人都可以請幫助我,這將不勝感激。用onClick監聽器事件顯示圖像
請參考以下照片和我的Java/XML代碼。
我的Java代碼到目前爲止(也是我在Java中創建我的菜單/子菜單,不是我不知道XML,如果這是不好的做法,但希望有人會告訴我的。):到目前爲止
package com.example.android.postvu;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.ContextMenu;
import android.view.SubMenu;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
}
@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_main, 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);
}
public void myOnClickMethod(View v) {
registerForContextMenu(v);
openContextMenu(v);
}
final int CONTEXT_MENU_VIEW = 1;
final int CONTEXT_MENU_EDIT = 2;
final int MENU_SORT = 3;
final int MENU_SORT_BY_NAME = 4;
final int MENU_SORT_BY_ADDRESS = 5;
@Override
public void onCreateContextMenu (ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
menu.setHeaderTitle("Options");
menu.add(Menu.NONE, CONTEXT_MENU_VIEW, Menu.NONE, "Take Photo");
menu.add(Menu.NONE, CONTEXT_MENU_EDIT, Menu.NONE, "Photo Album");
SubMenu sub=menu.addSubMenu(Menu.NONE, MENU_SORT, Menu.NONE, "Plain Image");
sub.add(Menu.NONE, MENU_SORT_BY_NAME, Menu.NONE, "GridVu");
sub.add(Menu.NONE, MENU_SORT_BY_ADDRESS, Menu.NONE, "Story");
}
}
我的XML代碼:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:showIn="@layout/activity_main"
tools:context="com.example.android.postvu.MainActivity">
<ImageView
android:id="@+id/imageView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="@string/background"
android:scaleType="centerCrop"
android:src="@drawable/grid"
tools:layout_editor_absoluteY="0dp"
android:layout_marginRight="7dp"
android:layout_marginEnd="7dp"
tools:ignore="MissingConstraints" />
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginTop="8dp"
android:text="@string/image_text_editor"
android:textColor="@android:color/black"
android:textSize="28sp"
android:textStyle="bold"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:text="Image Text Editor"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp" />
<Button
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_marginBottom="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginTop="8dp"
android:background="@mipmap/ic_launcher"
android:scaleType="centerCrop"
android:onClick="myOnClickMethod"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView"
app:layout_constraintVertical_bias="0.98"
android:id="@+id/button"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp" />
<ImageView
android:id="@+id/imageView3"
android:layout_width="450dp"
android:layout_height="450dp"
android:layout_marginBottom="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginTop="8dp"
android:contentDescription="@string/white_background"
android:visibility="invisible"
app:layout_constraintBottom_toBottomOf="@+id/button"
app:layout_constraintLeft_toLeftOf="@+id/imageView2"
app:layout_constraintRight_toRightOf="@+id/imageView2"
app:layout_constraintTop_toTopOf="@+id/textView"
app:srcCompat="@mipmap/white" />
</android.support.constraint.ConstraintLayout>
是否要在菜單項上顯示/隱藏ImageView點擊 –
是的。基本上當有人點擊以下任一選項時:'''sub.add(Menu.NONE,MENU_SORT_BY_NAME,Menu.NONE,「GridVu」); sub.add(Menu.NONE,MENU_SORT_BY_ADDRESS,Menu.NONE,「Story」);'''imageview顯示@SachinBahukhandi –