如何在按下另一个可点击的 ImageView 时使一个 ImageView 不可见?
How to make one ImageView invisible when another clickable ImageView is pressed?
我试图在按下 ImageView(Seethrough) 时让 ImageView(bul1) 消失。当我尝试 运行 这段代码时出现空指针错误。有什么问题吗?
JAVA代码
public class MainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final ImageView seethrough1 = (ImageView) findViewById(R.id.Seethrough);
final ImageView view1 = (ImageView) findViewById(R.id.bul1);
seethrough1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(view1.getVisibility() == View.VISIBLE)
{
view1.setVisibility(View.INVISIBLE);
}
}
});
}
XML代码
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:layout_width="match_parent"
android:layout_height="300dp"
android:paddingBottom="6dp"
android:src="@drawable/gun"
android:clickable="true"
android:id="@+id/Seethrough"
android:onClick="next"
/>
<ImageView
android:layout_width="30dp"
android:layout_height="wrap_content"
android:src="@drawable/bullet"
android:id="@+id/bul1"
/>
</LinearLayout>
您需要协调 seethrough 的 onClickListener 及其 onClick XML 属性。我建议从 xml:
中删除这一行
android:onClick="next"
并将代码放入您的下一个方法(如果您有)
public void next (View v){
some code
}
在你的可见性检查之后或之前,以更适合你的为准:
@Override
public void onClick(View v) {
//place some code here
if(view1.getVisibility() == View.VISIBLE){
view1.setVisibility(View.INVISIBLE);
}
//or here
}
我认为他们的 xml 代码有问题,请尝试如下编写 xml,
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<ImageView
android:layout_width="match_parent"
android:layout_height="300dp"
android:paddingBottom="6dp"
android:src="@drawable/gun"
android:clickable="true"
android:id="@+id/Seethrough"
android:onClick="next"
/>
<ImageView
android:layout_width="30dp"
android:layout_height="wrap_content"
android:src="@drawable/bullet"
android:id="@+id/bul1"
/>
</LinearLayout>
如果是return NullPointerExeption,我认为你的ImageView是Null,因为
setContentView(R.layout.activity_main);
和 activity_main.xml 与您 post 的内容不一样,请检查布局名称并重试。
我发现我应该在方法内而不是之前声明图像视图。
像这样
public void onClick(View v) {
ImageView seethrough1 = (ImageView) findViewById(R.id.Seethrough);
我试图在按下 ImageView(Seethrough) 时让 ImageView(bul1) 消失。当我尝试 运行 这段代码时出现空指针错误。有什么问题吗?
JAVA代码
public class MainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final ImageView seethrough1 = (ImageView) findViewById(R.id.Seethrough);
final ImageView view1 = (ImageView) findViewById(R.id.bul1);
seethrough1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(view1.getVisibility() == View.VISIBLE)
{
view1.setVisibility(View.INVISIBLE);
}
}
});
}
XML代码
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:layout_width="match_parent"
android:layout_height="300dp"
android:paddingBottom="6dp"
android:src="@drawable/gun"
android:clickable="true"
android:id="@+id/Seethrough"
android:onClick="next"
/>
<ImageView
android:layout_width="30dp"
android:layout_height="wrap_content"
android:src="@drawable/bullet"
android:id="@+id/bul1"
/>
</LinearLayout>
您需要协调 seethrough 的 onClickListener 及其 onClick XML 属性。我建议从 xml:
中删除这一行android:onClick="next"
并将代码放入您的下一个方法(如果您有)
public void next (View v){
some code
}
在你的可见性检查之后或之前,以更适合你的为准:
@Override
public void onClick(View v) {
//place some code here
if(view1.getVisibility() == View.VISIBLE){
view1.setVisibility(View.INVISIBLE);
}
//or here
}
我认为他们的 xml 代码有问题,请尝试如下编写 xml,
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<ImageView
android:layout_width="match_parent"
android:layout_height="300dp"
android:paddingBottom="6dp"
android:src="@drawable/gun"
android:clickable="true"
android:id="@+id/Seethrough"
android:onClick="next"
/>
<ImageView
android:layout_width="30dp"
android:layout_height="wrap_content"
android:src="@drawable/bullet"
android:id="@+id/bul1"
/>
</LinearLayout>
如果是return NullPointerExeption,我认为你的ImageView是Null,因为
setContentView(R.layout.activity_main);
和 activity_main.xml 与您 post 的内容不一样,请检查布局名称并重试。
我发现我应该在方法内而不是之前声明图像视图。
像这样
public void onClick(View v) {
ImageView seethrough1 = (ImageView) findViewById(R.id.Seethrough);