gpt4 book ai didi

java - Android中获取函数值

转载 作者:行者123 更新时间:2023-12-02 06:52:31 25 4
gpt4 key购买 nike

问题:我正在尝试获取所选单选按钮的索引并在 onClick 函数中使用它。

示例:

    btnCalc.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (getIndex(k) == 0) {
DO THIS
}
if (getIndex(k) == 1) {
DO THAT
}
});

我的 Android 应用程序中有以下代码:

    int POS; //global variable assigned right under MainActivity
final RadioGroup rgTypeOfTrip = (RadioGroup) findViewById(R.id.rgTripType);
Button btnCalc = (Button) findViewById(R.id.btnCalculate);

btnCalc.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Toast.makeText(MainActivity.this, Integer.toString(getIndex(POS1)), Toast.LENGTH_SHORT).show(); //DOESN'T WORK
});

rgTypeOfTrip.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
// TODO Auto-generated method stub

// Method 1
int pos=rgTypeOfTrip.indexOfChild(findViewById(checkedId));
getIndex(pos);
Toast.makeText(MainActivity.this, String.valueOf(pos), Toast.LENGTH_SHORT).show();
}
});

public int getIndex(int POS1) {
Toast.makeText(MainActivity.this, Integer.toString(POS1), Toast.LENGTH_SHORT).show(); // WORKS
return POS1;
}

我怎样才能实现这一点:

Toast.makeText(MainActivity.this, CALL FUNCTION GETINDEX() to get value, Toast.LENGTH_SHORT).show();

调用函数获取k的值?

最佳答案

假设您的问题在 Toast 中,如下所示:“CALL FUNCTION GETINDEX() to get value”

Toast.makeText(MainActivity.this, Integer.toString(getIndex(int)), Toast.LENGTH_SHORT).show();

您传入返回的整数,因此您可以使用以下内容(根据我对您的代码的假设):

Toast.makeText(MainActivity.this, Integer.toString(int), Toast.LENGTH_S

关于java - Android中获取函数值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17838717/

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