如何在 Android 中使自定义按钮在自定义警示对话框中起作用

huangapple go评论65阅读模式
英文:

How to make Custom button work in custom alert Dialog Box using android

问题

Sure, here's the translated code:

public class MainActivity extends AppCompatActivity {
    private TextView regis;
    public Button bt_ok;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        bt_ok = findViewById(R.id.bt_ok);
        getSupportActionBar().hide();
        SharedPreferences prefs = getSharedPreferences("prefs", MODE_PRIVATE);
        boolean firstStart = prefs.getBoolean("firstStart", true);
        if (firstStart) {
            showStartDialog();
        }
    }

    public void showStartDialog() {
        LayoutInflater inflater = LayoutInflater.from(this);
        View view = inflater.inflate(R.layout.activity_dialog, null);
        final AlertDialog.Builder dialog = new AlertDialog.Builder(MainActivity.this, R.style.DialogCustomTheme);
        final View customLayout = getLayoutInflater().inflate(R.layout.activity_dialog, null);
        dialog.setView(customLayout).create();
        dialog.setCancelable(false);
        dialog.show();
        SharedPreferences prefs = getSharedPreferences("prefs", MODE_PRIVATE);
        SharedPreferences.Editor editor = prefs.edit();
        editor.apply();
    }
}

If you have any more questions or need further assistance, feel free to ask.

英文:
public class MainActivity extends AppCompatActivity
{
    private TextView regis;
    public Button bt_ok;
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        bt_ok=findViewById(R.id.bt_ok);
        getSupportActionBar().hide();
        SharedPreferences prefs = getSharedPreferences("prefs", MODE_PRIVATE);
        boolean firstStart = prefs.getBoolean("firstStart", true);
        if (firstStart)
        {
            showStartDialog();
        }
    }
    public void showStartDialog()
    {
        LayoutInflater inflater = LayoutInflater.from(this);
        View view =inflater.inflate(R.layout.activity_dialog,null);
        final AlertDialog.Builder dialog= new AlertDialog.Builder(MainActivity.this,R.style.DialogCustomTheme);
        final View customLayout = getLayoutInflater().inflate(R.layout.activity_dialog,null);
        dialog.setView(customLayout).create();
        dialog.setCancelable(false);
        dialog.show();
        SharedPreferences prefs = getSharedPreferences("prefs", MODE_PRIVATE);
        SharedPreferences.Editor editor = prefs.edit();
        editor.apply();
        }
}

I am unable to make the custom button work , my requirement is simple when my custom dialog box is opened i have to dismiss it by simply clicking on button i dont want to use in-built alert dialog box buttons.Please help me thankyou in advance.

答案1

得分: 0

在对话框上声明您的按钮 XML,然后为该按钮设置点击监听器。

public void showStartDialog()
{

    final AlertDialog.Builder dialog = new AlertDialog.Builder(MainActivity.this, R.style.DialogCustomTheme);
    final View customLayout = getLayoutInflater().inflate(R.layout.activity_dialog, null);

    Button btn = customLayout.findViewById(R.id.your_btn_xml);

    dialog.setView(customLayout).create();
    dialog.setCancelable(false);

    SharedPreferences prefs = getSharedPreferences("prefs", MODE_PRIVATE);
    SharedPreferences.Editor editor = prefs.edit();
    editor.apply();

    AlertDialog alertDialog = dialog.create();
    alertDialog.show();

    btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            //do something when clicked
            alertDialog.dismiss();
        }
    });
}
英文:

Declare your button xml on the dialog then setOnclickListener on that button

public void showStartDialog()
{

    final AlertDialog.Builder dialog= new AlertDialog.Builder(MainActivity.this,R.style.DialogCustomTheme);
    final View customLayout = getLayoutInflater().inflate(R.layout.activity_dialog,null);

     Button btn = customLayout.findViewById(R.id.your_btn_xml);

    dialog.setView(customLayout).create();
    dialog.setCancelable(false);
    
    SharedPreferences prefs = getSharedPreferences("prefs", MODE_PRIVATE);
    SharedPreferences.Editor editor = prefs.edit();
    editor.apply();

    AlertDialog alertDialog = dialog.create();
    alertDialog.show();

    btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            //do something when clicked
            alertDialog.dismiss();
        }});
}

huangapple
  • 本文由 发表于 2020年10月9日 15:32:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/64275736.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定