Estou com o seguinte problema, referente ao uso do Tab host, no Android Studio: Quando boto o programa para funcionar, o programa automaticamente foca em um dos campos dentro do tabhost(Sem ao menos clicar), e quando aperta o botão “Enter” do teclado do celular, ele vai para o próximo campo. Depois que ele chega no ultimo campo ele não foca mais em nenhum objeto e quando quero clicar em um dos botões ou em algum dos campos de texto, o programa não funciona, no entanto eu consigo clicar nos botãos que ficam no campo inferior da tela do celular, como o botão de trocar de tela. Eu fiz um teste e recriei a mesma tela, sem o tabhost, e acabou atingindo o resultado esperado, indicando que o problema esteja no uso do tabhost.
Meu MainActivity.java:
package com.example.minhaagenda;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
EditText textNome, textoNumero, textoEndereco, textoEmail;
ImageView imagemContato;
ListView listaDeContato;
TextView textoTitulo;
Button botaoAdicionar;
Uri imagemUri = Uri.parse("android.resource://com.example.minhaagenda/drawable/ic_launcher_background.png");
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textNome = findViewById(R.id.campoNome);
textoNumero = findViewById(R.id.campoNumero);
textoEndereco = findViewById(R.id.campoEndereco);
textoEmail = findViewById(R.id.campoEmail);
listaDeContato = findViewById(R.id.listaContato);
imagemContato = findViewById(R.id.imageContato);
textoTitulo = findViewById(R.id.textoTitulo);
botaoAdicionar = findViewById(R.id.botaoAddContato);
textoTitulo.setText("Adcionar Contato");
botaoAdicionar.setText("Adcionar");
textNome.requestFocus();
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(textNome, InputMethodManager.SHOW_IMPLICIT);
}
public void clicaCarregarImagem(View v){
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Select Contact Image"), 1);
}
@SuppressLint("MissingSuperCall")
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
if (resultCode == RESULT_OK) {
if (requestCode == 1) {
imagemUri = data.getData();
imagemContato.setImageURI(data.getData());
}
}
}
}
Activity_main.xml:
<?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:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="visible"
tools:context=".MainActivity"
tools:visibility="visible">
<TabHost
android:id="@android:id/tabhost"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TabWidget
android:id="@android:id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="289dp"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:id="@+id/imageContato"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:contentDescription="@string/todo"
app:srcCompat="@android:mipmap/sym_def_app_icon" />
<LinearLayout
android:layout_width="36dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical">
<Button
android:id="@+id/botaoAddFoto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="clicaCarregarImagem"
android:text="@string/biblioteca" />
<Button
android:id="@+id/botaoTirarFoto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/camera" />
</LinearLayout>
</LinearLayout>
<TextView
android:id="@+id/textoTitulo"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/titulo"
android:textSize="24sp" />
<EditText
android:id="@+id/campoNome"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:autofillHints=""
android:ems="10"
android:hint="@string/nome"
android:inputType="textPersonName"
android:text="" />
<EditText
android:id="@+id/campoNumero"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:autofillHints=""
android:ems="10"
android:hint="@string/numero"
android:inputType="number"
android:text="" />
<EditText
android:id="@+id/campoEndereco"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:autofillHints=""
android:ems="10"
android:hint="@string/endereco"
android:inputType="textPostalAddress"
android:text="" />
<EditText
android:id="@+id/campoEmail"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:autofillHints=""
android:ems="10"
android:hint="@string/email"
android:inputType="textEmailAddress"
android:text="" />
<Button
android:id="@+id/botaoAddContato"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/adcionar_contato" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ListView
android:id="@+id/listaContato"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
</FrameLayout>
</LinearLayout>
</TabHost>
</androidx.constraintlayout.widget.ConstraintLayout>
OBS: O código está “incompleto”, pois estou seguindo as instruções de uma video aula, mas nessa parte o instrutor conseguiu fazer o programa rodar normalmente.