---------------------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">"
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/num1"
/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/risultato"
/>
<Button android:id="@+id/Conv"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Converti in Binario"
android:onClick="conv"
/>
<Button android:id="@+id/Conv1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Converti in Decimale"
android:onClick="toDec"
/>
</LinearLayout>
---------------------Converti.java---------------------
package org.me.binariotodecimale;
import android.app.Activity;
import android.os.Bundle;
import android.widget.*;
import android.view.View;
/**
*
* @author 4ALE
*/
public class Converti extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
}
public void conv(View view){
EditText numero1=(EditText)findViewById(R.id.num1);
int num1=Integer.parseInt(numero1.getText().toString());
String bin=Integer.toBinaryString(num1);
TextView ris=(TextView)findViewById(R.id.risultato);
ris.setText("Binario= "+bin);
}
public void toDec(View view){
EditText numero1=(EditText)findViewById(R.id.num1);
String num1=(numero1.getText().toString());
int dec=Integer.parseInt(num1, 2);
TextView ris=(TextView)findViewById(R.id.risultato);
ris.setText("Decimale= "+dec);
}
}
Molto bene
RispondiElimina