如何使用数字类型编辑文本传递整数值



解析时显示错误。我只想传递用户获得的整数值。但它在解析时显示错误。因此,此外,还要纠正如何接受来自用户的整数值以及如何以整数格式传递它的问题。编码:

String s=et1.getText().toString();
if(s.matches("")){
    Toast t1= Toast.makeText(MainActivity.this, "Empty", 2000);
    t1.show();
}
else{
    int l = Integer.parseInt(s);
    Toast t2= Toast.makeText(MainActivity.this, s, 2000);
    t2.show();
    tv1.setText(l);
    Intent i1= new Intent(Loadscreen.this,Game01.class);
    i1.putExtra("l", l);
    startActivity(i1);
    Loadscreen.this.finish();
}

编辑文本的 xml 部分

<EditText
        android:id="@+id/editText1"
        android:layout_width="match_parent"
        android:layout_height="45dp"
        android:layout_x="0dp"
        android:layout_y="219dp"
        android:background="#00000000"
        android:ems="10"
        android:gravity="center_vertical"
        android:hint="Specify Level"
        android:inputType="number" />

堆栈跟踪:

02-28 20:06:24.332: E/AndroidRuntime(647): FATAL EXCEPTION: main
02-28 20:06:24.332: E/AndroidRuntime(647): android.content.res.Resources$NotFoundException: String resource ID #0x5 
02-28 20:06:24.332: E/AndroidRuntime(647): at android.content.res.Resources.getText(Resources.java:201) 
02-28 20:06:24.332: E/AndroidRuntime(647): at android.widget.TextView.setText(TextView.java:2857)   
02-28 20:06:24.332: E/AndroidRuntime(647): at com.example.abc.MainActivity$1.onClick(MainActivity.java:37) 
02-28 20:06:24.332: E/AndroidRuntime(647): at android.view.View.performClick(View.java:2485)
02-28 20:06:24.332: E/AndroidRuntime(647): at android.view.View$PerformClick.run(View.java:9080) 
02-28 20:06:24.332: E/AndroidRuntime(647): at android.os.Handler.handleCallback(Handler.java:587) – Shubhankar 2 mins ago       
02-28 20:06:24.332: E/AndroidRuntime(647): at android.os.Handler.dispatchMessage(Handler.java:92) 
02-28 20:06:24.332: E/AndroidRuntime(647): at android.os.Looper.loop(Looper.java:123) 
02-28 20:06:24.332: E/AndroidRuntime(647): at android.app.ActivityThread.main(ActivityThread.java:3683)
02-28 20:06:24.332: E/AndroidRuntime(647): at java.lang.reflect.Method.invokeNative(Native Method) 
02-28 20:06:24.332: E/AndroidRuntime(647): at 

java.lang.reflect.Method.invoke(Method.java:507)

更改为

int l = Integer.parseInt(s);
tv1.setText(String.valueOf(l));

setText需要CharacterSequence.但是,还有一种方法将resourceid作为参数。在你的案例中,设置文本取了 int。如果 android 找不到带有上述 id 的资源,您将得到ResourceNotFoundException

相关内容

  • 没有找到相关文章

最新更新