XML OnClick属性不适用于setContent更改

时间:2014-05-01 11:52:41

标签: android

我正在尝试使用多次设置内容在一个活动中实现多个布局并再次使用它初始化所有窗口小部件,到目前为止我已经成功了,但我一度陷入困境。

  1. setContentView(R.layout.layout1) - > initWidgets1(); - > btn1.setOnClickListner() - >工作

  2. setContentView(R.layout.layout2) - > initWidgets2(); - > btn2.setOnClickListner() - >工作

  3. 但是

    handleClick2(View v)
    {
    Switch (v.getId())
    {-------------
    ---------
    Multiple click handles
    -----
    ---
    }
    
    Similarly.
    handleClick1(View v)
    {
    Switch (v.getId())
    {
    -----------
    ---------
    Multiple click handles
    -----
    ---
    }
    

    此方法对任何布局都不起作用。

    虽然btn.setOnClickListner()有效,但是对于大型列表会占用大量内存,如果有效,后者会很容易。

    代码:

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE); 
        setContentView(R.layout.activity_user);
        initCurrUserWidgets();      
        setAdapter();   
        setCurrUserClickListners();
    }
    
    private void initNewUsrWidgets() {
        // TODO Auto-generated method stub
               cancel_new_user          =   (TextView)findViewById(R.new_user.cancel_TV);
               name_new_user            =   (TextView)findViewById(R.new_user.name_TV);
               username_new_user        =   (TextView)findViewById(R.new_user.username_TV);
               password_new_user        =   (TextView)findViewById(R.new_user.password_TV);
               phone_new_user           =   (TextView)findViewById(R.new_user.phone_TV);
               email_new_user           =   (TextView)findViewById(R.new_user.email_TV);
               role_new_user            =   (TextView)findViewById(R.new_user.role_TV);
               window_group_new_user    =   (TextView)findViewById(R.new_user.window_group_TV);
               room_office_new_user     =   (TextView)findViewById(R.new_user.room_office_TV);
    
    
        //New User's input Dialog
                user_input_Dialog   = new Dialog(UserActivity.this);
                user_input_Dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
                user_input_Dialog.setContentView(R.layout.userdialog);
                user_input_Dialog.setCanceledOnTouchOutside(false);
                doneBtn_userDialog = (Button) user_input_Dialog.findViewById(R.userBox.done_Button);
                cancelBtn_userDialog = (Button) user_input_Dialog.findViewById(R.userBox.cancel_Button);
                inputbox_EditText = (EditText)user_input_Dialog.findViewById(R.userBox.ip_EditText);
                inputbox_EditText.setFocusableInTouchMode(true);        
    }
    
    private void initCurrUserWidgets() {
        // TODO Auto-generated method stub
        //new User Screen Widgets
    
        //user Main Screen
        userListView =  (ListView)findViewById(R.users.userList_ListView);
        adduser     =   (TextView)findViewById(R.users.adduser);        
    }
    
    
    private void setCurrUserClickListners() 
    {
        // TODO Auto-generated method stub
        adduser.setOnClickListener(new OnClickListener() {
    
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                setContentView(R.layout.new_user);
    
                initNewUsrWidgets();
    
                handleNewUsrClicks();
    
            }
        });
    }
    
    protected void handleNewUsrClicks() {
        // TODO Auto-generated method stub
    cancel_new_user.setOnClickListener(new OnClickListener() {
    
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
            setContentView(R.layout.activity_user); 
            initCurrUserWidgets();      
            setAdapter();   
            setCurrUserClickListners();
            }
        });
    
    doneBtn_userDialog.setOnClickListener(new OnClickListener() {
    
        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
    
            String input = "";
            switch(dialogPointer)
            {
            case R.new_user.name_TV:
             input = inputbox_EditText.getText().toString();
             Toast.makeText(getApplicationContext(), ""+input, Toast.LENGTH_SHORT).show();
            break;
    
            case R.new_user.username_TV:
                 input = inputbox_EditText.getText().toString();
                 Toast.makeText(getApplicationContext(), ""+input, Toast.LENGTH_SHORT).show();
    
            break;
    
            case R.new_user.password_TV: 
                 input = inputbox_EditText.getText().toString();
                 Toast.makeText(getApplicationContext(), ""+input, Toast.LENGTH_SHORT).show();
    
            break;
    
            case R.new_user.phone_TV: 
                 input = inputbox_EditText.getText().toString();
                 Toast.makeText(getApplicationContext(), ""+input, Toast.LENGTH_SHORT).show();
    
            break;
    
            case R.new_user.email_TV: 
                 input = inputbox_EditText.getText().toString();
                 Toast.makeText(getApplicationContext(), ""+input, Toast.LENGTH_SHORT).show();
    
            break;
    
            case R.new_user.role_TV: 
                 input = inputbox_EditText.getText().toString();
                 Toast.makeText(getApplicationContext(), ""+input, Toast.LENGTH_SHORT).show();
    
            break;
    
            }
    
        }
    });
    
    cancelBtn_userDialog.setOnClickListener(new OnClickListener() {
    
        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            user_input_Dialog.dismiss();
        }
    });
    
    }
    
    private void setAdapter() {
        // TODO Auto-generated method stub
        testData    =   new TestData();
        ListViewAdapter myAdapter = new ListViewAdapter(getApplicationContext(), testData.useTestData());
        userListView.setAdapter(myAdapter);
    
    }
    public void handleformClick(View v) //For New user Screen
    {  
        int point = v.getId();
        Toast.makeText(getApplicationContext(), ""+point,Toast.LENGTH_SHORT).show();
        switch(point)
        {
        case R.new_user.name_TV: 
        title_userDialog.setText("Name");
        user_input_Dialog.show();
        dialogPointer   =   point;
        break;
    
        case R.new_user.username_TV:
        title_userDialog.setText("Username");
        user_input_Dialog.show();
        dialogPointer   =   point;
        break;
    
        case R.new_user.password_TV: 
        title_userDialog.setText("Password");
        user_input_Dialog.show();
        dialogPointer   =   point;
        break;
    
        case R.new_user.phone_TV: 
        title_userDialog.setText("Phone No.");
        user_input_Dialog.show();
        dialogPointer   =   point;
        break;
    
        case R.new_user.email_TV: 
        title_userDialog.setText("Email");
        user_input_Dialog.show();
        dialogPointer   =   point;
        break;
    
        case R.new_user.role_TV: 
        title_userDialog.setText("Role");
        user_input_Dialog.show();
        dialogPointer   =   point;
        break;
        }
    }
    }
    

1 个答案:

答案 0 :(得分:1)

  

我尝试使用set在一个活动中实现多个布局   多次内容并再次使用它初始化所有小部件

尝试使用多个活动或片段。大多数人不会做你想做的事。