2014-06-08 68 views
3

请耐心等待,我是新来的android编程。我试图在android中点击按钮显示图像,但不幸的是我的模拟器没有显示所需的输出。这是我的layout.xml文件模拟器不显示按钮上的图像点击Android

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:paddingBottom="@dimen/activity_vertical_margin" 
android:paddingLeft="@dimen/activity_horizontal_margin" 
android:paddingRight="@dimen/activity_horizontal_margin" 
android:paddingTop="@dimen/activity_vertical_margin" 
tools:context=".MainActivity" > 


<Button 
    android:id="@+id/button1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignLeft="@+id/textView1" 
    android:layout_below="@+id/textView1" 
    android:layout_marginTop="14dp" 
    android:text="Button" /> 

</RelativeLayout> 

main_activity.java

package com.example.app1; 

import android.os.Bundle; 
import android.app.Activity; 
import android.view.Menu; 
import android.widget.Button; 
import android.widget.ImageView; 
import android.view.View; 
import android.view.View.OnClickListener; 

public class MainActivity extends Activity { 
Button button; 
ImageView image; 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
     } 


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


public void addListenerOnButton() { 

    button = (Button) findViewById(R.id.button1); 

    button.setOnClickListener(new OnClickListener() { 

     @Override 
     public void onClick(View arg0) { 


        image.setImageResource(R.drawable.optimus); 

     } 

    }); 


} 

}

我已经列入/ RES /绘制-MDPI的图像文件。当我尝试在模拟器上运行时,它不会在按钮单击时显示图像。代码有问题吗?在使用它之前

Button theButton = (Button)findViewById(R.id.button1); 
theButton.setOnClickListener(new OnClickListener() { 
     public void onClick(View v) { 
      // TODO Auto-generated method stub 

theButton.setBackgroundResource(R.drawable.optimus); 
     } 
+0

你甚至没有在你的Java文件中的按钮,更不用说一个点击监听器或图像 –

+0

我没有得到你。请你可以编辑我的代码并指出我的错误。 Thx提前预测。 – Darpit

+1

您的布局中没有** ImageView **。你如何假装把图像放在一个不存在的物体上?并且您也没有名为** textView1 **的控件**'android:layout_alignLeft =“@ + id/textView1”'或者我应该假定您**已经损坏了您的布局?你能完全展示**吗? –

回答

0

假设你有一个ImageView的所谓imageView1
你必须给它分配,:

1

在你onCreate()方法写这个吧。

所以改变这样的:

@Override 
    public void onClick(View arg0) 
    { 
     image.setImageResource(R.drawable.optimus); 
    } 

这个

@Override 
    public void onClick(View arg0) 
    { 
     image = (ImageView) findViewById(R.id.imageView1); 
     image.setImageResource(R.drawable.optimus); 
    } 
+0

让我知道,如果这个工程,并尝试这个时删除你的addListenerOnButton()方法。谢谢。 – BugaIulian

+0

兄弟工作的魅力...感谢您的回复... – Darpit

0

你必须写这样的代码的onclick方法,你也需要调用addListenerOnButton方法在onCreate()方法

package com.example.app1; 

import android.os.Bundle; 
import android.app.Activity; 
import android.view.Menu; 
import android.widget.Button; 
import android.widget.ImageView; 
import android.view.View; 
import android.view.View.OnClickListener; 

public class MainActivity extends Activity { 
    Button button; 

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

     addListenerOnButton(); 
    } 


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


public void addListenerOnButton() { 

    button = (Button) findViewById(R.id.button1); 

    button.setOnClickListener(new OnClickListener() { 

    @Override 
    public void onClick(View arg0) { 


       button.setBackgroundResource(R.drawable.optimus); 

    } 

    }); 


} 

}

0

试试这个:既然你已经在使用XML布局

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:id="@+id/background" <!-- you're adding this here --> 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:paddingBottom="@dimen/activity_vertical_margin" 
android:paddingLeft="@dimen/activity_horizontal_margin" 
android:paddingRight="@dimen/activity_horizontal_margin" 
android:paddingTop="@dimen/activity_vertical_margin" 
tools:context=".MainActivity" > 


<Button 
    android:id="@+id/button1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignLeft="@+id/textView1" 
    android:layout_below="@+id/textView1" 
    android:layout_marginTop="14dp" 
    android:onClick="showImage" <!-- you're adding this here --> 
    android:text="Button" /> 

</RelativeLayout> 

然后在java文件...

Package com.example.app1; 

import android.os.Bundle; 
import android.app.Activity; 
import android.view.Menu; 
import android.widget.Button; 
import android.widget.ImageView; 
import android.view.View; 
import android.view.View.OnClickListener; 

public class MainActivity extends Activity { 
Button button; 
ImageView image; 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
     } 


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


public void showImage(View view) { 
    RelativeLayout background = (RelativeLayout) findViewById(R.id.background); 
    background.setBackgroundResource(R.drawable.optimus); 
} /* This is a whole new section of code to replace the onClick listener */ 

} 

,应尽可能使用内置的onClick监听器。您可以通过在名为yourFunctionName的.java文件中命名一个函数,并使用一个参数(View view)来完成此操作。 然后,在您的XML中,您需要通过为需要onClick方法的视图添加android:onClick="yourFunctionName"来指向它。

最后,您从未将您所指的图像实际指向任何东西。你只是说,对象“图像”现在使用“R.drawable.optimus”,但该图像从未使用过!相反,我只是指出把它放在你的RelativeView的背景中。如果您想在单独的图像中使用它,则必须将ImageView添加到XML文件中,并以相同的方式为ImageView设置背景资源。

学习Android最好运气!

+0

我认为您的解决方案遵循最佳编码实践通过使用内置onClick监听器。感谢您的解释,这对新手来说意义重大。 – Darpit

+0

嘿,我们都是从一开始就开始的。即使你是“有经验的”,我们仍然是新手。如果我的回答有帮助,请记住加注并“接受”答案。谢谢。 –