上面的模拟器图像只是将给定的两个值相加,并在同一屏幕上的下面textview上显示结果。
这里我需要的是我只想在另一个屏幕的textview上显示结果。如何实现 这个?我必须在源头添加什么来源?
public class CheckingActivity extends Activity {
Button button1;
EditText txtbox1,txtbox2;
TextView tv;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
txtbox1= (EditText) findViewById(R.id.editText1);
button1 = (Button) findViewById(R.id.button1);
tv = (TextView) findViewById(R.id.textView5);
txtbox2= (EditText) findViewById(R.id.editText2);
button1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v)
{
String a,b;
Integer vis;
a = txtbox1.getText().toString();
b = txtbox2.getText().toString();
vis = Integer.parseInt(a)+Integer.parseInt(b);
tv.setText(vis.toString());
}
});
非常感谢!
答案 0 :(得分:4)
1。)用以下内容替换您的主要活动:
public class CheckingActivity extends Activity {
Button button1;
EditText txtbox1,txtbox2;
TextView tv;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
txtbox1= (EditText) findViewById(R.id.editText1);
button1 = (Button) findViewById(R.id.button1);
tv = (TextView) findViewById(R.id.textView5);
txtbox2= (EditText) findViewById(R.id.editText2);
button1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v)
{
String a,b;
Integer vis;
a = txtbox1.getText().toString();
b = txtbox2.getText().toString();
vis = Integer.parseInt(a)+Integer.parseInt(b);
//tv.setText(vis.toString());
Intent i = new Intent(getApplicationContext(),ResultActivity.class);
i.putExtra("sum",vis.toString());
startActivity(i);
}
3。)添加一个xml布局文件resultview
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/tvsum"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Sum : " />
<TextView
android:id="@+id/tvres"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/tvsum"/>
</RelativeLayout>
4.)最后加入你的清单:
<activity android:name=".ResultActivity" />
希望它有所帮助!!
public class ResultActivity extends Activity {
TextView tv;
String result;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.resultview);
Bundle extras = getIntent().getExtras();
if (extras != null) {
result = extras.getString("sum");
}
tv=(TextView) findViewById(R.id.tvres);
tv.setText(result);
}
}
答案 1 :(得分:2)
如果我理解正确,您希望显示带有结果值的新活动。如果是这样,您可以通过创建一个新活动并将此值传递给此活动的整数值符号来完成此操作。
你可以这样做:
在CheckingActivity活动中:
public void onClick(View v)
{
String a,b;
Integer vis;
a = txtbox1.getText().toString();
b = txtbox2.getText().toString();
vis = Integer.parseInt(a)+Integer.parseInt(b);
Intent in = new Intent(this, B.class);
in.putExtra("output_value", vis);
startActivity(in);
}
});
新活动的Xml:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:id="@+id/txt_sum"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Output: "/>
<TextView
android:id="@+id/txt_value"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/txt_sum"/>
</RelativeLayout>
在新活动中:
Bundle extras = getIntent().getExtras();
int value = extras.getInt("output_value");
TextView output = (TextView) findViewById(R.id.txt_value);
output.setText(value);