📜  如何在 Android 中实现轮询?

📅  最后修改于: 2022-05-13 01:55:23.676000             🧑  作者: Mango

如何在 Android 中实现轮询?

很多时候,您可能已经在一些应用程序(如 youtube、LinkedIn 等)上看到过。民意调查已完成,用户可以选择他们想要选择的任何选项。在这里,我们将在 Android Studio 中实现轮询。

我们将在本文中构建什么?

在本文中,我们将向用户提问并给他一些选项,当他/她选择一个选项时,该选项的百分比会增加。下面显示了我们将在本文中构建的示例视频。请注意,我们将使用Java语言构建项目。

分步实施

第 1 步:创建一个新项目

  • 打开一个新项目。
  • 我们将使用Java语言开发 Empty Activity。保持所有其他选项不变。
  • 将应用程序命名为 user_application。
  • 将有两个名为 activity_main.xml 和 MainActivity 的默认文件。Java

如果您不知道如何在 Android Studio 中创建新项目,可以参考如何在 Android Studio 中创建/启动新项目?

Step 2. 制作Drawable资源文件

导航到app > res > drawable > 右键单击 > new > drawable 资源文件 > 将其命名为 progress_track.xml 。以下是progress_track.xml 文件的代码-

XML


  
    
        
            
            
            
        
    
  
    
        
            
                
                
                    
                        
                        
                        
                        
                    
                
            
        
    


XML


  
    
  
    
  
    
    
    
  
    
    
    
  
    
    
    
    
    
    
    
    
    
  
    
    
    
    


Java
package com.example.polling;
  
import androidx.appcompat.app.AppCompatActivity;
  
import android.annotation.SuppressLint;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.widget.SeekBar;
import android.widget.TextView;
  
public class MainActivity extends AppCompatActivity {
  
    // Initialize variable
    SeekBar seekBar1,seekBar2,seekBar3,seekBar4;
    TextView tvOption1,tvOption2,tvOption3,tvOption4;
    TextView tvPercent1,tvPercent2,tvPercent3,tvPercent4;
    double count1=1,count2=1,count3=1,count4=1;
    boolean flag1=true,flag2=true,flag3=true,flag4=true;
  
    @SuppressLint("ClickableViewAccessibility")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
  
        // Assign variable
        seekBar1=findViewById(R.id.seek_bar1);
        seekBar2=findViewById(R.id.seek_bar2);
        seekBar3=findViewById(R.id.seek_bar3);
        seekBar4=findViewById(R.id.seek_bar4);
        tvOption1=findViewById(R.id.tv_option1);
        tvOption2=findViewById(R.id.tv_option2);
        tvOption3=findViewById(R.id.tv_option3);
        tvOption4=findViewById(R.id.tv_option4);
        tvPercent1=findViewById(R.id.tv_percent1);
        tvPercent2=findViewById(R.id.tv_percent2);
        tvPercent3=findViewById(R.id.tv_percent3);
        tvPercent4=findViewById(R.id.tv_percent4);
  
        seekBar2.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                return true;
            }
        });
  
        tvOption2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                // check condition
                if(flag2)
                {
                    // when flag two is true
                    count1=1;
                    count2++;
                    count3=1;
                    count4++;
                    flag1=true;
                    flag2=false;
                    flag3=true;
                    flag4=false;
                    // calculate percentage
                    calculatePecent();
                }
            }
        });
    }
  
    private void calculatePecent() {
        // calculate total
        double total=count1+count2+count3+count4;
        // Calculate percentage for all options
        double percent1=(count1/total)*100;
        double percent2=(count2/total)*100;
        double percent3=(count3/total)*100;
        double percent4=(count4/total)*100;
        // set percent on text view
        tvPercent1.setText(String.format("%.0f%%",percent1));
        // Set progress on seekbar
        seekBar1.setProgress((int)percent1);
        tvPercent2.setText(String.format("%.0f%%",percent2));
        seekBar2.setProgress((int)percent2);
        tvPercent3.setText(String.format("%.0f%%",percent3));
        seekBar3.setProgress((int)percent3);
        tvPercent4.setText(String.format("%.0f%%",percent4));
        seekBar4.setProgress((int)percent4);
  
    }
}


第 3 步:处理 activity_main.xml 文件

导航到app > res > layout > activity_main.xml文件并在其中使用以下代码 -

XML



  
    
  
    
  
    
    
    
  
    
    
    
  
    
    
    
    
    
    
    
    
    
  
    
    
    
    

第 4 步:处理 MainActivity。Java

转到 MainActivity。 Java文件并在其中使用以下代码。代码中添加注释以便详细理解

Java

package com.example.polling;
  
import androidx.appcompat.app.AppCompatActivity;
  
import android.annotation.SuppressLint;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.widget.SeekBar;
import android.widget.TextView;
  
public class MainActivity extends AppCompatActivity {
  
    // Initialize variable
    SeekBar seekBar1,seekBar2,seekBar3,seekBar4;
    TextView tvOption1,tvOption2,tvOption3,tvOption4;
    TextView tvPercent1,tvPercent2,tvPercent3,tvPercent4;
    double count1=1,count2=1,count3=1,count4=1;
    boolean flag1=true,flag2=true,flag3=true,flag4=true;
  
    @SuppressLint("ClickableViewAccessibility")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
  
        // Assign variable
        seekBar1=findViewById(R.id.seek_bar1);
        seekBar2=findViewById(R.id.seek_bar2);
        seekBar3=findViewById(R.id.seek_bar3);
        seekBar4=findViewById(R.id.seek_bar4);
        tvOption1=findViewById(R.id.tv_option1);
        tvOption2=findViewById(R.id.tv_option2);
        tvOption3=findViewById(R.id.tv_option3);
        tvOption4=findViewById(R.id.tv_option4);
        tvPercent1=findViewById(R.id.tv_percent1);
        tvPercent2=findViewById(R.id.tv_percent2);
        tvPercent3=findViewById(R.id.tv_percent3);
        tvPercent4=findViewById(R.id.tv_percent4);
  
        seekBar2.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                return true;
            }
        });
  
        tvOption2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                // check condition
                if(flag2)
                {
                    // when flag two is true
                    count1=1;
                    count2++;
                    count3=1;
                    count4++;
                    flag1=true;
                    flag2=false;
                    flag3=true;
                    flag4=false;
                    // calculate percentage
                    calculatePecent();
                }
            }
        });
    }
  
    private void calculatePecent() {
        // calculate total
        double total=count1+count2+count3+count4;
        // Calculate percentage for all options
        double percent1=(count1/total)*100;
        double percent2=(count2/total)*100;
        double percent3=(count3/total)*100;
        double percent4=(count4/total)*100;
        // set percent on text view
        tvPercent1.setText(String.format("%.0f%%",percent1));
        // Set progress on seekbar
        seekBar1.setProgress((int)percent1);
        tvPercent2.setText(String.format("%.0f%%",percent2));
        seekBar2.setProgress((int)percent2);
        tvPercent3.setText(String.format("%.0f%%",percent3));
        seekBar3.setProgress((int)percent3);
        tvPercent4.setText(String.format("%.0f%%",percent4));
        seekBar4.setProgress((int)percent4);
  
    }
}

这是我们应用程序的最终输出。

输出: