📜  如何在 Android 中创建 Pokedex UI?

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

如何在 Android 中创建 Pokedex UI?

今天,我们将构建我们童年时代都想要的最重要的小工具,图鉴。下面给出了一个示例图像,以了解我们将在本文中做什么。

我们的图鉴

分步实施

第 1 步:使用空活动创建新的 Android 项目

您应该始终为您的项目使用有意义的名称

第 2 步:使用 MainActivity



让我们也跟随创建启动画面的趋势。现在我们有两个文件activity_main.xmlMainActivity。Java。导航到app > res > layout > activity_main.xml并将以下代码添加到该文件中。下面是activity_main.xml文件的代码

XML


  
    
  


Java
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
  
import androidx.appcompat.app.AppCompatActivity;
  
public class MainActivity extends AppCompatActivity {
      
    Context context = this;
  
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Handler handler = new Handler();
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                change();
            }
        }, 1500);
    }
  
    private void change() {
        startActivity(new Intent(context, dashboard.class));
        finish();
    }
}


XML


  
    
  
        
  
            
  
                
  
                
  
                
  
                
            
  
            
  
                
  
                    
  
                        
  
                        
                    
  
                    
                    
                      
                
  
                
  
                    
  
                        
                    
  
                    
  
                        
                    
  
                    
  
                        
                    
  
                    
  
                        
                    
  
                    
  
                        
                    
                
  
            
        
  
        
  
            
  
            
  
                
  
                    
  
                        
                    
  
                    
  
                        
                    
  
                    
  
                        
                    
  
                    
  
                        
                    
  
                    
  
                        
                    
  
                    
  
                        
                    
  
                    
  
                        
                    
  
                    
  
                        
                    
  
                    
  
                        
                    
  
                    
  
                        
                    
                
            
  
            
  
                
  
                    
                
  
                
  
                    
                
  
                
            
  
            
  
                


转到主活动。 Java文件,参考如下代码。下面是MainActivity的代码。 Java文件。代码中添加了注释以更详细地理解代码。

Java

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
  
import androidx.appcompat.app.AppCompatActivity;
  
public class MainActivity extends AppCompatActivity {
      
    Context context = this;
  
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Handler handler = new Handler();
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                change();
            }
        }, 1500);
    }
  
    private void change() {
        startActivity(new Intent(context, dashboard.class));
        finish();
    }
}

输出:

启动画面

第 3 步:创建另一个空活动

现在使用您的愿望名称创建一个新的空活动。这里我们将选择仪表板。它将创建两个文件activity_dashboard.xmldashboard。Java。

下面是 activity_dashboard.xml 文件的代码:

XML



  
    
  
        
  
            
  
                
  
                
  
                
  
                
            
  
            
  
                
  
                    
  
                        
  
                        
                    
  
                    
                    
                      
                
  
                
  
                    
  
                        
                    
  
                    
  
                        
                    
  
                    
  
                        
                    
  
                    
  
                        
                    
  
                    
  
                        
                    
                
  
            
        
  
        
  
            
  
            
  
                
  
                    
  
                        
                    
  
                    
  
                        
                    
  
                    
  
                        
                    
  
                    
  
                        
                    
  
                    
  
                        
                    
  
                    
  
                        
                    
  
                    
  
                        
                    
  
                    
  
                        
                    
  
                    
  
                        
                    
  
                    
  
                        
                    
                
            
  
            
  
                
  
                    
                
  
                
  
                    
                
  
                
            
  
            
  
                

对于本教程,与仪表板无关。 Java文件。完整项目转至: https : //github.com/navdeepisno1/PokeDex