gpt4 book ai didi

java - Android 按钮不起作用?

转载 作者:数据小太阳 更新时间:2023-10-29 02:49:46 26 4
gpt4 key购买 nike

我想制作一个应用程序,以英尺和英寸为单位输入,以及每英尺的成本,并计算杆的成本。这是一个非常简单的应用程序,我已经完成了大部分工作,但是当我点击计算 按钮 时,代替“结果”,我的答案应该会出现。

现在是 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);
}

最佳答案

像这样更改您的代码:

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" />

关于java - Android 按钮不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31498360/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com