gpt4 book ai didi

android - 第二个按钮不起作用,但第一个可以

转载 作者:行者123 更新时间:2023-11-29 21:22:14 26 4
gpt4 key购买 nike

我创建了一个 main.xml,其中有 4 个按钮,然后我创建了 Activity,我在其中为第一个按钮编写了一个 Intent 以打开一个新 Activity 并且它起作用了。但是后来,我想对第二个按钮做同样的事情,但它没有像第一个按钮那样做......我的回答是为什么?为什么我要改变做同样的 Action ?

这是代码..

主.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>

主要 Activity .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);
}
}
}
}

第二个 Activity .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);

}
}

第二个.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>

第三个.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>

最佳答案

你有很多问题。首先,你只为你的第一个按钮设置一个点击监听器:

//buttonClick if from main.xml "@+id/buttonClick"
View btnClick = findViewById(R.id.button1);
//set event listener
findViewById(R.id.button1).setOnClickListener(this);

你应该为你的四个按钮做这件事:

findViewById(R.id.button1).setOnClickListener(this);
findViewById(R.id.button2).setOnClickListener(this);
findViewById(R.id.button3).setOnClickListener(this);
findViewById(R.id.button4).setOnClickListener(this);

然后,第二个问题出在您的 onClick 方法上,您仅在单击第一个按钮时才执行操作。如果单击四个按钮之一,您应该执行操作:

@Override
public void onClick(View arg0) {
// create a general intent
Intent intent = null;
// define an intent for all cases
switch(arg0.getId()){
case R.id.button1:
// Setting intent for first button
intent = new Intent(this,SecondActivity.class);
break;
case R.id.button2:
// Setting intent for second button
intent = new Intent(this,ThirdActivity.class);
break;
case R.id.button3:
// Setting intent for third button
intent = new Intent(this,ThirdActivity.class);
break;
case R.id.button4:
// Setting intent for fourth button
intent = new Intent(this,ThirdActivity.class);
break;
}
// start the Activity selected at the end
this.startActivity(intent);
}

关于android - 第二个按钮不起作用,但第一个可以,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20596037/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com