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

用户名

我的布局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代码出现在textViewandroid: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;
}

}

解决此问题的正确方法是什么?谢谢!

cy198706

基本上,您只需要获取三个按钮的文本并将其添加为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] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

使用javascript以纯HTML格式将表单字段值传送到下一页

来自分类Dev

ANDROID:将文字发送到下一页

来自分类Dev

如何在php中将会话值从一页传送到另一页?

来自分类Dev

获取值并根据下拉菜单中的选定内容发送到下一页

来自分类Dev

使用php会话将值从一页发送到另一页

来自分类Dev

无法点击下一页的按钮

来自分类Dev

表格 - 转到下一页的按钮

来自分类Dev

隐藏首页/最后一页上的上一页/下一页按钮

来自分类Dev

将数据发送到下一页时遇到问题

来自分类Dev

C#UWP将列表发送到下一页

来自分类Dev

无法使postbackurl将数据发送到下一页

来自分类Dev

使用锚标记将ID发送到php中的下一页

来自分类Dev

如何使用 C# 将新日期发送到下一页

来自分类Dev

防止下一页按钮转到下一页

来自分类Dev

阅读下一页的teaxt面积值。存储下一页的值

来自分类Dev

甜页jQuery的上一页/下一页按钮

来自分类Dev

用户单击按钮后以角度将数组传递到下一页

来自分类Dev

将数据从按钮传输到下一页的发布表单。姜戈

来自分类Dev

禁用轮播上一页下一页按钮

来自分类Dev

iFrame 内容的下一页和上一页按钮

来自分类Dev

单击按钮后,CasperJS解析下一页

来自分类Dev

保存并移至下一页的HTML按钮

来自分类Dev

下一页按钮导航方式错误

来自分类Dev

刮板点击下一页按钮但什么也没取

来自分类Dev

如何使用jQuery的JavaScript将值发布到下一页?

来自分类Dev

将选择选项值作为url参数传递给下一页

来自分类Dev

单击表格行时将值传递到下一页?

来自分类Dev

如何将链接(<a>)中的某些信息(值)发送到另一页?

来自分类Dev

如何将链接(<a>)中的某些信息(值)发送到另一页?

Related 相关文章

  1. 1

    使用javascript以纯HTML格式将表单字段值传送到下一页

  2. 2

    ANDROID:将文字发送到下一页

  3. 3

    如何在php中将会话值从一页传送到另一页?

  4. 4

    获取值并根据下拉菜单中的选定内容发送到下一页

  5. 5

    使用php会话将值从一页发送到另一页

  6. 6

    无法点击下一页的按钮

  7. 7

    表格 - 转到下一页的按钮

  8. 8

    隐藏首页/最后一页上的上一页/下一页按钮

  9. 9

    将数据发送到下一页时遇到问题

  10. 10

    C#UWP将列表发送到下一页

  11. 11

    无法使postbackurl将数据发送到下一页

  12. 12

    使用锚标记将ID发送到php中的下一页

  13. 13

    如何使用 C# 将新日期发送到下一页

  14. 14

    防止下一页按钮转到下一页

  15. 15

    阅读下一页的teaxt面积值。存储下一页的值

  16. 16

    甜页jQuery的上一页/下一页按钮

  17. 17

    用户单击按钮后以角度将数组传递到下一页

  18. 18

    将数据从按钮传输到下一页的发布表单。姜戈

  19. 19

    禁用轮播上一页下一页按钮

  20. 20

    iFrame 内容的下一页和上一页按钮

  21. 21

    单击按钮后,CasperJS解析下一页

  22. 22

    保存并移至下一页的HTML按钮

  23. 23

    下一页按钮导航方式错误

  24. 24

    刮板点击下一页按钮但什么也没取

  25. 25

    如何使用jQuery的JavaScript将值发布到下一页?

  26. 26

    将选择选项值作为url参数传递给下一页

  27. 27

    单击表格行时将值传递到下一页?

  28. 28

    如何将链接(<a>)中的某些信息(值)发送到另一页?

  29. 29

    如何将链接(<a>)中的某些信息(值)发送到另一页?

热门标签

归档