2013-02-16 99 views
0

我在整数转换整个字符串时出现问题。 当我编辑edittext“你好”,当我按下按钮,然后在其他编辑文本中,它应该显示我 72 101 108 108 111。 请帮我...转换整数字符串在android中的整数值

我.xml文件

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    tools:context=".MainActivity" > 



    <EditText 
     android:id="@+id/editText1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentTop="true" 

     android:layout_marginRight="22dp" 
     android:layout_marginTop="20dp" 
     android:ems="10" 
     android:hint="@string/Message" > 

     <requestFocus /> 
    </EditText> 

    <Button 
     android:id="@+id/button1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_below="@+id/editText1" 

     android:text="@string/Decimal" /> 

    <EditText 
     android:id="@+id/editText2" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_below="@+id/button1" 
     android:layout_marginTop="30dp" 

     android:ems="10" 

     android:hint="@string/Decimal"/> 

</RelativeLayout> 
+0

请把你的代码.. – Shachi 2013-02-16 13:17:41

回答

1

您必须每一个字符从输入的字符串为int转换。下面 示例代码可以帮助你:

String str = "Hello"; 
String strInt = ""; 
for(int i=0;i<str.length();i++){ 
    char c = str.charAt(i); 
    strInt += (int)c + " "; 
} 
System.out.println(strInt); 
1

那么只投字符来诠释,你会得到的ASCII值。

StringBuilder stringToAppendInts = new StringBuilder(); 
StringBuilder stringToAppendHexs = new StringBuilder(); 

for(char item : myString.toCharArray()){ 
    stringToAppendHexs.append(Integer.toHexString((int)item) +" "); 
    stringToAppendInts.append((int)item+" "); 
} 

edittext.setText(stringToAppendInts.toString()); 
edittext.setText(stringToAppendHexs.toString()); 
+0

@ Gjordis..Hey其工作的人......谢谢.. – 2013-02-16 13:29:51

+0

什么而使用或''StringBuilder'代替StringBuffer''String'?你应该知道'String'是不可变的,如果他将例如从一些大尺寸的file.txt生成的文本,所以你的方法会消耗太多的内存。 – Sajmon 2013-02-16 13:30:27

+0

@ Gjordis ..你可以告诉我如何将字符串转换为十六进制值.. ?? – 2013-02-16 13:30:58