具有来自 xml 和 java 代码的值的按钮事件

发布于 2024-12-12 09:34:57 字数 6608 浏览 0 评论 0原文

已经通过 XML 放置了四个编辑文本框,并且可以通过单击加号按钮添加其他两个连续的文本框。在它下面。

现在,我有按钮名称计算,我想在其中分配与这些编辑文本框中存储的值相关的事件。

就像

<edit text1> <edit text2> 
< edit text3> <edit text4>
<edit text 5> <edit text6> 

在这两个中一样,我将输入一些值,并假设我想要执行此操作

((edit text1)*(edit text2))+((edittext3)*(edit text4))+((edittext5)*(edit text6))

,并且当我单击计算时应该完成此事件。我知道如何执行按钮单击事件,正如您所看到的,我已经做到了。我需要帮助将这些值解析为计算器单击事件。甚至是来自 XML 的编辑文本框中的值以及由加号按钮生成的值。

另外,我想稍微改变一下布局。我尝试使用它,但现在我设法获得编辑文本框下方的按钮,但是当我单击加号 + 按钮时,编辑框出现在下面,而我希望它们出现在顶部?就像我上面的编辑文本的布局一样。

Java 代码:

import android.app.Activity;

import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

    public class PlusbuttonActivity extends Activity 
    implements OnClickListener  {

        private TextView tt;
        private TextView j;
        /** Called when the activity is first created. */
        private LinearLayout root;
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);

            View btnButton = findViewById(R.id.button_next);
            Button mButton = (Button) findViewById(R.id.button);
            mButton.setGravity(Gravity.CENTER);

             tt=(TextView)findViewById(R.id.tt);
             j=(TextView)findViewById(R.id.j);

             root = (LinearLayout) findViewById(R.id.linearLayout);
             mButton.setOnClickListener(this);
        }

            @Override
            public void onClick(View v) {
              switch (v.getId()) {
                 case R.id.button:
                 View view = doStuff();
                  addViewToRoot(view);
                break;
                 case R.id.button_next:
                     View view1 = doCalc();
                     addViewToRoot(view1);
                     break;

              }
            }

          private View doCalc() {
                // but these are text boxes.. i want to implement the edit boxes and output the result in text box
              int parsedInt = Integer.parseInt(tt.getText().toString());
              int parsedIn = Integer.parseInt(j.getText().toString());




                return null;
            }

        private View doStuff() {  
                EditText t = new EditText(PlusbuttonActivity.this);
                t.setGravity(Gravity.LEFT);
                t.setWidth(250);
                EditText a = new EditText(PlusbuttonActivity.this);
                a.setGravity(Gravity.RIGHT);
                a.setWidth(250);
                LinearLayout l = new LinearLayout(PlusbuttonActivity.this);

                t.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
                // t.setBackgroundColor(0xffCECECE);


                a.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));

                l.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));

                l.addView(t);
                l.addView(a); 

                return l; 


        }          

        private void addViewToRoot(View v){
          root.addView(v);
        }

    }

XML 文件:

      <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
                 android:orientation="vertical"
                 android:layout_width="fill_parent"
                 android:layout_height="fill_parent"
                 android:id="@+id/linearLayout">


    <LinearLayout 
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                 >

            <TextView  
                android:layout_width="wrap_content" 
                android:layout_height="wrap_content" 
                android:gravity="left"
                android:layout_weight="1"
                android:text="Units"
                android:id="@+id/tt"
                />
            <TextView android:layout_height="wrap_content" 
            android:gravity="right" 
            android:layout_width="wrap_content" 
            android:layout_weight="1" 
            android:text="Grades"
            android:id="@+id/j"></TextView>
        </LinearLayout>
    <LinearLayout android:id="@+id/LinearLayout01" 
                      android:layout_width="wrap_content" 
                      android:layout_height="wrap_content"
                      android:orientation="horizontal">

              <EditText 
                android:layout_width="250px"     
                android:layout_height="wrap_content"    
                android:id="@+id/edLat">
              </EditText>

              <EditText     
                android:layout_height="wrap_content"    
                android:id="@+id/edLong" android:layout_width="150dp">
               </EditText>
        </LinearLayout>

    <LinearLayout android:id="@+id/LinearLayout01" 
                      android:layout_width="wrap_content" 
                      android:layout_height="wrap_content"
                      android:orientation="horizontal">

              <EditText 
                android:layout_width="250px"     
                android:layout_height="wrap_content"    
                android:id="@+id/et2">
              </EditText>

              <EditText     
                android:layout_height="wrap_content"    
                android:id="@+id/et1" android:layout_width="150dp">
               </EditText>
        </LinearLayout>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" >

        <Button android:layout_height="wrap_content" 
        android:gravity="center" android:id="@+id/button" android:text="+" android:layout_width="wrap_content"></Button>

    <Button
    android:id="@+id/button_next"
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"
    android:layout_alignParentRight="true"
    android:text="CALCULATE"
    >
    </Button>
    </RelativeLayout>



    </LinearLayout>

There are four edit text boxes already placed via XML and other can two in a row can be added by clicking plus button. below it.

Now, I have button name calculate in which I want to assign event which will be related to the value stored in these edit text boxes.

So like

<edit text1> <edit text2> 
< edit text3> <edit text4>
<edit text 5> <edit text6> 

In both of these I will enter some value and lets say I want to do

((edit text1)*(edit text2))+((edittext3)*(edit text4))+((edittext5)*(edit text6))

and this event should be done when I click calculate. I know how to do button click event as you can see I have done that. I need help in parsing these values into the calculator click event. Even the values going in edit text boxes from XML and the one that are getting generated by the plus button.

Also, I want to change the layout a bit. I tried to play around with it, but now I managed to get the buttons below the edit text boxes but when I click plus + button edit boxes come below, whereas I want them to come to the top? Like the layout of the edit text I have above.

Java code:

import android.app.Activity;

import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

    public class PlusbuttonActivity extends Activity 
    implements OnClickListener  {

        private TextView tt;
        private TextView j;
        /** Called when the activity is first created. */
        private LinearLayout root;
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);

            View btnButton = findViewById(R.id.button_next);
            Button mButton = (Button) findViewById(R.id.button);
            mButton.setGravity(Gravity.CENTER);

             tt=(TextView)findViewById(R.id.tt);
             j=(TextView)findViewById(R.id.j);

             root = (LinearLayout) findViewById(R.id.linearLayout);
             mButton.setOnClickListener(this);
        }

            @Override
            public void onClick(View v) {
              switch (v.getId()) {
                 case R.id.button:
                 View view = doStuff();
                  addViewToRoot(view);
                break;
                 case R.id.button_next:
                     View view1 = doCalc();
                     addViewToRoot(view1);
                     break;

              }
            }

          private View doCalc() {
                // but these are text boxes.. i want to implement the edit boxes and output the result in text box
              int parsedInt = Integer.parseInt(tt.getText().toString());
              int parsedIn = Integer.parseInt(j.getText().toString());




                return null;
            }

        private View doStuff() {  
                EditText t = new EditText(PlusbuttonActivity.this);
                t.setGravity(Gravity.LEFT);
                t.setWidth(250);
                EditText a = new EditText(PlusbuttonActivity.this);
                a.setGravity(Gravity.RIGHT);
                a.setWidth(250);
                LinearLayout l = new LinearLayout(PlusbuttonActivity.this);

                t.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
                // t.setBackgroundColor(0xffCECECE);


                a.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));

                l.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));

                l.addView(t);
                l.addView(a); 

                return l; 


        }          

        private void addViewToRoot(View v){
          root.addView(v);
        }

    }

XML file:

      <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
                 android:orientation="vertical"
                 android:layout_width="fill_parent"
                 android:layout_height="fill_parent"
                 android:id="@+id/linearLayout">


    <LinearLayout 
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                 >

            <TextView  
                android:layout_width="wrap_content" 
                android:layout_height="wrap_content" 
                android:gravity="left"
                android:layout_weight="1"
                android:text="Units"
                android:id="@+id/tt"
                />
            <TextView android:layout_height="wrap_content" 
            android:gravity="right" 
            android:layout_width="wrap_content" 
            android:layout_weight="1" 
            android:text="Grades"
            android:id="@+id/j"></TextView>
        </LinearLayout>
    <LinearLayout android:id="@+id/LinearLayout01" 
                      android:layout_width="wrap_content" 
                      android:layout_height="wrap_content"
                      android:orientation="horizontal">

              <EditText 
                android:layout_width="250px"     
                android:layout_height="wrap_content"    
                android:id="@+id/edLat">
              </EditText>

              <EditText     
                android:layout_height="wrap_content"    
                android:id="@+id/edLong" android:layout_width="150dp">
               </EditText>
        </LinearLayout>

    <LinearLayout android:id="@+id/LinearLayout01" 
                      android:layout_width="wrap_content" 
                      android:layout_height="wrap_content"
                      android:orientation="horizontal">

              <EditText 
                android:layout_width="250px"     
                android:layout_height="wrap_content"    
                android:id="@+id/et2">
              </EditText>

              <EditText     
                android:layout_height="wrap_content"    
                android:id="@+id/et1" android:layout_width="150dp">
               </EditText>
        </LinearLayout>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" >

        <Button android:layout_height="wrap_content" 
        android:gravity="center" android:id="@+id/button" android:text="+" android:layout_width="wrap_content"></Button>

    <Button
    android:id="@+id/button_next"
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"
    android:layout_alignParentRight="true"
    android:text="CALCULATE"
    >
    </Button>
    </RelativeLayout>



    </LinearLayout>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

绮烟 2024-12-19 09:34:57

我不完全确定您理解当前的代码。

  1. 在此代码部分中:

    <预><代码> @Override
    公共无效onClick(查看v){
    开关 (v.getId()) {
    案例 R.id. 按钮:
    视图视图= doStuff();
    添加ViewToRoot(视图);
    休息;
    案例 R.id.button_next:
    查看 view1 = doCalc();
    addViewToRoot(view1);
    休息;
    }
    }

为什么要为“button_next”调用 addViewToRoot ?
您不希望结果显示在另一个 Activity 中吗?
您应该使用:

                 case R.id.button_next:
                 startActivity(new Intent(this, YourShowCalcResultsActivity.class));
                 break;
  1. 我认为您应该考虑使用 RelativeLayout 来实现在顶部插入这些布局元素。简单来说,步骤是: 将您在 doStuff() 中创建的 LinearLayout 包装在relativelayout 中。您需要让父布局也成为RelativeLayout。
    您可以使用 addRules()< /a> 并根据需要对齐元素。

  2. 3.

I am not completely sure you understand the current code.

  1. In this code section:

        @Override
        public void onClick(View v) {
          switch (v.getId()) {
             case R.id.button:
             View view = doStuff();
              addViewToRoot(view);
            break;
             case R.id.button_next:
                 View view1 = doCalc();
                 addViewToRoot(view1);
                 break;
          }
        }
    

Why are you calling addViewToRoot for "button_next" ?
Don't you want the results to be shown in another Activity?
You should use:

                 case R.id.button_next:
                 startActivity(new Intent(this, YourShowCalcResultsActivity.class));
                 break;
  1. I think you should consider using RelativeLayout to achieve inserting those layout elements at the top. Briefly the steps are: Wrap the LinearLayout you create in doStuff() in RelativeLayout. You need to have the parent Layout also to be RelativeLayout.
    Than you can use addRules() and align the elements as you like.

  2. 3.

稀香 2024-12-19 09:34:57

让我看看我理解是否正确。您想要从每个 EditText 中解析一个数字,以便可以对它们执行算术...

首先,获取对 EditText 的引用。如果 EditText 是用 XML 制作的,请确保为它分配了一个 id 并使用。

EditText et = (EditText)findViewById(R.id.editID);

如果 EditText 是用代码制作的,您应该已经有对它的引用,因此无需使用 findViewById。

从 EditText 解析 int:

int parsedInt = Integer.parseInt(et.getText().toString());

从 EditText 解析 double:

Double.parseDouble(et.getText().toString());

解析 float:

Float.parseFloat(et.getText().toString());

一旦使用所需的任何类型解析数字,算术就很容易了。

Let me see if I understand correctly. You want to parse a number from each EditText so you can perform arithmetic on them...

First, get a reference to the EditText. If the EditText was made in XML, be sure you assigned it an id and use

EditText et = (EditText)findViewById(R.id.editID);

If the EditText was made in code, you should already have a reference to it, so there's no need to use findViewById.

To parse an int from an EditText:

int parsedInt = Integer.parseInt(et.getText().toString());

To parse a double from an EditText:

Double.parseDouble(et.getText().toString());

To parse a float:

Float.parseFloat(et.getText().toString());

Once you parse the numbers using whatever type you want, arithmetic is easy.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文