我在主屏幕上有两个按钮。当我单击悲伤的那一个时,它可以工作,但是当我单击快乐的那一个时,它不起作用(“ Feliz”按钮)。我已经尝试了一切,但实际上我不知道在哪里放置代码或放置什么。这是我所拥有的:
package com.example.clara;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btn1 = findViewById(R.id.btn1);
Button btn2 = findViewById(R.id.btn2);
btn1.setOnClickListener(this);
btn2.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch(v.getId()){
case R.id.btn1:
Intent intent = new Intent(this,ThirdActivity.class);
startActivity(intent);
Toast.makeText(this, "Yaaay",Toast.LENGTH_SHORT).show();
break;
case R.id.btn2:
Intent tristeza = new Intent(this,SecondActivity.class);
startActivity(tristeza);
Toast.makeText(this, "Noooo",Toast.LENGTH_SHORT).show();
break;
}
}
}
我删除了方法,但仍然无法正常工作。这是此应用的清单:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.clara">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".ThirdActivity"></activity>
<activity android:name=".SecondActivity" />
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
布局:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:background="@drawable/claralinda">
<TextView
android:id="@+id/text2"
android:layout_width="95dp"
android:layout_height="46dp"
android:layout_marginStart="150dp"
android:layout_marginLeft="150dp"
android:layout_marginTop="300dp"
android:layout_marginEnd="150dp"
android:layout_marginRight="150dp"
android:background="#0c3d46"
android:text="@string/Hola"
android:textColor="#c1f1ab"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/btn1"
android:layout_width="125dp"
android:layout_height="55dp"
android:layout_marginStart="32dp"
android:layout_marginLeft="32dp"
android:layout_marginTop="190dp"
android:layout_marginEnd="220dp"
android:layout_marginRight="220dp"
android:layout_marginBottom="140dp"
android:background="#0c3d46"
android:text="@string/second_page"
android:textColor="#71cfae"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.101"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/text2" />
<Button
android:id="@+id/btn2"
android:layout_width="125dp"
android:layout_height="55dp"
android:layout_marginStart="220dp"
android:layout_marginLeft="220dp"
android:layout_marginTop="190dp"
android:layout_marginEnd="32dp"
android:layout_marginRight="32dp"
android:layout_marginBottom="140dp"
android:background="#0c3d46"
android:text="@string/second_page_sad"
android:textColor="#71cfae"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.902"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/text2" />
</android.support.constraint.ConstraintLayout>
答案 0 :(得分:0)
尝试一下
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<activity android:name=".ThirdActivity"></activity>
<activity android:name=".SecondActivity"></activity>
</application>