gpt4 book ai didi

java - "Real time"转换程序中的答案

转载 作者:行者123 更新时间:2023-12-02 04:20:55 25 4
gpt4 key购买 nike

如何在java中编写代码,以便在用户用键盘输入一些值时显示在屏幕上的实时输出,而无需在简单的转换程序中的 Activity 中使用计算按钮?我已经给出了下面的代码。因为它需要一个输入值以及“FROM”和“TO”来转换该值并通过按“计算”按钮显示在显示屏上

现在我想消除“计算”按钮,并且一旦用户输入数字就想在显示屏上显示答案

我的代码如下。请告诉我需要的任何其他信息

package sujaynambiar.textilecalculation;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.List;

public class LengthConverter extends AppCompatActivity {

private Spinner spinner11, spinner21;
private EditText from;
private TextView to;

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

Button btnSubmit = (Button) findViewById(R.id.btnSubmit1);
from = (EditText) findViewById(R.id.InputEditText1);
to = (TextView) findViewById(R.id.OutputTextView1);
spinner11 = (Spinner) findViewById(R.id.spinner11);
List<String> list1 = new ArrayList<String>();
list1.add("Kilometer");
list1.add("Meter");
list1.add("Centimeter");
list1.add("Millimeter");
list1.add("Feet");
list1.add("Yard");
list1.add("Inch");
list1.add("Mile");

ArrayAdapter<String> dataAdapter1 = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, list1);
dataAdapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner11.setAdapter(dataAdapter1);
spinner21 = (Spinner) findViewById(R.id.spinner21);
List<String> list2 = new ArrayList<String>();
list2.add("Kilometer");
list2.add("Meter");
list2.add("Centimeter");
list2.add("Millimeter");
list2.add("Feet");
list2.add("Yard");
list2.add("Inch");
list2.add("Mile");

ArrayAdapter<String> dataAdapter2 = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, list2);
dataAdapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner21.setAdapter(dataAdapter2);
}

public void onClick(View v)
{

int index1 = spinner11.getSelectedItemPosition();
int index2 = spinner21.getSelectedItemPosition();
double value = 0;
if (from.getText().toString().isEmpty())
Toast.makeText(getApplicationContext(), getResources().getString(R.string.toastmessage1),
Toast.LENGTH_LONG).show();
else {
value = Double.parseDouble(from.getText().toString());
}
//From Kilometer
if (index1 == 0 && index2 == 0 )//N

{
double result = value*1;
to.setText(result+"");
}

最佳答案

您正在寻找文本更改监听器。

from.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {

}

@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
calculateNewResult(s);
}

@Override
public void afterTextChanged(Editable s) {

}
});

您可能还想在微调器上使用项目选择监听器。

mySpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
updateTheCalculation();
}

@Override
public void onNothingSelected(AdapterView<?> parent) {

}
});

关于java - "Real time"转换程序中的答案,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32774037/

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