Android按钮无法正常工作?

时间:2015-07-19 05:43:05

标签: java android xml

我想创建一个以英尺和英尺为单位输入的应用程序,以及每英尺的成本,并计算杆的成本。这是一个非常简单的应用程序,我完成了大部分工作,但当我点击计算Button时,代替"结果",我的回答应该来了。

现在这里是XML代码:     

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:text="@string/ft"
    android:id="@+id/feet"
    android:layout_marginTop="41dp"
    android:layout_alignParentTop="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true" />

<EditText
    android:layout_width="100dp"
    android:layout_height="wrap_content"
    android:id="@+id/feet1"
    android:layout_alignBottom="@+id/feet"
    android:layout_toRightOf="@+id/feet"
    android:inputType="text" />

<EditText
    android:layout_width="100dp"
    android:layout_height="wrap_content"
    android:id="@+id/inches"
    android:layout_alignBottom="@+id/feet"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true"
    android:inputType="text" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:text="@string/in"
    android:id="@+id/Inches"
    android:layout_alignBottom="@+id/feet"
    android:layout_toLeftOf="@+id/inches"
    android:layout_toStartOf="@+id/inches" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:text="@string/cost"
    android:id="@+id/cost"
    android:layout_marginTop="60dp"
    android:layout_below="@+id/feet"
    android:layout_toRightOf="@+id/feet"
    android:layout_toEndOf="@+id/feet" />

<EditText
    android:layout_width="100dp"
    android:layout_height="wrap_content"
    android:id="@+id/calcost"
    android:layout_alignBottom="@+id/cost"
    android:layout_toRightOf="@+id/cost"
    android:layout_toEndOf="@+id/cost"
    android:inputType="text" />

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/cal"
    android:id="@+id/calculate"
    android:layout_below="@+id/cost"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="73dp" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:text="@string/ans"
    android:id="@+id/result"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"
    android:layout_marginBottom="123dp" />

以下是Java代码:

package com.example.baqir.saecost;

import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;


public class MainActivity extends ActionBarActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}
public void add(View v)
{
    RelativeLayout rel= (RelativeLayout) findViewById(R.id.rel);
    final TextView result= (TextView)findViewById(R.id.result);
    EditText et1= (EditText)findViewById(R.id.feet1);
    EditText et2= (EditText) findViewById(R.id.inches);
    EditText ct= (EditText)findViewById(R.id.calcost);
    final double a=Double.parseDouble(String.valueOf(et1.getText()));
    final double b=Double.parseDouble(String.valueOf(et2.getText()));
    final double c=Double.parseDouble(String.valueOf(ct.getText()));
    final Button button = (Button) findViewById(R.id.calculate);
    button.setOnClickListener(new View.OnClickListener() {
        public void onClick(View g) {
                double i;
                i=(a+(b/12))*c;
            result.setText(i+" Rs");
            }
    });
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}

}

5 个答案:

答案 0 :(得分:3)

更改您的代码:

public void add(View v)
{
    RelativeLayout rel= (RelativeLayout) findViewById(R.id.rel);
    final TextView result= (TextView)findViewById(R.id.result);
    EditText et1= (EditText)findViewById(R.id.feet1);
    EditText et2= (EditText) findViewById(R.id.inches);
    EditText ct= (EditText)findViewById(R.id.calcost);
    final double a=Double.parseDouble(String.valueOf(et1.getText()));
    final double b=Double.parseDouble(String.valueOf(et2.getText()));
    final double c=Double.parseDouble(String.valueOf(ct.getText()));
    final Button button = (Button) findViewById(R.id.calculate);
    // button.setOnClickListener(new View.OnClickListener() {
    //    public void onClick(View g) {
                double i;
                i=(a+(b/12))*c;
            result.setText(i+" Rs");
   //         }
  //  });
}

并在 XML 中更改添加onClick,如下:

<Button
    android:onClick="add"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/cal"
    android:id="@+id/calculate"
    android:layout_below="@+id/cost"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="73dp" />

答案 1 :(得分:2)

我知道你的问题只是在 XML 布局中添加此行,其中Button

android:onClick="add"

答案 2 :(得分:2)

|Employee 1|  |Employee 1|
|----------|  |----------|
| Job 1    |  | Job 2    |

|Employee 2|  |Employee 2|  |Employee 2|
|----------|  |----------|  |----------|
| Job 3    |  | Job 4    |  | Job      |

删除方法并在oncreate方法中编写代码。

答案 3 :(得分:1)

package com.example.baqir.saecost;

import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;


public class MainActivity extends ActionBarActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    RelativeLayout rel= (RelativeLayout) findViewById(R.id.rel);
    final TextView result= (TextView)findViewById(R.id.result);
    EditText et1= (EditText)findViewById(R.id.feet1);
    EditText et2= (EditText) findViewById(R.id.inches);
    EditText ct= (EditText)findViewById(R.id.calcost);



    final Button button = (Button) findViewById(R.id.calculate);


    button.setOnClickListener(new View.OnClickListener() {
        public void onClick(View g) {
          double i;

          final double a=Double.parseDouble(String.valueOf(et1.getText()));
          final double b=Double.parseDouble(String.valueOf(et2.getText()));
          final double c=Double.parseDouble(String.valueOf(ct.getText()));

          i=(a+(b/12))*c;
          result.setText(i+" Rs");
         }
    });

}



@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}
}

只要XML文件没问题(ID是您实际使用的ID,没有混淆或任何东西),这应该可以解决您的问题。

在按下按钮之前你正在从editTexts中获取值...所以实际上,在你写了任何东西后,抓取值的代码已经被执行了。

答案 4 :(得分:0)

在oncreate方法中初始化你的所有视图它应该工作