gpt4 book ai didi

java - 循环通过复选框并在 Android 中获取文本

转载 作者:行者123 更新时间:2023-11-30 10:38:03 25 4
gpt4 key购买 nike

我正在尝试制作简单的复选框,当一个复选框被选中时,我想显示一个带有复选框文本的 toast 。

这是我的代码,我试过但没有用

 nextscreen.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
RelativeLayout layout = (RelativeLayout)findViewById(R.id.DSP);
for (int i = 0; i < layout.getChildCount(); i++) {
v = layout.getChildAt(i);
if (v instanceof CheckBox) {
if (((CheckBox) v).isChecked()){
String text = String.valueOf(((CheckBox) v).getText());

Log.d("@@@@@@@@@" , "somethinng" +text);

}

}

}
}
});

这是我的布局。

<RelativeLayout
android:layout_below="@+id/customer_email_addnew_edittext"
android:layout_width="match_parent"
android:id="@+id/DSP"
android:layout_height="match_parent">

<android.support.v7.widget.AppCompatCheckBox
android:id="@+id/toi"
android:layout_width="match_parent"
android:layout_margin="5dp"
android:layout_height="wrap_content"
android:text="Times of India"
android:background="@drawable/customborder"/>

<android.support.v7.widget.AppCompatCheckBox
android:id="@+id/danikjagran"
android:layout_width="match_parent"
android:layout_below="@+id/toi"
android:layout_margin="5dp"
android:layout_height="wrap_content"
android:text="Times of India"
android:background="@drawable/customborder"/>

<android.support.v7.widget.AppCompatCheckBox
android:id="@+id/htimes"
android:layout_width="match_parent"
android:layout_below="@+id/danikjagran"
android:layout_margin="5dp"
android:layout_height="wrap_content"
android:text="Times of India"
android:background="@drawable/customborder"/>

<Button
android:id="@+id/conntinue"
android:text="continue"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"/>
</RelativeLayout>

有什么方法可以遍历所有选中的复选框并获取文本。

最佳答案

您提供的代码不显示 Toast,而是写入日志。

试试这个:

if (((CheckBox) v).isChecked()) {
String text = String.valueOf(((CheckBox) v).getText());
Toast.makeText(this, text, Toast.LENGTH_SHORT).show();
}

更新

这样您就不必每次都迭代 subview :

public class YourActivity extends Activity {

private android.support.v7.widget.AppCompatCheckBox mToi;
private android.support.v7.widget.AppCompatCheckBox mDanikjagran;
private android.support.v7.widget.AppCompatCheckBox mHtimes;

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

mToi = (android.support.v7.widget.AppCompatCheckBox) findViewById(R.id.toi);
mDanikjagran = (android.support.v7.widget.AppCompatCheckBox) findViewById(R.id.danikjagran);
mHtimes = (android.support.v7.widget.AppCompatCheckBox) findViewById(R.id.htimes);

// ...

nextscreen.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (mToi.isChecked()) {
Toast.makeText(this, mToi.getText(), Toast.LENGTH_SHORT).show();
}
if (mDanikjagran.isChecked()) {
Toast.makeText(this, mDanikjagran.getText(), Toast.LENGTH_SHORT).show();
}
if (mHtimes.isChecked()) {
Toast.makeText(this, mHtimes.getText(), Toast.LENGTH_SHORT).show();
}
}
});
}
}

关于java - 循环通过复选框并在 Android 中获取文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39884353/

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