我的布局xml文件中有25个按钮(称为activity_button_page.xml
);下面是5个按钮的代码。
<LinearLayout 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"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".ButtonPage" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="00"
android:id="@+id/ze1" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="25"
android:id="@+id/tf1"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="50"
android:id="@+id/fi1"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="25"
android:id="@+id/tf2"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="00"
android:id="@+id/ze2"/>
</LinearLayout>
</LinearLayout>
我的目标是让用户单击长达3个按键,和我想的值的总和(由给定的android:text
代码出现在textView
(android:id="@+id/resulttextview"
)中的下一个页面(activity_result_page.xml
)。
这是我目前在ButtonPage.java中拥有的
package com.example.buttonfield;
import android.app.Activity;
import android.app.AlertDialog;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
public class ButtonPage extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_button_page);
Button ze1 = ((Button)this.findViewById(R.id.ze1));
ze1.setOnClickListener(this);
}
public void onClickListener(View v){
pressed=((Button)v).getText();
switch (v.getId()) {
case R.id.zero1:
pressed=zero1.getText().toString();
break;
//OR
case R.id.zero1:
pressed=R.id.zero1.getText().toString();
break;
}
new AlertDialog.Builder(this).setTitle("Info").setMessage(pressed).setNeutralButton("Okey", null).show();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.button_page, menu);
return true;
}
}
解决此问题的正确方法是什么?谢谢!
基本上,您只需要获取三个按钮的文本并将其添加为Integer。然后,使用Intent Extra将值传递给您喜欢的任何页面。
// button page
Button submit = (Button)findViewById(R.id.submit);
submit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
int sum = Integer.parseInt(button1.getText()) +
Integer.parseInt(button2.getText()) +
Integer.parseInt(button3.getText());
Intent intent = new Intent(this, ResultActivity.class);
intent.putExtra("sum", sum);
startActivity(intent);
}
});
// result page
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_result_page);
Bundle bundle = getIntent().getExtras();
if(bundle != null) {
int sum = bundle.getInt("sum");
TextView textView = (TextView)findViewById(R.id.your_text_view);
textView.setText(Integer.toString(sum));
}
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句