【问题标题】:how to call my dialog function in the onclick function?如何在 onclick 函数中调用我的对话框函数?
【发布时间】:2014-05-15 18:38:42
【问题描述】:

我不知道如何将对话框功能添加到我的 onclick 功能中,我希望在单击按钮后调用对话框。

这是 oncreate 方法

 /** Called when the activity is first created. */
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.listview);
    fillData();
    boxAdapter = new ListAdapter(this, products);

    ListView lvMain = (ListView) findViewById(R.id.lvMain);
    lvMain.setAdapter(boxAdapter);


    Button btn = (Button) findViewById(R.id.insert);

    OnClickListener listener = new OnClickListener() {          
        @Override
        public void onClick(View v) {                               


        }
    };

    /** Setting the event listener for the add button */
    btn.setOnClickListener(listener);       



  }

那么这是我在同一个 java 类中的对话框函数

public Dialog onCreateDialog(Bundle savedInstanceState) {
      AlertDialog.Builder builder = new AlertDialog.Builder(this);
      // Get the layout inflater
      LayoutInflater inflater = this.getLayoutInflater();

      // Inflate and set the layout for the dialog
      // Pass null as the parent view because its going in the dialog layout
      builder.setView(inflater.inflate(R.layout.dialog, null))
      // Add action buttons
             .setPositiveButton(R.string.Insert, new DialogInterface.OnClickListener() {
                 @Override
                 public void onClick(DialogInterface dialog, int id) {

                 }
             })
             .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
                 public void onClick(DialogInterface dialog, int id) {
                    // DialogFragment.this.getDialog().cancel();
                 }
             });      
      return builder.create();
  }

【问题讨论】:

标签: android onclick android-dialog


【解决方案1】:

答案在这里,将 Bundle 设为 final

public void onCreate(final Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.listview);
    fillData();
    boxAdapter = new ListAdapter(this, products);

ListView lvMain = (ListView) findViewById(R.id.lvMain);
lvMain.setAdapter(boxAdapter);


Button btn = (Button) findViewById(R.id.insert);

OnClickListener listener = new OnClickListener() {          
    @Override
    public void onClick(View v) {                               
            Dialog d = onCreateDialog(savedInstanceState);
            d.show();

    }
};

/** Setting the event listener for the add button */
btn.setOnClickListener(listener);       

}

【讨论】:

    【解决方案2】:

    在您的 Dialog 函数中,您不必传递参数 Bundle savedInstanceState,然后在您的按钮的 onClick 函数中调用如下函数:

      OnClickListener listener = new OnClickListener() {          
            @Override
            public void onClick(View v) {                               
    Dialog dialog = onCreateDialog(); // assuming that you will remove the argmunent
    dialog.show();
    
            }
        };
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-03-23
      • 2019-01-11
      • 1970-01-01
      • 1970-01-01
      • 2016-06-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多