Verifica 13/05/10

Creare un applicazione che converte la velocità da km/h in m/sec e da m/sec a km/h

----------------main.xml----------------

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

<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:stretchColumns="1"

>

<TableRow>

<EditText

android:id="@+id/v1"

android:layout_width="200px"

android:layout_height="wrap_content"

/>

<TextView android:text="km/h" />

</TableRow>

<TableRow>

<EditText

android:id="@+id/v2"

android:layout_width="200px"

android:layout_height="wrap_content"

/>

<TextView android:text="m/sec" />

</TableRow>

<Button

android:id="@+id/conv1"

android:layout_width="200px"

android:layout_height="wrap_content"

android:onClick="conv1"

android:text="Converti da km/h a m/sec"

/>

<Button

android:id="@+id/conv2"

android:layout_width="200px"

android:layout_height="wrap_content"

android:onClick="conv2"

android:text="Converti da m/sec a km/h"

/>

</TableLayout>

----------------convertiVelocita.java----------------

package org.me.convertiVelocita;

import android.app.Activity;

import android.os.Bundle;

import android.content.Context;

import android.widget.*;

import android.view.View;

import android.content.Intent;

public class convertiVelocita extends Activity

{

/** Called when the activity is first created. */

final double a=0.2777;

final double b=3.6;

@Override

public void onCreate(Bundle savedInstanceState)

{

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

}

public void conv1 (View view){

//da km/h a m/sec

EditText vel1=(EditText)findViewById(R.id.v1);

double kmh=Double.parseDouble(vel1.getText().toString());

double velocita=kmh*a;

String risp=Double.toString(velocita);

EditText vel2=(EditText)findViewById(R.id.v2);

vel2.setText(risp);

vel1.setText("");

}

public void conv2 (View view){

//da m/sec a km/h

EditText vel2=(EditText)findViewById(R.id.v2);

double kmh=Double.parseDouble(vel2.getText().toString());

double velocita=kmh*b;

String risp=Double.toString(velocita);

EditText vel1=(EditText)findViewById(R.id.v1);

vel1.setText(risp);

vel2.setText("");

}

}

clip_image002

----------------------------------------------------------------

Creare un applicazione con 3 bottoni che mostra quale bottone è stato premuto

----------------qualeBottone.java----------------

package org.me.qualeBottone;

import android.app.Activity;

import android.os.Bundle;

import android.content.Context;

import android.widget.*;

import android.view.View;

import android.content.Intent;

public class qualeBottone extends Activity

{

/** Called when the activity is first created. */

@Override

public void onCreate(Bundle savedInstanceState)

{

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

}

public void bottone(View view){

int x=view.getId();

TextView visualizza=(TextView)findViewById(R.id.mostra);

if( x==R.id.a1){

visualizza.setText("Premuto il bottone 1");

}else if(x==R.id.a2){

visualizza.setText("Premuto il bottone 2");

}else if(x==R.id.a3){

visualizza.setText("Premuto il bottone 3");

}

}

}

----------------main.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/a1"

android:layout_width="200px"

android:layout_height="wrap_content"

android:onClick="bottone"

android:text="1"

/>

<Button

android:id="@+id/a2"

android:layout_width="200px"

android:layout_height="wrap_content"

android:onClick="bottone"

android:text="2"

/>

<Button

android:id="@+id/a3"

android:layout_width="200px"

android:layout_height="wrap_content"

android:onClick="bottone"

android:text="3"

/>

<TextView

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:id="@+id/mostra"

/>

</LinearLayout>

 

clip_image002[4]

Commenti

Posta un commento