Using onClick attribute in layout xml causes a NoSuchMethodException in Android dialogs

后端 未结 9 1643
说谎
说谎 2020-11-29 07:45

I have created a custom dialog and a layout xml:




        
9条回答
  •  自闭症患者
    2020-11-29 08:07

    Following on from Jett Hsieh's post, I've implemented my dialogs slightly differently using showDialog and dismissDialog, but the fundamentals of getting the android:onClick working have been the same, my example code is below for future reference.

    public class ExampleActivity extends Activity { 
        static final int DIALOG_DISCLAIMER = 0;
    
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
    
            showDialog(DIALOG_DISCLAIMER);
        }
    
        protected Dialog onCreateDialog(int id)
        {
            Dialog dialog;
            switch(id)
            {
                case DIALOG_DISCLAIMER:
                    dialog = new Dialog(this);
                    dialog.setContentView(R.layout.main_disclaimer);
    
                    LinearLayout ll = (LinearLayout) LayoutInflater.from(this).inflate(R.layout.main_disclaimer, null);
                    dialog.setContentView(ll);
    
                    break;
                default:
                    dialog = null;
            }       
            return dialog;
        }
    
        public void onClick(View v)
        {
            switch(v.getId())
            {
                case R.id.maindisclaimer_button_accept:
                    dismissDialog(DIALOG_DISCLAIMER);
                    break;
            }
        }
    }
    

    And the layout file:

    
        
    
    

提交回复
热议问题