简体   繁体   English

切换到不同的活动onClick

[英]Switch to different Activities onClick

I am new to Android and exploring it at the moment. 我是Android的新手,目前正在探索它。 I have two Image Buttons which have to load different activities onClick. 我有两个图像按钮,它们必须加载不同的onClick活动。

ImageButton btn1= (ImageButton)findViewById(R.id.timetable);
btn1.setOnClickListener(btnListener1);

ImageButton btn2= (ImageButton)findViewById(R.id.location);
btn2.setOnClickListener(btnListener2);
private OnClickListener btnListener1 = new OnClickListener()
{
    public void onClick(View view)
    {                        
         Intent myIntent = new Intent(getBaseContext(), HelloWorld1.class);
         startActivity(myIntent);
    }
};

private OnClickListener btnListener2 = new OnClickListener()
{
    public void onClick(View view)
    {                 
        Intent myIntent2 = new Intent(getBaseContext(), HelloWorld2.class);         
        startActivity(myIntent2);
    }
};

//my manifest ...... //我的清单...

<activity android:name="myApp" android:label="@string/app_name">
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
</activity>        
<activity android:name=".HelloWorld1"></activity>
<activity android:name=".HelloWorld2"></activity>

//and my main.xml //和我的main.xml

<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout
android:id="@+id/widget34"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
android:background="#ffffff"
>

<GridView
    android:id="@+id/widget36"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:numColumns="2"
    android:layout_x="110px"

    android:layout_y="32px"
    android:layout_centerInParent="true">
</GridView>
<ImageButton
    android:id="@+id/timetable"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_x="210px"
    android:layout_y="142px"
    android:background="@drawable/icon2">
</ImageButton>
<ImageButton
    android:id="@+id/location"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_x="100px"
    android:layout_y="342px"
    android:background="@drawable/icon">
</ImageButton>

This code causes errors, could anyone point where I am going wrong please. 此代码会导致错误,请任何人指出我要去哪里。 Many thanks in advance. 提前谢谢了。

Let's put aside the finish() method since i dont know what the heck it's doing there :) Case1: Look careful at your activity xml view file, you might accidentally define your button as Button instead of ImageButton -> Error 让我们搁置一下finish()方法,因为我不知道它在做什么:)案例1:仔细看一下您的活动xml视图文件,您可能不小心将按钮定义为Button而不是ImageButton-> Error

Case2: dont use view.getContext() , instead use getBaseContext() or getApplicationContext() 情况2:不要使用view.getContext() ,而是使用getBaseContext()getApplicationContext()

You are calling startActivityForResult() and then immediately calling finish() . 您正在调用startActivityForResult() ,然后立即调用finish() Where is the result going to go if the activity is finished? 如果活动结束,结果将去哪里?

What behavior do you want and what are you getting instead. 您想要什么行为,而您得到什么。 The more specific you can be, the better the quality of help you will get. 您可以越具体,获得的帮助质量就越高。

Try to write something simillar like code below. 尝试编写类似下面的代码的东西。 You can also define first function which will create new activity when button will be pressed. 您还可以定义第一个功能,当按下按钮时,该功能将创建新的活动。

public class HelloAndroid extends Activity {
private Button button_1;
private Button button_2;

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

private void initialiyeFields(){
    button_1 = (Button)findViewById(R.id.button1);
    button_1.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(HelloAndroid.this, HelloWord1.class);
            startActivity(intent);
        }
    });

    button_2 = (Button)findViewById(R.id.button2);
    button_2.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(HelloAndroid.this, HelloWord2.class);
            startActivity(intent);
        }
    });
}

} }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM