【问题标题】:what do i have to do to make a imagebutton launch a other app in android我该怎么做才能让imagebutton在android中启动另一个应用程序
【发布时间】:2012-03-15 11:53:57
【问题描述】:

我正在创建一个应用程序。但现在我希望它用图像按钮打开另一个应用程序。我在谷歌上搜索了 5 天,有很多遮阳篷,但不是合适的。

有遮阳篷,但没有完整的解释。

我找到了这部分

Intent intent = new Intent(Intent.ACTION_MAIN); intent.setComponent(new ComponentName("com.package.address","com.package.address.MainActivity")); startActivity(intent);

但是我必须把它放在哪里才能在我的图像按钮上工作。以及我必须做哪些课程或事情。

这是我的 appactity.java

    package eu.cornholio.rom;

import android.app.Activity;
import android.os.Bundle;

public class CornholioROMActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }


}

这是我的 main.xml

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

    <ImageView
        android:id="@+id/imageView1"
        android:gravity="center"
         android:layout_gravity="center"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/cornh" android:contentDescription="@string/cornholio"/>

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="@string/cornholio" />

    <ImageButton
        android:id="@+id/imageButton1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/cwm" android:clickable="true" android:contentDescription="@string/cmw"/>

    <ImageButton
        android:id="@+id/imageButton2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/jkay"
         android:clickable="true"
          android:contentDescription="@string/jkay"/>


</LinearLayout>            

这是我的代码

    package eu.cornholio.rom;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;

public class CornholioROMActivity extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        //finding your image button
        ImageButton btn = (ImageButton) findViewByid(R.id.imageButton1);

        //setting onClick listener
        btn.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(Intent.ACTION_MAIN);
                intent.setComponent(new ComponentName("eu.chainfire.cfroot.cwmmanager",
                                       "eu.chainfire.cfroot.cwmmanager.MainActivity"));
                startActivity(intent); 
            }
        });

    }

}

打个招呼

【问题讨论】:

  • 是的,正好在你的 ImageButton 的 onClicklistner 中

标签: android eclipse android-intent


【解决方案1】:

将 onClickListener 设置为您的 ImageButton。

yourImageButton.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View v) {
        Intent intent = new Intent(Intent.ACTION_MAIN);
        intent.setComponent(new ComponentName("com.package.address",
                               "com.package.address.MainActivity"));
        startActivity(intent); 
    }
});

更新

例如,如果您想在imageButton1 上设置点击操作,那么您需要在您的onCreate 方法中进行以下更改:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    //finding your image button
    ImageButton btn = (ImageButton) findViewById(R.id.imageButton1);

    //setting onClick listener
    btn.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(Intent.ACTION_MAIN);
            intent.setComponent(new ComponentName("com.package.address",
                                   "com.package.address.MainActivity"));
            startActivity(intent); 
        }
    });

}

【讨论】:

  • ok 我必须把这个推到哪里?在按钮所在的xml里面?
  • 在您的代码中;在找到您的控件并引用它之后
  • ok,但是在什么文件中,我的按钮在 main.xml 中,这是按钮
  • 将您的活动代码粘贴到您的问题中,以便我可以适当地帮助您
  • 1. ComponentName 无法解析为类型 2. Imagebutton 无法解析为类型 3. Imagebutton 无法解析为类型 4. OnClickListener 无法解析为类型 5. 构造函数 Intent(string) 未定义 6. 方法 findViewByIf( int) 未定义类型 CornholioROMActifity 7 视图无法解析为类型
【解决方案2】:

你必须为你的 ImageButton 定义一个 OnClickListener。

这看起来像这样:

  myFancyButton.setOnClickListener(new OnClickListener() {
  public void onClick(View v) {
    Intent intent = new Intent(Intent.ACTION_MAIN);
    intent.setComponent(new ComponentName("com.package.address","com.package.address.MainActivity"));
    startActivity(intent);
  }
});

【讨论】:

    【解决方案3】:

    您也可以在不知道主要活动的情况下调用另一个应用程序:

    imageButton.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
              Intent intent = getPackageManager().getLaunchIntentForPackage("com.package.address"); 
              startActivity(intent);
    
            }
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多