Como eu Faço para colocar um botão na minha segunda tela . Java android

2 respostas
R

Eu consegui criar um Layout Activity e ir para ele ,mas os botões não funcionam. O que é preciso colocar?.
Meu layout activit

<?xml version="1.0" encoding="utf-8"?>

<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content" >
   
   <LinearLayout
   android:layout_width="match_parent"
   android:layout_height="wrap_content" 
   android:orientation="vertical"
   android:gravity="center" >


   <ImageView 
    android:id="@+id/imageView1" 
    android:layout_height="match_parent" 
    android:layout_width="match_parent"
    android:gravity="center" 
    android:src="@drawable/ic_launcher" />
    
  
  <EditText 
  android:id="@+id/TxtNombre"
  android:layout_height="match_parent"
  android:layout_width="match_parent"
  android:gravity="center" />
  
  <TextView
    android:id="@+id/txt_hello1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:text="aperte aqui para ir para outra tela" />
    
  <Button 
 android:id="@+idd/button_outratela" 
 android:layout_height="wrap_content" 
 android:layout_width="wrap_content" 
 android:text="@string/button_a" 
 android:onClick="Tela" />
    
    <TextView
    android:id="@+id/txt_hello1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:text="aperte aqui para sair" />

</LinearLayout>
 </ScrollView>

Meu SegundaTela.Java
package com.example.myapplication;

import android.app.Activity;

import android.content.Intent;

import android.os.Bundle;

import android.view.View;

import android.widget.Button;

import android.widget.EditText;

import android.widget.ImageView;

import android.widget.TextView;

import com.example.myapplication.R;

public class SegundaTela extends Activity {

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.segundatela);
Button voltar=(Button)findViewById(R.ida.button_ida);
      voltar.setOnClickListener(new View.OnClickListener() {
         public void onClick(View v) 
       { setContentView(R.layout.activity_main);}
       });
       
       Button sairb=(Button)findViewById(R.idb.button_idb);
     sairb.setOnClickListener(new View.OnClickListener() {
         public void onClick(View v) 
           { finish( );}

});
}
}

Meu MainActivity.Java

package com.example.myapplication;

import android.app.Activity;

import android.content.Intent;

import android.os.Bundle;

import android.view.View;

import android.widget.Button;

import android.widget.EditText;

import android.widget.ImageView;

import android.widget.TextView;

import com.example.myapplication.R;

public class MainActivity extends Activity {

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

       Button sair=(Button)findViewById(R.id.button_id);
     sair.setOnClickListener(new View.OnClickListener() {
         public void onClick(View v) 
           { finish( );}
});

}

public void Tela(View view) {

Intent intent=new Intent(getApplicationContext(), SegundaTela.class);

startActivity(intent);
}

}

Meu AndroiManifest.xml
<?xml version="1.0" encoding="utf-8"?>

<uses-sdk
    android:minSdkVersion="14"
    android:targetSdkVersion="25" />

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme">

    <activity android:name="com.example.myapplication.MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

</application>

SegundaTela. xml

<?xml version="1.0" encoding="utf-8"?>

<ScrollView
   android:layout_width="match_parent"
   android:layout_height="wrap_content" >
   
   <LinearLayout
   android:layout_width="match_parent"
   android:layout_height="wrap_content" 
   android:orientation="vertical"
   android:gravity="center" />
    
    <TextView
    android:id="@+id/txt_hello1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:text="aperte aqui para ir para outra tela" />
    
  <Button 
 android:id="@+ida/button_ida" 
 android:layout_height="wrap_content" 
 android:layout_width="wrap_content" 
 android:text="@string/button_b" />
    
    <TextView
    android:id="@+id/txt_hello1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:text="aperte aqui para sair" />

</LinearLayout>
 </ScrollView>
</LinearLayout>

2 Respostas

P

Como está o seu código da sua Activity?

R

Era só colocar no AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>

<uses-sdk
    android:minSdkVersion="14"
    android:targetSdkVersion="25" />

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme">

    <activity android:name="com.example.myapplication.MainActivity" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    
   <activity android:name="com.example.myapplication.SegundaTela" />
</application>
Criado 18 de junho de 2018
Ultima resposta 18 de jun. de 2018
Respostas 2
Participantes 2