📜  如何在 Android 中实现 Dialog 内部的视图绑定?

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

如何在 Android 中实现 Dialog 内部的视图绑定?

在 android 中,View binding 是一个功能,可以让您更轻松地编写与视图交互的代码。在模块中启用视图绑定后,它会为该模块中存在的每个 XML 布局文件生成一个绑定类。绑定类的实例包含对在相应布局中具有 ID 的所有视图的直接引用。

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

在本文中,我们将在对话框中实现 viewBinding 的概念。这是我们将要构建的应用程序的示例视频。请注意,我们将用Java语言实现这个项目。

分步实施

步骤 1:创建一个新项目

  • 打开一个新项目。
  • 我们将使用Java语言开发 Empty Activity。保持所有其他选项不变。
  • 您可以在方便时更改项目的名称。
  • 将有两个名为activity_main.xmlMainActivity 的默认文件。Java

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

第 2 步:导航到 Build scripts -> build.gradle(module) 文件并在其中添加以下代码。

buildFeatures
{
  viewBinding true
}

单击立即同步以保存更改。

第 3 步:使用 XML 文件

导航到app > res > layout > activity_main.xml并将以下代码添加到该文件。下面是activity_main.xml文件的代码。

XML



     
      
    


XML



 
      
    
       
          
        
          
        
        
           
        
        
 
              
            


Java
package com.example.bindingdialog;
 
import androidx.appcompat.app.AppCompatActivity;
 
import android.app.Dialog;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.View;
 
import com.example.bindingdialog.databinding.ActivityMainBinding;
import com.example.bindingdialog.databinding.DialogMainBinding;
 
public class MainActivity extends AppCompatActivity {
 
    // Initialize variables
    ActivityMainBinding binding;
    DialogMainBinding dialogMainBinding;
    Dialog dialog;
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
         
          // Inflate activity main
        binding=ActivityMainBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
 
        binding.btShow.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {             
                // Inflate dialog main
                dialogMainBinding=DialogMainBinding.inflate(getLayoutInflater());  
            
                // Initialize dialog
                dialog=new Dialog(MainActivity.this);       
         
                // set background transparent
                dialog.getWindow().setBackgroundDrawable(new ColorDrawable(
                        Color.TRANSPARENT
                ));             
                // set view
                dialog.setContentView(dialogMainBinding.getRoot());
                // set listener on plus button
                dialogMainBinding.btPlus.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        // get count from text view
                        String sCount=dialogMainBinding.tvCount.getText().toString();
                        // convert into int
                        int count=Integer.parseInt(sCount);
                        // Increase count
                        ++count;
                        // set count on textview
                        dialogMainBinding.tvCount.setText(String.valueOf(count));
                    }
                });
 
                // set listener on minus button
                dialogMainBinding.btMinus.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        // get count from text view
                        String sCount=dialogMainBinding.tvCount.getText().toString();
                        // convert into int
                        int count=Integer.parseInt(sCount);
                        // check condition
                        if(count!=0)
                        {
                            // When count is not equal to 0
                            // Decrease count
                            --count;
                            // set count on text view
                            dialogMainBinding.tvCount.setText(String.valueOf(count));
                        }
                    }
                });
                // display dialog
                dialog.show();
            }
        });
    }
}



按照路径app > res > 布局 > 右键单击 > 新建 > 布局资源文件并创建一个名为 dialog_main.xml 的新文件。在dialog_main.xml文件中使用以下代码 -

XML




 
      
    
       
          
        
          
        
        
           
        
        
 
              
            

执行上述代码后,dialog_main.xml 文件的设计如下所示。

第 4 步:使用 MainActivity。Java

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

Java

package com.example.bindingdialog;
 
import androidx.appcompat.app.AppCompatActivity;
 
import android.app.Dialog;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.View;
 
import com.example.bindingdialog.databinding.ActivityMainBinding;
import com.example.bindingdialog.databinding.DialogMainBinding;
 
public class MainActivity extends AppCompatActivity {
 
    // Initialize variables
    ActivityMainBinding binding;
    DialogMainBinding dialogMainBinding;
    Dialog dialog;
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
         
          // Inflate activity main
        binding=ActivityMainBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
 
        binding.btShow.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {             
                // Inflate dialog main
                dialogMainBinding=DialogMainBinding.inflate(getLayoutInflater());  
            
                // Initialize dialog
                dialog=new Dialog(MainActivity.this);       
         
                // set background transparent
                dialog.getWindow().setBackgroundDrawable(new ColorDrawable(
                        Color.TRANSPARENT
                ));             
                // set view
                dialog.setContentView(dialogMainBinding.getRoot());
                // set listener on plus button
                dialogMainBinding.btPlus.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        // get count from text view
                        String sCount=dialogMainBinding.tvCount.getText().toString();
                        // convert into int
                        int count=Integer.parseInt(sCount);
                        // Increase count
                        ++count;
                        // set count on textview
                        dialogMainBinding.tvCount.setText(String.valueOf(count));
                    }
                });
 
                // set listener on minus button
                dialogMainBinding.btMinus.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        // get count from text view
                        String sCount=dialogMainBinding.tvCount.getText().toString();
                        // convert into int
                        int count=Integer.parseInt(sCount);
                        // check condition
                        if(count!=0)
                        {
                            // When count is not equal to 0
                            // Decrease count
                            --count;
                            // set count on text view
                            dialogMainBinding.tvCount.setText(String.valueOf(count));
                        }
                    }
                });
                // display dialog
                dialog.show();
            }
        });
    }
}


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

输出: