一.营造开发环境
)1)安装JDK并放置环境变量; (省略细节) )。
)2)下载AndroidStudio,双击打开安装;
二.具体开发过程
(第一步的效果图)
(1)寻找参考资料
)2)编写初步代码(如果未实现usb串行功能) )。
文件activity_main.xml :
文件MainActivity.java:
package com.example.usb_to_ttl;
import androidx.appcompat.app .悲伤的黑猫;
import android.content.Context;
import android.widget.Toast;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends悲伤黑猫implements view.onclick listener {
//number 0-9
私有button n _ 0;
私有button n _ 1;
private Button n_2;
私有button n _ 3;
私有button n _ 4;
私有button n _ 5;
private Button n_6;
private Button n_7;
private Button n_8;
私有button n _ 9;
//other symbol
private Button clear_all;
私有button equal;
布尔清除;
private EditText editText_table;
private EditText editText_ele;
私有上下文上下文;
私有字符串USB number;
@Override
保护性视频创建(bundlesavedinstancestate )。
super.oncreate (savedinstancestate;
setcontentview (r.layout.activity _ main;
//the instantiation of number 0-9
n_0=findviewbyid(r.id.button0);
n_1=findviewbyid(r.id.Button1);
n_2=findviewbyid(r.id.Button2);
n_3=findviewbyid(r.id.Button3);
n_4=findviewbyid(r.id.Button4);
n_5=findviewbyid(r.id.Button5);
n_6=findviewbyid(r.id.Button6);
n_7=findviewbyid(r.id.Button7);
n_8=findviewbyid(r.id.Button8);
n_9=findviewbyid(r.id.Button9);
//the instantiation of others
clear _ all=findviewbyid (r.id.button 11 );
equal=findviewbyid (r.id.button 12 );
edittext _ table=findviewbyid (r.id.edittext );
edittext _ ele=findviewbyid (r.id.edittext2);
//unable to edit
edittext _ ele.setfocus able (false );
edittext _ ele.setfocusableintouchmode (false;
edittext _ table.setfocus able (false;
edittext _ table.setfocusableintouchmode (false;
//click
n_0.setonclicklistener(this );
n_1.setonclicklistener(this );
n_2.setonclicklistener(this );
n_3.setonclicklistener(this );
n_4.setonclicklistener(this );
n_5.setonclicklistener(this );
n_6.setonclicklistener(this );
n_7.setonclicklistener(this );
n_8.setonclicklistener(this );
n_9.setonclicklistener(this );
clear _ all.setonclicklistener (this;
equal.setonclicklistener(this;
上下文=this;
}
公共void onclick (view view ) {
//get the input
string input=edittext _ table.gettext ().toString );
//determinewhichkeyisbeingpressed
switch(view.getid () ) ) ) ) ) )。
case R.id.button0:
case R.id.button1:
case R.id.button2:
case R.id.button3:
case R.id.button4:
case R.id.button5:
case R.id.button6:
case R.id.button7:
case R.id.button8:
case R.id.button9:
if(clean ) {
clean=false;
editText_table.setText (' );
}
edittext_table.settext(input ) (button ) view ).getText () ');
黑;
//clean
case R.id.button11:
if(clean ) {
clean=false;
editText_table.setText (' );
}
if (! input.equals (' ' ) {
edittext _ table.settext (input.substring (0,input.length ) (- 1 ) );
黑;
}
黑;
case R.id.button12:
sendToUSB (;
黑;
}
}
private void sendToUSB (
toastt=toast.maketext(context,' Succeed to send ' usbNumber,Toast.LENGTH_LONG );
t.show (;
}
}