package com.com.twe.wew;
import com.waps.AdView;
import com.waps.AppConnect;
import android.app.Activity;
import android.os.Bundle;
import android.view.Display;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
public class JZZH extends Activity {
private Button js_jz;
private Button CK;
private Button jt4;
private EditText two=null;
private EditText eight=null;
private EditText ten=null;
private EditText sixteen=null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//去掉标题
requestWindowFeature(Window.FEATURE_NO_TITLE);
//全屏
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.jzzh);
AppConnect.getInstance(this);
LinearLayout container =(LinearLayout)findViewById(R.id.AdLinearLayout);
new AdView(this,container).DisplayAd();
//获取屏幕宽和高
WindowManager w=getWindowManager();
Display display=w.getDefaultDisplay();
int width=display.getWidth();
js_jz=(Button)findViewById(R.id.js_jz);
CK=(Button)findViewById(R.id.ck);
jt4=(Button)findViewById(R.id.jt4);
two=(EditText)findViewById(R.id.two);
eight=(EditText)findViewById(R.id.eight);
ten=(EditText)findViewById(R.id.ten);
sixteen=(EditText)findViewById(R.id.sixteen);
js_jz.setWidth(width/4);
CK.setWidth(width/4);
jt4.setWidth(width/4);
js_jz.setOnClickListener(new ButtonListener());
CK.setOnClickListener(new ButtonListener());
jt4.setOnClickListener(new ButtonListener());
}
class ButtonListener implements View.OnClickListener{
public void onClick(View v) {
// TODO Auto-generated method stub
String temp=null,num1=null,num2=null,num3=null,num4=null;
switch(v.getId())
{
case R.id.js_jz:
{
if(two.hasFocus())
{
try
{
temp=two.getText().toString();
num3=Integer.valueOf(temp,2).toString();//将二进制转换为十进制;
num2=Integer.toOctalString(Integer.parseInt(num3));
num4=Integer.toHexString(Integer.parseInt(num3));
eight.setText(num2);
ten.setText(num3);
sixteen.setText(num4.toUpperCase());
}
catch(Exception e)
{
TTS("数据错误,请重新输入数据");
}
}
else if(eight.hasFocus())
{
try{
temp=eight.getText().toString();
num3=Integer.valueOf(temp,8).toString();
num1=Integer.toBinaryString(Integer.parseInt(num3));
num4=Integer.toHexString(Integer.parseInt(num3));
two.setText(num1);
ten.setText(num3);
sixteen.setText(num4.toUpperCase());
}
catch(Exception e)
{
TTS("数据错误,请重新输入数据");
}
}
else if(ten.hasFocus())
{
try{
num3=ten.getText().toString();
num1=Integer.toBinaryString(Integer.parseInt(num3));
num2=Integer.toOctalString(Integer.parseInt(num3));
num4=Integer.toHexString(Integer.parseInt(num3));
two.setText(num1);
eight.setText(num2);
sixteen.setText(num4.toUpperCase());
}
catch(Exception e)
{
TTS("数据错误,请重新输入数据");
}
}
else if(sixteen.hasFocus())
{
try{
temp=sixteen.getText().toString();
num3=Integer.valueOf(temp,16).toString();
num1=Integer.toBinaryString(Integer.parseInt(num3));
num2=Integer.toOctalString(Integer.parseInt(num3));
two.setText(num1);
eight.setText(num2);
ten.setText(num3);
sixteen.setText(temp.toUpperCase());
}
catch(Exception e)
{
TTS("数据错误,请重新输入数据");
}
}
}
break;
case R.id.ck:
two.setText("");
eight.setText("");
ten.setText("");
sixteen.setText("");
break;
case R.id.jt4:
if(two.hasFocus())
{
String a=two.getText().toString();
String b=null;
int c=a.length();
if(c!=0)
{
b=a.substring(0,c-1);
two.setText(b);
}
else
{
two.setText("");
}
}
else if(eight.hasFocus())
{
String a=eight.getText().toString();
String b=null;
int c=a.length();
if(c!=0)
{
b=a.substring(0,c-1);
eight.setText(b);
}
else
{
eight.setText("");
}
}
else if(ten.hasFocus())
{
String a=ten.getText().toString();
String b=null;
int c=a.length();
if(c!=0)
{
b=a.substring(0,c-1);
ten.setText(b);
}
else
{
ten.setText("");
}
}
else if(sixteen.hasFocus())
{
String a=sixteen.getText().toString();
String b=null;
int c=a.length();
if(c!=0)
{
b=a.substring(0,c-1);
sixteen.setText(b);
}
else
{
sixteen.setText("");
}
}
}
}
};
public void TTS(String str)
{
View view=getLayoutInflater().inflate(R.layout.taost,null);
TextView textView=(TextView)view.findViewById(R.id.toast);
textView.setText(str);
textView.setBackgroundResource(R.drawable.corner_view);
Toast toast=new Toast(JZZH.this);
toast.setDuration(Toast.LENGTH_LONG);
toast.setGravity(Gravity.CENTER, 0, 0);
toast.setView(view);
toast.show();
}
}
布局文件为jzzh.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:background="@drawable/my_shape3"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:text="二进制"
android:textColor="#FF6347"
android:textSize="25px"
android:layout_width="wrap_content"
android:layout_height="wrap_content"></TextView>
<EditText
android:layout_height="wrap_content"
android:id="@+id/two"
android:background="@drawable/my_shape3"
android:cursorVisible="false"
android:textColor="#FF6347"
android:textSize="25px"
android:layout_width="match_parent"></EditText>
<TextView
android:text="八进制"
android:textColor="#FF6347"
android:textSize="25px"
android:layout_width="wrap_content"
android:layout_height="wrap_content"></TextView>
<EditText
android:layout_height="wrap_content"
android:id="@+id/eight"
android:background="@drawable/my_shape3"
android:cursorVisible="false"
android:textColor="#FF6347"
android:textSize="25px"
android:layout_width="match_parent"></EditText>
<TextView
android:text="十进制"
android:textColor="#FF6347"
android:textSize="25px"
android:layout_width="wrap_content"
android:layout_height="wrap_content"></TextView>
<EditText
android:layout_height="wrap_content"
android:id="@+id/ten"
android:textColor="#FF6347"
android:textSize="25px"
android:background="@drawable/my_shape3"
android:cursorVisible="false"
android:layout_width="match_parent"></EditText>
<TextView
android:text="十六进制"
android:textColor="#FF6347"
android:textSize="25px"
android:layout_width="wrap_content"
android:layout_height="wrap_content"></TextView>
<EditText
android:layout_height="wrap_content"
android:id="@+id/sixteen"
android:background="@drawable/my_shape3"
android:textColor="#FF6347"
android:textSize="25px"
android:cursorVisible="false"
android:layout_width="match_parent"></EditText>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<Button
android:text="←"
android:id="@+id/jt4"
android:textColor="#FF6347"
android:layout_margin="3px"
android:background="@drawable/my_shape3"
android:textSize="25px"
android:layout_width="wrap_content"
android:layout_height="wrap_content"></Button>
<Button
android:text="C"
android:id="@+id/ck"
android:layout_margin="3px"
android:textColor="#FF6347"
android:background="@drawable/my_shape3"
android:textSize="25px"
android:layout_width="wrap_content"
android:layout_height="wrap_content"></Button>
<Button
android:text="计算"
android:textColor="#FF6347"
android:layout_margin="3px"
android:id="@+id/js_jz"
android:background="@drawable/my_shape3"
android:textSize="25px"
android:layout_width="wrap_content"
android:layout_height="wrap_content"></Button>
</LinearLayout>
<LinearLayout
android:id="@+id/AdLinearLayout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"/>
</LinearLayout>