Android调查问卷

想要在Android中实现调查问卷的功能,在网上找寻了一圈发现没有我想要的效果。随后打算自己写一个,开始是用xml生成样式,但是始终是没有成功,最后还是用代码生成了样式,好歹是拼凑出来了。

效果如图:

Android调查问卷图1

Android调查问卷图2

生成表单部分主要代码:
/**
     * Title: createLayout
     * Description:
     * @param questionList
     */
    private void createLayout(ArrayList<QuestionModel> questionList) {
        for (int i = 0; i < questionList.size(); i++) {
   
            // ####生成item框架####
            LinearLayout itemFeedbackLayout = new LinearLayout(this);
            itemFeedbackLayout.setBackgroundColor(Color.parseColor("#FFFFFF"));
            // 设置item宽度、高度
            // LinearLayout.LayoutParams paramsItem = new
            // LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
            // UiUtils.dip2px(this, 100));
            LinearLayout.LayoutParams paramsItem = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
            // 设置item margin
            paramsItem.setMargins(0, UiUtils.dip2px(this, 10), 0, 0);
            itemFeedbackLayout.setPadding(UiUtils.dip2px(this, 5), 0, UiUtils.dip2px(this, 5), 0);
            itemFeedbackLayout.setLayoutParams(paramsItem);
     
  • 3
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个简单的 Android 调查问卷代码,供参考: 布局文件 activity_main.xml: ```xml <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <TextView android:id="@+id/questionTextView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="问题 1:你喜欢 Android 吗?" /> <RadioGroup android:id="@+id/answerRadioGroup" android:layout_width="wrap_content" android:layout_height="wrap_content"> <RadioButton android:id="@+id/answerYesRadioButton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="喜欢" /> <RadioButton android:id="@+id/answerNoRadioButton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="不喜欢" /> <RadioButton android:id="@+id/answerNeutralRadioButton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="不确定" /> </RadioGroup> <Button android:id="@+id/submitButton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="提交" /> </LinearLayout> ``` Java 代码 MainActivity.java: ```java import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.RadioButton; import android.widget.RadioGroup; import android.widget.TextView; import android.widget.Toast; import androidx.appcompat.app.AppCompatActivity; public class MainActivity extends AppCompatActivity { private TextView questionTextView; private RadioGroup answerRadioGroup; private RadioButton answerYesRadioButton; private RadioButton answerNoRadioButton; private RadioButton answerNeutralRadioButton; private Button submitButton; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); questionTextView = findViewById(R.id.questionTextView); answerRadioGroup = findViewById(R.id.answerRadioGroup); answerYesRadioButton = findViewById(R.id.answerYesRadioButton); answerNoRadioButton = findViewById(R.id.answerNoRadioButton); answerNeutralRadioButton = findViewById(R.id.answerNeutralRadioButton); submitButton = findViewById(R.id.submitButton); submitButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { int checkedRadioButtonId = answerRadioGroup.getCheckedRadioButtonId(); if (checkedRadioButtonId == -1) { Toast.makeText(MainActivity.this, "请选择一个选项!", Toast.LENGTH_SHORT).show(); } else { RadioButton checkedRadioButton = findViewById(checkedRadioButtonId); String answerText = checkedRadioButton.getText().toString(); Toast.makeText(MainActivity.this, "你选择了:" + answerText, Toast.LENGTH_SHORT).show(); } } }); } } ``` 这段代码中,我们使用了一个 RadioGroup 来显示问题的选项,并在点击提交按钮时获取用户选择的选项,并用 Toast 显示在屏幕上。当然,这只是一个简单的示例,实际的调查问卷可能会更加复杂。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值