0

我这里有问题。

Activity A--->B--->C A是欢迎页面,B是用户添加页面,C是用户详情输入页面

我在我的活动 B 中设置了 10 行。我想在第一次启动此应用程序时使其最初处于 GONE 模式,并且在我单击“添加”后,每次单击都会显示该行。如果我单击行中的下一个按钮,我将被定向到活动 C。或者如果我在活动 B 中按回,我将被定向到 A。

现在,我的问题是,我想在第一次启动此应用程序时将其设置为 GONE 模式,然后单击“添加”后,每次单击都会显示该行。而且,如果我去 C 并返回 B,或者返回 A 并返回 B,我希望我显示 VISIBLE 或 GONE 的行保持以前的状态。我怎样才能做到这一点?

public class AccountSetup extends Activity {

    TableRow tableRow1, tableRow2, tableRow3, tableRow4, tableRow5, tableRow6, tableRow7, tableRow8, tableRow9, tableRow10;
    View border1, border2, border3, border4, border5, border6, border7, border8, border9, border10;
    Button iconNext1, iconNext2, iconNext3, iconNext4, iconNext5, iconNext6, iconNext7, iconNext8, iconNext9, iconNext10;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.account_main);
        this.initViews();
        //Set onClick on Button Done
/*      Button btnDone  = (Button)findViewById(R.id.btnDone);       
        btnDone.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            if(tableRow1.isSelected()) 
            return;
        }});*/

        //Set onClick on Button Add
        Button btnAdd = (Button)findViewById(R.id.btnAdd);        
        btnAdd.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                if (tableRow1.getVisibility() == (View.GONE)){
                    tableRow1.setVisibility(View.VISIBLE);
                    border1.setVisibility(View.VISIBLE);}
                else if (tableRow2.getVisibility() == (View.GONE)){
                        tableRow2.setVisibility(View.VISIBLE);
                        border2.setVisibility(View.VISIBLE);}
                else if (tableRow3.getVisibility() == (View.GONE)){
                        tableRow3.setVisibility(View.VISIBLE);
                        border3.setVisibility(View.VISIBLE);}
                else if (tableRow4.getVisibility() == (View.GONE)){
                        tableRow4.setVisibility(View.VISIBLE);
                        border4.setVisibility(View.VISIBLE);}
                else if (tableRow5.getVisibility() == (View.GONE)){
                        tableRow5.setVisibility(View.VISIBLE);
                        border5.setVisibility(View.VISIBLE);}
                else if (tableRow6.getVisibility() == (View.GONE)){
                        tableRow6.setVisibility(View.VISIBLE);
                        border6.setVisibility(View.VISIBLE);}
                else if (tableRow7.getVisibility() == (View.GONE)){
                        tableRow7.setVisibility(View.VISIBLE);
                        border7.setVisibility(View.VISIBLE);}
                else if (tableRow8.getVisibility() == (View.GONE)){
                        tableRow8.setVisibility(View.VISIBLE);
                        border8.setVisibility(View.VISIBLE);}
                else if (tableRow9.getVisibility() == (View.GONE)){
                        tableRow9.setVisibility(View.VISIBLE);
                        border9.setVisibility(View.VISIBLE);}
                else if (tableRow10.getVisibility() == (View.GONE)){
                        tableRow10.setVisibility(View.VISIBLE);
                        border10.setVisibility(View.VISIBLE);}
                else {
                        final AlertDialog alertDialog = new AlertDialog.Builder(AccountSetup.this).create();
                        alertDialog.setTitle("Alert Message");
                        alertDialog.setMessage("You have reached the maximum number of users");
                        alertDialog.setIcon(R.drawable.icon_tick);
                        alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int which) {
                            }
                        });
                        alertDialog.show();
                    }   
        }});
    }

    private void initViews(){ 
        //Account 1  
        tableRow1  = (TableRow)findViewById(R.id.tableRow1);
        border1    = (View)findViewById(R.id.border1);
        iconNext1  = (Button)findViewById(R.id.iconNext1);
        tableRow1.setOnLongClickListener(new OnLongClickListener() {
            public boolean onLongClick(View v) {
            AlertDialog.Builder alertDialog = new AlertDialog.Builder(AccountSetup.this);
            alertDialog.setTitle("User Account Deletion");
            alertDialog.setMessage("Are you sure to delete this user account?");
            alertDialog.setIcon(R.drawable.icon_alert);
            alertDialog.setPositiveButton("YES", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog,int which) {
                    tableRow1.setVisibility(View.GONE);
                    border1.setVisibility(View.GONE);}});
            alertDialog.setNegativeButton("CANCEL", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                    dialog.cancel();}});
            alertDialog.show();
            return true;
            }});
        tableRow1.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
                Intent Intent1 = new Intent(AccountSetup.this, BluGuardProto2Activity.class);
                Intent1.putExtra("activity", "activity1");
                startActivity(Intent1);
                finish();
                }
        });          
        iconNext1.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            Intent Intent1 = new Intent(AccountSetup.this, AccountSettingActivity1.class);
            startActivityForResult(Intent1, 100);
            finish();}});

        //Account 2
        tableRow2  = (TableRow)findViewById(R.id.tableRow2);  
        border2    = (View)findViewById(R.id.border2);
        iconNext2  = (Button)findViewById(R.id.iconNext2);
        tableRow2.setOnLongClickListener(new OnLongClickListener() {
        public boolean onLongClick(View v) {
            AlertDialog.Builder alertDialog = new AlertDialog.Builder(AccountSetup.this);
            alertDialog.setTitle("User Account Deletion");
            alertDialog.setMessage("Are you sure to delete this user account?");
            alertDialog.setIcon(R.drawable.icon_alert);
            alertDialog.setPositiveButton("YES", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog,int which) {
                tableRow2.setVisibility(View.GONE);
                border2.setVisibility(View.GONE);}});
            alertDialog.setNegativeButton("CANCEL", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                dialog.cancel();}});
            alertDialog.show();
            return true;}});
        tableRow2.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                    /*tableRow1.setEnabled(true);
                    tableRow1.setSelected(true);
                    tableRow1.requestFocusFromTouch();*/
                    Intent launchIntent = new Intent(AccountSetup.this, BluGuardProto2Activity.class);
                    launchIntent.putExtra("originActivity", this.getClass().getName());
                    startActivityForResult(launchIntent, 250);
                   //Intent Intent2 = new Intent(AccountSetup.this, BluGuardProto2Activity.class);
                   //Intent2.putExtra("activity", "activity2");
                   //startActivityForResult(Intent2, 250);
                    }
            });
        iconNext2.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            Intent Intent2 = new Intent(AccountSetup.this, AccountSettingActivity2.class);
            startActivityForResult(Intent2, 200);
            finish();}});

        //Account 3
        tableRow3  = (TableRow)findViewById(R.id.tableRow3);  
        border3    = (View)findViewById(R.id.border3);
        iconNext3  = (Button)findViewById(R.id.iconNext3); 
        tableRow3.setOnLongClickListener(new OnLongClickListener() {
        public boolean onLongClick(View v) {
            AlertDialog.Builder alertDialog = new AlertDialog.Builder(AccountSetup.this);
            alertDialog.setTitle("User Account Deletion");
            alertDialog.setMessage("Are you sure to delete this user account?");
            alertDialog.setIcon(R.drawable.icon_alert);
            alertDialog.setPositiveButton("YES", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog,int which) {
                tableRow3.setVisibility(View.GONE);
                border3.setVisibility(View.GONE);}});
            alertDialog.setNegativeButton("CANCEL", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                dialog.cancel();}});
            alertDialog.show();
            return true;}});   
        iconNext3.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
            Intent Intent3 = new Intent(AccountSetup.this, AccountSettingActivity3.class);
            startActivityForResult(Intent3, 300);
            finish();}});

        //Account 4
        tableRow4  = (TableRow)findViewById(R.id.tableRow4);  
        border4    = (View)findViewById(R.id.border4);
        iconNext4  = (Button)findViewById(R.id.iconNext4); 
        tableRow4.setOnLongClickListener(new OnLongClickListener() {
        public boolean onLongClick(View v) {
            AlertDialog.Builder alertDialog = new AlertDialog.Builder(AccountSetup.this);
            alertDialog.setTitle("User Account Deletion");
            alertDialog.setMessage("Are you sure to delete this user account?");
            alertDialog.setIcon(R.drawable.icon_alert);
            alertDialog.setPositiveButton("YES", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog,int which) {
                tableRow4.setVisibility(View.GONE);
                border4.setVisibility(View.GONE);}});
            alertDialog.setNegativeButton("CANCEL", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                dialog.cancel();}});
            alertDialog.show();
            return true;}});              
        iconNext4.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            Intent Intent4 = new Intent(AccountSetup.this, AccountSettingActivity4.class);
            startActivityForResult(Intent4, 400);
            finish();}});

        //Account 5
        tableRow5  = (TableRow)findViewById(R.id.tableRow5);  
        border5    = (View)findViewById(R.id.border5);
        iconNext5  = (Button)findViewById(R.id.iconNext5); 
        tableRow5.setOnLongClickListener(new OnLongClickListener() {
            public boolean onLongClick(View v) {
            AlertDialog.Builder alertDialog = new AlertDialog.Builder(AccountSetup.this);
            alertDialog.setTitle("User Account Deletion");
            alertDialog.setMessage("Are you sure to delete this user account?");
            alertDialog.setIcon(R.drawable.icon_alert);
            alertDialog.setPositiveButton("YES", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog,int which) {
                tableRow5.setVisibility(View.GONE);
                border5.setVisibility(View.GONE);}});
            alertDialog.setNegativeButton("CANCEL", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                dialog.cancel();}});
            alertDialog.show();
            return true;}});
        iconNext5.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            Intent Intent5 = new Intent(AccountSetup.this, AccountSettingActivity5.class);
            startActivityForResult(Intent5, 500);
            finish();}});

        //Account 6
        tableRow6  = (TableRow)findViewById(R.id.tableRow6);  
        border6    = (View)findViewById(R.id.border6); 
        iconNext6  = (Button)findViewById(R.id.iconNext6); 
        tableRow6.setOnLongClickListener(new OnLongClickListener() {
        public boolean onLongClick(View v) {
            AlertDialog.Builder alertDialog = new AlertDialog.Builder(AccountSetup.this);
            alertDialog.setTitle("User Account Deletion");
            alertDialog.setMessage("Are you sure to delete this user account?");
            alertDialog.setIcon(R.drawable.icon_alert);
            alertDialog.setPositiveButton("YES", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog,int which) {
                tableRow6.setVisibility(View.GONE);
                border6.setVisibility(View.GONE);}});
            alertDialog.setNegativeButton("CANCEL", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                dialog.cancel();}});
            alertDialog.show();
            return true;}});    
        iconNext6.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            Intent Intent6 = new Intent(AccountSetup.this, AccountSettingActivity6.class);
            startActivityForResult(Intent6, 600);
            finish();}});

        //Account 7
        tableRow7  = (TableRow)findViewById(R.id.tableRow7);  
        border7    = (View)findViewById(R.id.border7); 
        iconNext7  = (Button)findViewById(R.id.iconNext7);
        tableRow7.setOnLongClickListener(new OnLongClickListener() {
        public boolean onLongClick(View v) {
            AlertDialog.Builder alertDialog = new AlertDialog.Builder(AccountSetup.this);
            alertDialog.setTitle("User Account Deletion");
            alertDialog.setMessage("Are you sure to delete this user account?");
            alertDialog.setIcon(R.drawable.icon_alert);
            alertDialog.setPositiveButton("YES", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog,int which) {
                tableRow7.setVisibility(View.GONE);
                border7.setVisibility(View.GONE);}});
            alertDialog.setNegativeButton("CANCEL", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                dialog.cancel();}});
            alertDialog.show();
            return true;}});    
        iconNext7.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            Intent Intent7 = new Intent(AccountSetup.this, AccountSettingActivity7.class);
            startActivityForResult(Intent7, 700);
            finish();}});

        //Account 8
        tableRow8  = (TableRow)findViewById(R.id.tableRow8);  
        border8    = (View)findViewById(R.id.border8);  
        iconNext8  = (Button)findViewById(R.id.iconNext8);
        tableRow8.setOnLongClickListener(new OnLongClickListener() {
        public boolean onLongClick(View v) {
            AlertDialog.Builder alertDialog = new AlertDialog.Builder(AccountSetup.this);
            alertDialog.setTitle("User Account Deletion");
            alertDialog.setMessage("Are you sure to delete this user account?");
            alertDialog.setIcon(R.drawable.icon_alert);
            alertDialog.setPositiveButton("YES", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog,int which) {
                tableRow8.setVisibility(View.GONE);
                border8.setVisibility(View.GONE);}});
            alertDialog.setNegativeButton("CANCEL", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                dialog.cancel();}});
            alertDialog.show();
            return true;}});    
        iconNext8.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            Intent Intent8 = new Intent(AccountSetup.this, AccountSettingActivity8.class);
            startActivityForResult(Intent8, 800);
            finish();}});

        //Account 9
        tableRow9  = (TableRow)findViewById(R.id.tableRow9);  
        border9    = (View)findViewById(R.id.border9);  
        iconNext9  = (Button)findViewById(R.id.iconNext9);
        tableRow9.setOnLongClickListener(new OnLongClickListener() {
        public boolean onLongClick(View v) {
            AlertDialog.Builder alertDialog = new AlertDialog.Builder(AccountSetup.this);
            alertDialog.setTitle("User Account Deletion");
            alertDialog.setMessage("Are you sure to delete this user account?");
            alertDialog.setIcon(R.drawable.icon_alert);
            alertDialog.setPositiveButton("YES", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog,int which) {
                tableRow9.setVisibility(View.GONE);
                border9.setVisibility(View.GONE);}});
            alertDialog.setNegativeButton("CANCEL", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                dialog.cancel();}});
            alertDialog.show();
            return true;}});     
        iconNext9.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            Intent Intent9 = new Intent(AccountSetup.this, AccountSettingActivity9.class);
            startActivityForResult(Intent9, 900);
            finish();}});

        //Account 10
        tableRow10  = (TableRow)findViewById(R.id.tableRow10);  
        border10    = (View)findViewById(R.id.border10);
        iconNext10  = (Button)findViewById(R.id.iconNext10);
        tableRow10.setOnLongClickListener(new OnLongClickListener() {
        public boolean onLongClick(View v) {
            AlertDialog.Builder alertDialog = new AlertDialog.Builder(AccountSetup.this);
            alertDialog.setTitle("User Account Deletion");
            alertDialog.setMessage("Are you sure to delete this user account?");
            alertDialog.setIcon(R.drawable.icon_alert);
            alertDialog.setPositiveButton("YES", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog,int which) {
                tableRow10.setVisibility(View.GONE);
                border10.setVisibility(View.GONE);}});
            alertDialog.setNegativeButton("CANCEL", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                dialog.cancel();}});
            alertDialog.show();
            return true;}});      
        iconNext10.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            Intent Intent10 = new Intent(AccountSetup.this, AccountSettingActivity10.class);
            startActivityForResult(Intent10, 1000);
            finish();}});
    }

    @Override
    public void onBackPressed() {
        Intent i = new Intent(AccountSetup.this, WelcomeActivity.class);
        finish();
        startActivity(i);
        super.onBackPressed();
    }

    protected void onPause() {
        // When user leaves this tab, saves the values
        super.onPause();
    }

    @Override
    protected void onActivityResult(int requestCode,int resultCode, Intent data) {
    SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this);
       if (requestCode == 100) {
           if (resultCode == RESULT_OK) {    
               data.getStringExtra("accountName1");
               TextView user1 = (TextView)findViewById(R.id.user1);  
               user1.setText(sp.getString("accountName1", ""));}} 
       else if (requestCode == 200) {
           if (resultCode == RESULT_OK) {      
               data.getStringExtra("accountName2");   
               TextView user2 = (TextView)findViewById(R.id.user2);  
               user2.setText(sp.getString("accountName2", ""));}}
       else if (requestCode == 300) {
           if (resultCode == RESULT_OK) {      
               data.getStringExtra("accountName3");   
               TextView user3 = (TextView)findViewById(R.id.user3);  
               user3.setText(sp.getString("accountName3", ""));}}
       else if (requestCode == 400) {
           if (resultCode == RESULT_OK) {      
               data.getStringExtra("accountName4");   
               TextView user4 = (TextView)findViewById(R.id.user4);  
               user4.setText(sp.getString("accountName4", ""));}}
       else if (requestCode == 500) {
           if (resultCode == RESULT_OK) {      
               data.getStringExtra("accountName5");   
               TextView user5  = (TextView)findViewById(R.id.user5);  
               user5.setText(sp.getString("accountName5", ""));}}
       else if (requestCode == 600) {
           if (resultCode == RESULT_OK) {      
               data.getStringExtra("accountName6");   
               TextView user6 = (TextView)findViewById(R.id.user6);  
               user6.setText(sp.getString("accountName6", ""));}}
       else if (requestCode == 700) {
           if (resultCode == RESULT_OK) {      
               data.getStringExtra("accountName7");   
               TextView user7 = (TextView)findViewById(R.id.user7);  
               user7.setText(sp.getString("accountName7", ""));}}
       else if (requestCode == 800) {
           if (resultCode == RESULT_OK) {      
               data.getStringExtra("accountName8");   
               TextView user8 = (TextView)findViewById(R.id.user8);  
               user8.setText(sp.getString("accountName8", ""));}}
       else if (requestCode == 900) {
           if (resultCode == RESULT_OK) {      
               data.getStringExtra("accountName9");   
               TextView user9 = (TextView)findViewById(R.id.user9);  
               user9.setText(sp.getString("accountName9", ""));}}
       else if (requestCode == 1000) {
           if (resultCode == RESULT_OK) {      
               data.getStringExtra("accountName10");   
               TextView user10 = (TextView)findViewById(R.id.user10);  
               user10.setText(sp.getString("accountName10", ""));}}
    }

    @Override
    public void onResume() {
        super.onResume();
        onSaveSettings();
        SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this);
        TextView user1  = (TextView)findViewById(R.id.user1);
        user1.setText(sp.getString("accountName1", ""));
        TextView user2  = (TextView)findViewById(R.id.user2);
        user2.setText(sp.getString("accountName2", ""));
        TextView user3  = (TextView)findViewById(R.id.user3);
        user3.setText(sp.getString("accountName3", ""));
        TextView user4  = (TextView)findViewById(R.id.user4);
        user4.setText(sp.getString("accountName4", ""));
        TextView user5  = (TextView)findViewById(R.id.user5);
        user5.setText(sp.getString("accountName5", ""));
        TextView user6  = (TextView)findViewById(R.id.user6);
        user6.setText(sp.getString("accountName6", ""));
        TextView user7  = (TextView)findViewById(R.id.user7);
        user7.setText(sp.getString("accountName7", ""));
        TextView user8  = (TextView)findViewById(R.id.user8);
        user8.setText(sp.getString("accountName8", ""));
        TextView user9  = (TextView)findViewById(R.id.user9);
        user9.setText(sp.getString("accountName9", ""));
        TextView user10 = (TextView)findViewById(R.id.user10);
        user10.setText(sp.getString("accountName10", ""));
    }
}
4

2 回答 2

0

您想要使用 aListView并动态添加和删除行,并且您想要使用数组而不是变量 1 到 10。例如,此示例演示了如何通过单击 a 中的项目来启动新活动ListView

    ListView lv = (ListView)findViewById(R.id.listView); // You need to add this!

    // listening to single list item on click
    lv.setOnItemClickListener(new OnItemClickListener() {
      public void onItemClick(AdapterView<?> parent, View view,
          int position, long id) {

          Intent intent = new Intent(AccountSetup.this, AccountSettingActivity.class);
          intent.putExtra("rowNumber", position);
          startActivityForResult(intent, position * 100);
      }
    });

但是,如果不完全重写您的代码,我很难为您提供更详细的帮助。

于 2013-05-16T08:11:43.337 回答
0

您可以使用 tprefernce 值来保存每一行的状态。因此,当您退出活动时保存行的值(活动或非活动),以及当您启动活动时,将值加载到活动或非活动行。

这是一个例子:

public static void updateBooleanValue(boolean value, int id, Context context)
{
    SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
    Editor edit = preferences.edit();
    edit.putBoolean(context.getResources().getString(id), value);
    edit.commit();
}

public static boolean getBoolPreference(int id, Context context)
{
    // Initialize preferences
    SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
    return preferences.getBoolean(context.getResources().getString(id), false);

}
  • 函数 updateBooleanValue 设置当前行的状态(真或假)
  • 函数 getBoolPreference 获取当前行的状态(真或假)
  • id 是您在 string.xml 中设置的例如 R.String.id

就这样。

祝你好运。

于 2013-05-16T08:12:09.107 回答