我创建了一个main.xml,其中我有4个按钮,然后创建了Activity,在那里我为第一个按钮编写了一个Intent,以打开一个新的活动并且它工作。但是,然后,我想为第二个按钮做同样的事情,但它没有像第一个那样做。我的答案是为什么?为什么我应该改变做同样的行动?第二个按钮不起作用,但第一个是
这里是代码..
的main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<Button
android:id="@+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/noua" />
<Button
android:id="@+id/button2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/zece" />
<Button
android:id="@+id/button3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/unspe" />
<Button
android:id="@+id/button4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/doispe" />
</LinearLayout>
MainActivity.java:
package com.orar;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.View;
import android.view.View.OnClickListener;
//implement the OnClickListener interface
public class MainActivity extends Activity
implements OnClickListener {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//get the Button reference
//Button is a subclass of View
//buttonClick if from main.xml "@+id/buttonClick"
View btnClick = findViewById(R.id.button1);
//set event listener
btnClick.setOnClickListener(this);
}
//override the OnClickListener interface method
@Override
public void onClick(View arg0) {
if(arg0.getId() == R.id.button1){
//define a new Intent for the second Activity
Intent intent = new Intent(this,SecondActivity.class);
//start the second Activity
this.startActivity(intent);
}
}
从这里开始的第二个按钮和问题...
//implement the OnClickListener interface
public class MainActivity extends Activity
implements OnClickListener {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//get the Button reference
//Button is a subclass of View
//buttonClick if from main.xml "@+id/buttonClick"
View btnClick = findViewById(R.id.button2);
//set event listener
btnClick.setOnClickListener(this);
}
//override the OnClickListener interface method
@Override
public void onClick(View arg0) {
if(arg0.getId() == R.id.button2){
//define a new Intent for the second Activity
Intent intent2 = new Intent(this,ThirdActivity.class);
//start the second Activity
this.startActivity(intent2);
}
}
}
}
秒ondActivity.java:
package com.orar;
import android.app.Activity;
import android.os.Bundle;
public class SecondActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setContentView(R.layout.second);
}
}
ThirdActivity.java:
package com.orar;
import android.app.Activity;
import android.os.Bundle;
public class ThirdActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setContentView(R.layout.third);
}
}
second.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="This is the second Activity"
/>
</LinearLayout>
third.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="This is the third Activity"
/>
</LinearLayout>
有一个在你的活动没有第二个按钮的电话。你可以分享你的尝试,并告诉我们什么不按预期工作? – gahfy
我应该如何在我的活动中调用我的第二个按钮?我试图改变的东西,使第二个按钮开始一个新的活动,但我没有......我应该改变第一个按钮的代码,为第二个按钮做同样的动作? – user3104504