将选定的按钮值传送到下一页

时间:2014-05-19 01:04:29

标签: android

我的布局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;
}

}

接近这个的正确方法是什么?谢谢!

1 个答案:

答案 0 :(得分:0)

基本上你只需要获得三个按钮文本并将它们添加为整数。然后使用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));
    }
}
相关问题