Android/Java 初学者 - 帮助了解 Intent 和添加两个数字
我已经为此工作好几天了,但我没有主意。 我到处搜索并找到了 ProAndroid2 这本书。
我正在尝试做一些简单的事情(我认为),它有两个文本框,用户在每个文本框中输入一个数字,然后按下一个按钮,就会显示数字的总和。
我收到的错误是 EditText 无法解析为整数,即使我发现的所有内容都说使用我正在使用的 toString 方法。我认为这可能是因为我不确定如何让应用程序等待解析它,直到单击按钮,这就是我认为我正在做的设置方式。所以本质上它什么也没解析,当然也无法解析。啊。
非常感谢任何帮助。请相信我已经搜索、搜索、阅读、阅读,我想我可能会错过一些重要的概念,并且不知道到底要搜索什么才能获得帮助。我已经完成了一些其他教程,但这真的让我抓狂。
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class AddNumbers extends Activity
{
TextView textview;
Button button1;
EditText number1, number2;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
textview = (TextView)findViewById(R.id.textView);
//gets numbers from user
number1 = new EditText(this);
number2 = new EditText(this);
button1 = (Button)findViewById(R.id.button1);
button1.setOnClickListener(btnListener);
}
private OnClickListener btnListener = new OnClickListener()
{
public void onClick(View v)
{
int int1 = Integer.parseInt(number1.getText().toString());
int int2 = Integer.parseInt(number2.getText().toString());
int sum = int1 + int2;
Intent intent = new Intent(this,AddNumbers.class);
intent.putExtra("sum",sum);
startActivity(intent);
getIntent();
intent.getIntExtra("sum", sum);
textview.setText(String.valueOf(sum));
}
}
};
这是我的 xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<EditText android:layout_width="match_parent" android:text="EditText"
android:layout_height="wrap_content" android:id="@+id/number1"></EditText>
<EditText android:layout_width="match_parent" android:text="EditText"
android:layout_height="wrap_content" android:id="@+id/number2"></EditText>
<Button android:id="@+id/button1" android:text="Button" android:layout_width="wrap_content"
android:layout_height="wrap_content"></Button>
<TextView android:text="TextView" android:id="@+id/textView"
android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView>
</LinearLayout>
和清单
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.self.projectOne"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name="AddNumbers"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
i've been working on this for several days and i'm just out of ideas.
i've searched everywhere and have the book ProAndroid2.
i'm trying to do something simple (i thought) which was have two text boxes where a user enters a number in each, then pushes a button and the sum of the numbers will display.
i found this on stackoverflow about doing pretty much the exact same thing and have been doing pretty good at learning all sorts of stuff, but i'm apparently missing something very very basic. i can get the text view to display a sum if i set the variables, but i can't figure out how to get user input.
the errors i'm getting are that EditText can't be parsed as in integer, even tho everything i find says to use the toString method i'm using. and i think it might be because i'm not sure how to get the app to wait to parse it until the button is clicked which is what i thought i was doing with how i have it set up. so essentially it's parsing nothing and of course that can't be parsed. ugh.
any help much appreciated. please believe i've searched and searched and read and read, i think i might be missing out on some important concept and not know what exactly to search for to get help. i've done some other tutorials but this is just really making me crazy.
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class AddNumbers extends Activity
{
TextView textview;
Button button1;
EditText number1, number2;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
textview = (TextView)findViewById(R.id.textView);
//gets numbers from user
number1 = new EditText(this);
number2 = new EditText(this);
button1 = (Button)findViewById(R.id.button1);
button1.setOnClickListener(btnListener);
}
private OnClickListener btnListener = new OnClickListener()
{
public void onClick(View v)
{
int int1 = Integer.parseInt(number1.getText().toString());
int int2 = Integer.parseInt(number2.getText().toString());
int sum = int1 + int2;
Intent intent = new Intent(this,AddNumbers.class);
intent.putExtra("sum",sum);
startActivity(intent);
getIntent();
intent.getIntExtra("sum", sum);
textview.setText(String.valueOf(sum));
}
}
};
and here's my xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<EditText android:layout_width="match_parent" android:text="EditText"
android:layout_height="wrap_content" android:id="@+id/number1"></EditText>
<EditText android:layout_width="match_parent" android:text="EditText"
android:layout_height="wrap_content" android:id="@+id/number2"></EditText>
<Button android:id="@+id/button1" android:text="Button" android:layout_width="wrap_content"
android:layout_height="wrap_content"></Button>
<TextView android:text="TextView" android:id="@+id/textView"
android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView>
</LinearLayout>
and the manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.self.projectOne"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name="AddNumbers"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您应该从 Activity 的内容中获取 editTexts,如下所示:
然后在 onClick 方法上:您不需要启动 Activity,因为您已经在上面了:试试这个:
}
you should get your editTexts from the content of your activity like this :
and then on your onClick method: you dont need to start your activity because you are already on it: try this :
}
您尚未将 Java
EditText
映射到 XML 中的文本。指定number1 = (EditText) findViewById(R.id.number1);
以从 XML 布局中选取它。You haven't mapped the Java
EditText
to the one in the XML. Assignnumber1 = (EditText) findViewById(R.id.number1);
to pick it up off of the XML layout.