utilização do tts para saudação
This commit is contained in:
@@ -14,6 +14,9 @@
|
|||||||
<activity
|
<activity
|
||||||
android:name=".MenuActivity"
|
android:name=".MenuActivity"
|
||||||
android:exported="false" />
|
android:exported="false" />
|
||||||
|
<activity
|
||||||
|
android:name=".NomeActivity"
|
||||||
|
android:exported="false" />
|
||||||
<activity
|
<activity
|
||||||
android:name=".MainActivity"
|
android:name=".MainActivity"
|
||||||
android:exported="true" >
|
android:exported="true" >
|
||||||
|
|||||||
@@ -4,7 +4,6 @@ import android.content.Intent;
|
|||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
import android.view.View;
|
import android.view.View;
|
||||||
import android.widget.Button;
|
import android.widget.Button;
|
||||||
import android.widget.Toast;
|
|
||||||
|
|
||||||
import androidx.activity.EdgeToEdge;
|
import androidx.activity.EdgeToEdge;
|
||||||
import androidx.appcompat.app.AppCompatActivity;
|
import androidx.appcompat.app.AppCompatActivity;
|
||||||
@@ -32,7 +31,7 @@ public class MainActivity extends AppCompatActivity {
|
|||||||
@Override
|
@Override
|
||||||
public void onClick(View v) {
|
public void onClick(View v) {
|
||||||
//Toast.makeText(MainActivity.this, "SOU O MAIOR!!!!", Toast.LENGTH_SHORT).show();
|
//Toast.makeText(MainActivity.this, "SOU O MAIOR!!!!", Toast.LENGTH_SHORT).show();
|
||||||
Intent intent = new Intent(MainActivity.this, MenuActivity.class);
|
Intent intent = new Intent(MainActivity.this, NomeActivity.class);
|
||||||
startActivity(intent);
|
startActivity(intent);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|||||||
83
app/src/main/java/pt/epvc/dreambar/NomeActivity.java
Normal file
83
app/src/main/java/pt/epvc/dreambar/NomeActivity.java
Normal file
@@ -0,0 +1,83 @@
|
|||||||
|
package pt.epvc.dreambar;
|
||||||
|
|
||||||
|
import android.content.Context;
|
||||||
|
import android.content.Intent;
|
||||||
|
import android.content.SharedPreferences;
|
||||||
|
import android.os.Bundle;
|
||||||
|
import android.speech.tts.TextToSpeech;
|
||||||
|
import android.view.View;
|
||||||
|
import android.widget.Button;
|
||||||
|
import android.widget.EditText;
|
||||||
|
import android.widget.Toast;
|
||||||
|
|
||||||
|
import androidx.activity.EdgeToEdge;
|
||||||
|
import androidx.appcompat.app.AppCompatActivity;
|
||||||
|
import androidx.core.graphics.Insets;
|
||||||
|
import androidx.core.view.ViewCompat;
|
||||||
|
import androidx.core.view.WindowInsetsCompat;
|
||||||
|
|
||||||
|
import java.util.Locale;
|
||||||
|
|
||||||
|
public class NomeActivity extends AppCompatActivity {
|
||||||
|
|
||||||
|
private Button seguinteButton;
|
||||||
|
private EditText nomeEditText;
|
||||||
|
|
||||||
|
private TextToSpeech tts;
|
||||||
|
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void onCreate(Bundle savedInstanceState) {
|
||||||
|
super.onCreate(savedInstanceState);
|
||||||
|
EdgeToEdge.enable(this);
|
||||||
|
setContentView(R.layout.activity_nome);
|
||||||
|
ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
|
||||||
|
Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
|
||||||
|
v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
|
||||||
|
return insets;
|
||||||
|
});
|
||||||
|
|
||||||
|
seguinteButton = findViewById(R.id.seguinteButton);
|
||||||
|
nomeEditText = findViewById(R.id.nomeEditText);
|
||||||
|
|
||||||
|
tts = new TextToSpeech(this, status -> {
|
||||||
|
if (status == TextToSpeech.SUCCESS) {
|
||||||
|
int lang = tts.setLanguage(new Locale("pt", "PT")); // pt-PT
|
||||||
|
if (lang == TextToSpeech.LANG_MISSING_DATA || lang == TextToSpeech.LANG_NOT_SUPPORTED) {
|
||||||
|
// fallback
|
||||||
|
tts.setLanguage(Locale.US);
|
||||||
|
}
|
||||||
|
|
||||||
|
tts.setSpeechRate(1.0f); // 1.0 = normal
|
||||||
|
tts.setPitch(1.0f); // 1.0 = normal
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
seguinteButton.setOnClickListener(new View.OnClickListener() {
|
||||||
|
@Override
|
||||||
|
public void onClick(View v) {
|
||||||
|
String nome = nomeEditText.getText().toString();
|
||||||
|
if (nome.isBlank()){
|
||||||
|
Toast.makeText(NomeActivity.this, "Tem que preencher o nome", Toast.LENGTH_SHORT).show();
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
SharedPreferences prefs = getSharedPreferences("dados", Context.MODE_PRIVATE);
|
||||||
|
prefs.edit()
|
||||||
|
.putString("nome", nome)
|
||||||
|
.apply();
|
||||||
|
speak("Olá " + nome + "! Como te posso ajudar?");
|
||||||
|
Intent intent = new Intent(NomeActivity.this, MenuActivity.class);
|
||||||
|
startActivity(intent);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
private void speak(String text) {
|
||||||
|
// QUEUE_FLUSH: cancela o que estava a falar e fala já
|
||||||
|
tts.speak(text, TextToSpeech.QUEUE_FLUSH, null, "utterance_1");
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -7,45 +7,4 @@
|
|||||||
android:layout_height="match_parent"
|
android:layout_height="match_parent"
|
||||||
tools:context=".MenuActivity">
|
tools:context=".MenuActivity">
|
||||||
|
|
||||||
<Button
|
|
||||||
android:id="@+id/button"
|
|
||||||
android:layout_width="0dp"
|
|
||||||
android:layout_height="wrap_content"
|
|
||||||
android:layout_marginStart="32dp"
|
|
||||||
android:layout_marginEnd="32dp"
|
|
||||||
android:layout_marginBottom="32dp"
|
|
||||||
android:text="Button"
|
|
||||||
app:layout_constraintBottom_toBottomOf="parent"
|
|
||||||
app:layout_constraintEnd_toEndOf="parent"
|
|
||||||
app:layout_constraintStart_toStartOf="parent" />
|
|
||||||
|
|
||||||
<TextView
|
|
||||||
android:id="@+id/textView2"
|
|
||||||
android:layout_width="wrap_content"
|
|
||||||
android:layout_height="wrap_content"
|
|
||||||
android:layout_marginTop="64dp"
|
|
||||||
android:text="TextView"
|
|
||||||
app:layout_constraintEnd_toEndOf="parent"
|
|
||||||
app:layout_constraintStart_toStartOf="parent"
|
|
||||||
app:layout_constraintTop_toTopOf="parent" />
|
|
||||||
|
|
||||||
<ImageView
|
|
||||||
android:id="@+id/imageView2"
|
|
||||||
android:layout_width="wrap_content"
|
|
||||||
android:layout_height="wrap_content"
|
|
||||||
android:layout_marginTop="64dp"
|
|
||||||
app:layout_constraintEnd_toEndOf="@+id/textView2"
|
|
||||||
app:layout_constraintStart_toStartOf="@+id/textView2"
|
|
||||||
app:layout_constraintTop_toBottomOf="@+id/textView2"
|
|
||||||
tools:srcCompat="@tools:sample/avatars" />
|
|
||||||
|
|
||||||
<EditText
|
|
||||||
android:id="@+id/editTextText"
|
|
||||||
android:layout_width="wrap_content"
|
|
||||||
android:layout_height="wrap_content"
|
|
||||||
android:ems="10"
|
|
||||||
android:inputType="text"
|
|
||||||
android:text="Name"
|
|
||||||
tools:layout_editor_absoluteX="95dp"
|
|
||||||
tools:layout_editor_absoluteY="352dp" />
|
|
||||||
</androidx.constraintlayout.widget.ConstraintLayout>
|
</androidx.constraintlayout.widget.ConstraintLayout>
|
||||||
53
app/src/main/res/layout/activity_nome.xml
Normal file
53
app/src/main/res/layout/activity_nome.xml
Normal file
@@ -0,0 +1,53 @@
|
|||||||
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
|
<androidx.constraintlayout.widget.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:id="@+id/main"
|
||||||
|
android:layout_width="match_parent"
|
||||||
|
android:layout_height="match_parent"
|
||||||
|
tools:context=".NomeActivity">
|
||||||
|
|
||||||
|
<Button
|
||||||
|
android:id="@+id/seguinteButton"
|
||||||
|
android:layout_width="0dp"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:layout_marginStart="32dp"
|
||||||
|
android:layout_marginEnd="32dp"
|
||||||
|
android:layout_marginBottom="32dp"
|
||||||
|
android:text="@string/seguinteButton"
|
||||||
|
app:layout_constraintBottom_toBottomOf="parent"
|
||||||
|
app:layout_constraintEnd_toEndOf="parent"
|
||||||
|
app:layout_constraintStart_toStartOf="parent" />
|
||||||
|
|
||||||
|
<TextView
|
||||||
|
android:id="@+id/textView2"
|
||||||
|
android:layout_width="wrap_content"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:layout_marginTop="64dp"
|
||||||
|
android:text="TextView"
|
||||||
|
app:layout_constraintEnd_toEndOf="parent"
|
||||||
|
app:layout_constraintStart_toStartOf="parent"
|
||||||
|
app:layout_constraintTop_toTopOf="parent" />
|
||||||
|
|
||||||
|
<ImageView
|
||||||
|
android:id="@+id/imageView2"
|
||||||
|
android:layout_width="wrap_content"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:layout_marginTop="64dp"
|
||||||
|
app:layout_constraintEnd_toEndOf="@+id/textView2"
|
||||||
|
app:layout_constraintStart_toStartOf="@+id/textView2"
|
||||||
|
app:layout_constraintTop_toBottomOf="@+id/textView2"
|
||||||
|
tools:srcCompat="@tools:sample/avatars" />
|
||||||
|
|
||||||
|
<EditText
|
||||||
|
android:id="@+id/nomeEditText"
|
||||||
|
android:layout_width="wrap_content"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:layout_marginTop="32dp"
|
||||||
|
android:ems="10"
|
||||||
|
android:hint="@string/nomeEditText"
|
||||||
|
android:inputType="text"
|
||||||
|
app:layout_constraintEnd_toEndOf="parent"
|
||||||
|
app:layout_constraintStart_toStartOf="parent"
|
||||||
|
app:layout_constraintTop_toBottomOf="@+id/imageView2" />
|
||||||
|
</androidx.constraintlayout.widget.ConstraintLayout>
|
||||||
@@ -1,4 +1,6 @@
|
|||||||
<resources>
|
<resources>
|
||||||
<string name="app_name">Dream Bar</string>
|
<string name="app_name">Dream Bar</string>
|
||||||
<string name="iniciarButton">Clica aqui para começar!</string>
|
<string name="iniciarButton">Clica aqui para começar!</string>
|
||||||
|
<string name="nomeEditText">Insere o teu nome</string>
|
||||||
|
<string name="seguinteButton">Seguinte</string>
|
||||||
</resources>
|
</resources>
|
||||||
Reference in New Issue
Block a user