【问题标题】:Using Java Android, is there a way to access a dynamic object inside a flipperview?使用 Java Android,有没有办法访问 Flipperview 中的动态对象?
【发布时间】:2012-05-11 06:40:10
【问题描述】:

我是 Java/Android 开发的新手。我正在viewflipper中动态构建一个问题/答案,以便每次翻转都有一个带有一些答案的新问题。现在,在我的 XML 文件中,我有一个 Flipperview。下面的代码构建了 X 个 [linearlayout with a [radiogroup and [4 radio elements]]]。我的问题是:如何根据脚蹼中的“当前”可见窗口获取选定的单选按钮?

for (DataQuizQuiz quiz_question : PLT.dataQuiz.getQuiz_data()) {
    LinearLayout ll = new LinearLayout(this);
    ll.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
                        LayoutParams.FILL_PARENT));
    ll.setOrientation(LinearLayout.VERTICAL);
    RadioGroup rg = new RadioGroup(this);

    TextView tv_answer = new TextView(this);
    tv_answer.setText("Question: " + quiz_question.getQuestion());
    ll.addView(tv_answer);

    for (DataQuizAnswers answer : quiz_question.getAnswers()) {
        RadioButton rb = new RadioButton(this);
        rb.setText(answer.getAnswer());
        rg.addView(rb);
    }

    ll.addView(rg);
    vf_quiz_data.addView(ll);
}

我只知道vf_quiz_data.getCurrentView(),但除此之外,我不知道如何引用其中的元素,因为它们没有 id 并且是动态创建的。该代码用于构建布局;我只是不确定现在如何引用其中的数据。感谢您的帮助。

更新: 我想出了一种在可见视图中定位广播组的方法,但我认为必须有更好的方法。我为无线电组分配了计数器 0、1、2 等的 id,因为它循环并使用以下方法捕获无线电组元素:

int selected = (int) ((RadioGroup)
vf_quiz_data.getCurrentView().findViewById(
    vf_quiz_data.getDisplayedChild())).getCheckedRadioButtonId();
RadioButton b = (RadioButton) findViewById(selected);
Log.v("DEBUG",(String) b.getText());

我也不确定根据计数器分配 ID 有多安全。如果有人有其他方法,请告诉我。

【问题讨论】:

    标签: java android dynamic radio-button viewflipper


    【解决方案1】:

    一位朋友告诉我为我的数据构建一个或多个自定义视图,以便我可以使用 vf_quiz_data.getCurrentView() 从我自己的方法中访问它。经过一堆试验和错误,我得到了一个测试示例。我的自定义类名为“ViewQuiz”,我添加了一个名为“getName()”的方法,该方法返回该类添加到视图中的编辑文本的值。我最终能够像这样检索它:(ViewQuiz) vf_quiz_data.getCurrentView()).getName()。我的类扩展了 linearlayout 并在其中输出了一个edittext,我在循环中创建了该类的一个新实例,并将其与 addView 一起添加到 viewflipper 上。以防这对其他人有帮助,例如:

    public class ViewQuiz extends LinearLayout {
    
    public EditText name;
    
    public ViewQuiz(Context context, AttributeSet attrs) {
        super(context, attrs);
    
        name = new EditText(context);
    
        name.setText("This is a test");
        addView(name);
    }
    
    public ViewQuiz(Context context) {
        super(context);
        name = new EditText(context);
        name.setText("This is a test");
        addView(name);
    }
    
    public String getName() {
        return name.getText().toString();
    
    }
    
    }
    

    ViewQuiz test;
    for (loop stuff here) {
            test = new ViewQuiz(this);
            test.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
            // vf_quiz_data = (ViewFlipper) findViewById(R.id.vf_quiz_data);
            vf_quiz_data.addView(test);
    }
    

    // get text from visible view
    (ViewQuiz) vf_quiz_data.getCurrentView()).getName()
    

    【讨论】:

      猜你喜欢
      • 2016-03-31
      • 2018-11-08
      • 1970-01-01
      • 2021-02-04
      • 1970-01-01
      • 2021-10-10
      • 1970-01-01
      • 2020-09-24
      • 1970-01-01
      相关资源
      最近更新 更多