我在以下段落中添加了我的布局:
布局:
<EditText
android:id="@+id/EditTextEmissionDetails"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:gravity="top"
android:hint="Emission Details"
android:scrollbarStyle="insideOverlay"
android:isScrollContainer="true">
</EditText>
在本段中,我想通过检查第一个(&#34; 0&#34;)和第二个(x,b,d)字符来显示输入是文本还是十六进制,二进制,十进制,以及然后在新添加的布局部分显示结果。
爪哇:
import android.os.Bundle;
import android.view.KeyEvent;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.TextView.OnEditorActionListener;
import java.io.IOException;
public class ConsoleActivity extends SerialPortActivity {
String dataformat;
EditText mReception;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.console);
mReception = (EditText) findViewById(R.id.EditTextReception);
EditText Emission;
Emission = (EditText) findViewById(R.id.EditTextEmission);
EditText EmissionDetails;
EmissionDetails = (EditText) findViewById(R.id.EditTextEmissionDetails);
Emission.setOnEditorActionListener(new OnEditorActionListener() {
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
int i;
CharSequence t = v.getText();
char[] text = new char[t.length()];
for (i=0; i<t.length(); i++) {
text[i] = t.charAt(i);
dataformat = "text";
if (text[0] == '0') {
System.out.println("I got a Zero");
if (text[1] == 'b') {
dataformat = "binary";
}
if (text[1] == 'd') {
dataformat = "decimal";
}
if (text[1] == 'x') {
dataformat = "hex";
}
}
EmissionDetails = "Dataformat " + text[0] + text[1] + " is " + dataformat;
}
try {
mOutputStream.write(new String(text).getBytes());
mOutputStream.write('\n');
} catch (IOException e) {
e.printStackTrace();
}
return false;
}
});
}
无论我尝试什么,正如Android-Studio中的红灯所示,迟早会回到开头。
如果输入十六进制数据,我应该尝试调整以仅显示句子(以&#34; 0x&#34;开头):&#34;数据格式0x为十六进制&#34;
答案 0 :(得分:0)
EmissionDetails.setSingleLine(true) by programatically
or
do by xml
<EditText
android:id="@+id/EditTextEmissionDetails"
android:singleline="true"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:gravity="top"
android:hint="Emission Details"
android:scrollbarStyle="insideOverlay"
android:isScrollContainer="true">
</EditText>