简体   繁体   中英

XML OnClick attribute Does no Work on setContent Change

I'm trying to implement multiple layouts in one activity using set content multiple times and initializing all widgets again with it, so far i'm sucessfull, but i'm stuck at one point.

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

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

But

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

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

this method does not work either for any layouts.

while btn.setOnClickListner() works but it will take huge memory for huge lists, latter would be easy if it works.

code:

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;
    }
}
}

I'm trying to implement multiple layouts in one activity using set content multiple times and initializing all widgets again with it

Try using multiple Activities or Fragments instead. Most people do not do what you are trying to do.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM