0

我有两个单选按钮,分别是一次和每天。当我单击一次时,它将加载一个图像,当我单击另一个按钮时,它将显示另一个静态图像。一切正常。我通过获取单选按钮的 id 并设置背景资源来设计带有图像加载的单选按钮。但现在我对如何使用编辑文本和按钮字段配置图像感到困惑。谁能帮我完成这项任务?

public void onCheckedChanged(RadioGroup group, int checkedId) {

        if (checkedId == R.id.once) {
              int imageId = (Integer) once.getTag();

                imgview.setBackgroundResource(imageId);

        } else if (checkedId == R.id.daily) {
             int imageId = (Integer) daily.getTag();

                imgview.setBackgroundResource(imageId);

        } 
    } 

这是我的代码。现在我的要求是要访问单击单选按钮时加载的图像中的edittext字段和按钮字段。

4

3 回答 3

1

我不确定我是否完全理解了你的问题。您想显示/隐藏文本视图和按钮而不是图像?

如果是这样,只需创建一个包含文本视图和按钮的 LinearLayout,然后设置属性

android:visibility="gone"

在 LinearLayout 标签中。

然后通过代码,您可以设置其可见性:

LinearLayout ll=(LinearLayout)findViewById(R.id.linearlayoutid);
ll.setVisibility(View.VISIBLE / View.GONE)

如果这不能回答您的问题,请向我们提供更多详细信息

于 2013-10-25T12:24:53.070 回答
0

您可以使用以下非常适合我的代码:

// declare your vars 
ImageView imgView;
Bitmap bitmap1,bitmap2;

// in your constructor do the following……
// load your bitmaps here
bitmap1 =  BitmapFactory.decodeResource(getResources(),R.drawable.bitmap1);
bitmap2 =  BitmapFactory.decodeResource(getResources(),R.drawable.bitmap2);

// init your image view
showImageView = new Imageview(getContext());


// AND IN YOUR LISTENER 
public void onCheckedChanged(RadioGroup group, int checkId){
    if(checkedId == R.id.once){
        imgView.setImageBitmap(bitmap1);
    }else if(checkedId == R.id.daily){
        imgView.setImageDrawable(bitmap2);
    }
}
于 2013-10-25T13:00:23.413 回答
0

如果你有身份证试试这个,

public void onCheckedChanged(RadioGroup group, int checkedId) {

    if (checkedId == R.id.once) {
          int imageId = (Integer) once.getTag();

            imgview.setBackgroundResource(imageId);
            EditText et = (EditText) imgview.findViewById(R.id.textFieldId);
            Button btn = (Button) imgview.findViewById(R.id.buttonId);

    } else if (checkedId == R.id.daily) {
         int imageId = (Integer) daily.getTag();

            imgview.setBackgroundResource(imageId);
            EditText et = (EditText) imgview.findViewById(R.id.textFieldId);
            Button btn = (Button) imgview.findViewById(R.id.buttonId);
    } 
} 

否则你可以使用

EditText et = (EditText) imgview.getChildAt(0);
Button btn = (Button) = (Button ) imgview.getChildAt(1);

如果您没有 ID,请注意创建视图的顺序

如何修改线性布局背景

根据您的需要,将其放入 if 或 else 块中的 onCheckedChanged 中:

LinearLayout ll=(LinearLayout)findViewById(R.id.linearlayoutid);
ll.setVisibility(View.VISIBLE/View.GONE);
ll.setBackgroundResource(imageId);
于 2013-10-25T12:39:16.217 回答