www.ways2u/?post=183
文章地址上
android 在线翻译软件
filed in android post by onelong on2010-7-21 21:19 Wednesday
Android在近几年得到了千千万万的开发者和移动厂商的一致好评。Android承诺开源,秉承了Google的一贯作风,开源为移动生产商节约了成本。当然单靠节约成本是不够的,Android系统是一个具有丰富用户体验的手机平台或移动平台,不仅让用户赏心悦目享受到她提供的music、影视、摄影和互联网等丰富的多媒体冲击。如果只有这些,android怎可能流行起来呢?所以android还针对开发者提供了十分丰富的编程接口(api)极其简单地复杂的应用,正因为android对开发者的万般呵护,以致万千开发者热情地投入了android的怀抱,当然我也是其中一个,废话就不多说了!借助“首届Google暑期大学生博客分享大赛——2010 Andriod篇”的机会分享android可爱之美吧!下面以在线翻译软件为例子,带大家体验android开发的无穷乐趣吧!
我的英语不怎么好,平时都是依靠电脑上翻译工具完成作业,可是电脑太臃肿了,于是我有了一个异想天开的想法,自己做一个手机翻译软件!DIY,会不会太难了,也许在别的手机系统上,这个的确好难!但是android不一样!开发android应用简单到让你惊讶…..
开发这个软件的思路:本人比较擅长javascript,对Google api比较了解,所以想借助Google翻译api结合webView来实现!
参考网站:le/intl/zh-CN/apis/ajaxlanguage/documentation/
anslate(text|option, srcLang, destLang, callback)
至于webView的介绍,大家可以参考android docs
哈哈,不废话了,下面做第一件事,规划简洁而不单调的用户界面,代码如下:
l
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="schemas.android/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:gravity=制作android软件流程"right"
    >
<TextView
    android:layout_marginTop="10px"
    android:id="@+id/l_about" 
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/app_name"
    android:textSize="20sp"
    android:gravity="center_horizontal"
    android:typeface="sans"
    />
< TextView
    android:layout_marginTop="10px"
    android:id="@+id/l_select"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    />
<EditText
    android:layout_marginTop="10px"
    android:id="@+id/tinput"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:hint="输入要翻译的词或句子"
    />
<Button
    android:layout_marginTop="10px"
    android:id="@+id/submit"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text=" 开始翻译  "
/>
<TextView
    android:id="@+id/tips"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="翻译结果如下:"
    android:textSize="14sp"
    android:typeface="sans"
    android:visibility="invisible"
    />
<WebView
    android:layout_marginTop="10px"
    android:id="@+id/toutput"
    android:layout_width="fill_parent"
    android:layout_height="180px"
    android:visibility="invisible"
    />
</LinearLayout>
在规划这个界面中只用了几个小组件:TextViewEditTextButtonWebView
哈哈,看到这里,你一定想知道这段代码生成的界面的样子是怎样的吧?好,先保持一点神秘感!实现了布局,下面应该做什么呢?
答案是显而易见的,
public class TranslationOnline extends Activity {
    private TextView tips;
    private EditText editText;
    private WebView webView;
    private String[] chooseStr={"简体中文->英语","英语->简体中文"};
       
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
       
        webView=(WebView)findViewById(R.id.toutput);
        final Button submit=(Button)findViewById(R.id.submit);
        editText=(EditText)findViewById(R.id.tinput);
        tips=(TextView)findViewById(R.id.tips);
        final Spinner l_select=(Spinner)findViewById(R.id.l_select);
 
        ArrayAdapter<String> adapter=new ArrayAdapter<String>(this,
                android.R.layout.simple_spinner_item,chooseStr);//配置一个适配器
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);