【问题标题】:Android : show an image after clicking a button [closed]Android:单击按钮后显示图像[关闭]
【发布时间】:2012-03-22 11:11:51
【问题描述】:

我正在寻找这个 2 天。我不知道如何显示图像(在我的 res 文件夹中)单击按钮后如何显示图像?

【问题讨论】:

  • 你应该提供更多信息:你想如何显示它,也许包括不工作的代码(部分)......
  • 我想在单击按钮后在我的 res 文件夹中显示图像我写了这个 View.OnClickListener schedule = new View.OnClickListener() { public void onClick(View v) {//我不知道我必须在这里写什么?}我怎样才能更具体?
  • 您想在哪里显示它会非常有帮助... ImageView,在对话框中的 ImageView 中... ;)
  • 我只想全屏向用户显示图像我不知道如何使用 ImageView 等,所以我问这个问题:)
  • user1248643 你可以用 this 这是,这里 s3 是单选按钮的对象 S3.setBackgroundResource(R.drawable.abc);

标签: android image button onclick click


【解决方案1】:

活动一:

public class TestbuttontestActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        Button btn=(Button)findViewById(R.id.widget45);
        btn.setOnClickListener(new OnClickListener() {

            public void onClick(View v) { 
             Intent inf=new Intent(TestbuttontestActivity.this,Activityfullscreen.class);

             startActivity(inf);
            }
          });
    }
}

活动二:

public class Activityfullscreen extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.calc);
       ImageView img=(ImageView)findViewById(R.id.widget45);
       img.setBackgroundResource(R.drawable.digitallovesaktid);
    }
}

AndroidManifest.xml:

<application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:label="@string/app_name"
            android:name=".TestbuttontestActivity" >
            <intent-filter >
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity 
            android:theme="@android:style/Theme.Light.NoTitleBar.Fullscreen"
            android:name="Activityfullscreen"></activity>
    </application>

活动一布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >
    <Button
    android:id="@+id/widget45"
        android:layout_width="fill_parent"
        android:layout_height="120dp"
         />
         <TextView
    android:id="@+id/widget45"
        android:layout_width="fill_parent"
        android:layout_height="120dp"
         />
</LinearLayout>

活动二布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
  <ImageView
    android:id="@+id/widget45"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
         />


</LinearLayout>

第二个也是最好的方法使用 popupwindow 和两个布局,一个用于主要活动,另一个用于 popupwindow

private void showpopup()
    {
       LayoutInflater inflater = this.getLayoutInflater();
       View mView= inflater.inflate(R.layout.popup,(ViewGroup)findViewById(R.id.lnparentpopup));
       PopupWindow mPopupWindow = new PopupWindow(mView,LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT, false);
       mPopupWindow.setAnimationStyle(android.R.style.Animation_Dialog);

       TextView TV=(TextView)this.findViewById(R.id.txtmain);          
      // TableLayout L1 = (TableLayout)findViewById(R.id.tblntarialview);

       mPopupWindow.showAtLocation(TV, Gravity.CENTER, 45, 0);

     }

当用户按下按钮时,此代码将全屏显示图像,除此之外您想要什么,请分享您的代码....

【讨论】:

  • 谢谢我会试试这个。如果我想全屏显示图像,我必须创建两个活动。我无法使用方法显示图像。我说对了吗?
  • 谢谢,但我想另一种方法是使用 popupwindow 你可以试试,否则我会为你尝试...
【解决方案2】:

好的,首先请注意,Android 网站上有tutorials 和文档(如ImageView),以及全网的示例程序。

这个answer 向您展示了如何将这样的教程和全屏选项结合起来。

通常,图像显示在 ImageView 中。你调用哪种方法取决于你的图像如何可用:如果你将它作为静态资源添加到你的项目中,你可以使用setImageResource(int resId)。如果您已将其下载为位图或可绘制,请使用相应的 setX 方法。

希望这会有所帮助。

【讨论】:

  • 没有像 setBackground() 这样的方法来显示图像吗?是查看图像创建新类的唯一方法吗?
  • 目前还没试过,但是View类里有这么一个method。但是您应该问自己是否要为图像设置某些背景并在其上放置某些内容-还是只想简单明了地显示图像(-> ImageView)
  • 它显示了我的图像,但作为背景,我可以看到图像上的其他按钮。有什么方法可以显示全图而不是背景?
  • 很抱歉,你把事情搞混了。如果您想将图像“覆盖”在其他所有内容上,最好使用我上面答案中的链接中描述的新活动。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-04-28
  • 1970-01-01
  • 2021-08-16
相关资源
最近更新 更多