gpt4 book ai didi

android - 将不同类型的 View 传递给方法

转载 作者:行者123 更新时间:2023-11-29 15:36:22 25 4
gpt4 key购买 nike

我想创建一个可以清除 EditTextTextView 文本的方法。

我怎样才能使任一类型的 View 都可以传入和清除?

这在 Android 中可行吗?

package com.androidapps.x90.battleexchange;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.TextView;
import java.text.DecimalFormat;

public class MainActivity extends AppCompatActivity {

//declare variables
double arrowBattleRate = 55.1;
double swordBattleRate = 100.9;
double amountEntered;
double convertedValue;

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

final EditText currencyValue = (EditText) findViewById(R.id.txtCurrency);
final RadioButton swordsToArrows = (RadioButton) findViewById(R.id.radSwordsToArrows);
final RadioButton arrowsToSwords = (RadioButton) findViewById(R.id.radArrowToSwords);
final TextView battleResult = (TextView) findViewById(R.id.txtResult);
Button convert = (Button) findViewById(R.id.btnConvert);

convert.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick (View v) {
amountEntered = Double.parseDouble(magicValue.getText().toString());

DecimalFormat tenth = new DecimalFormat("#.#");

if(arrowsToSwords.isChecked()) {
clearValuesFromControls(battleResult);
convertedValue = amountEntered * arrowBattleRate;
battleResult.setText(tenth.format(convertedValue) + " Swords.");
}
if(swordsToArrows.isChecked()){
convertedValue = amountEntered * swordsBattleRate;
battleResult.setText(tenth.format(convertedValue) + " Arrows.");
}

}
});
}
//pass in either EditText or TextView control
public void clearValuesFromControls(control) {
control.setText("");
}
}

最佳答案

EditText 派生自 TextView,所以传入一个 TextView 即可。

关于android - 将不同类型的 View 传递给方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49037847/

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