英文:
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();
}});
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论