0

ログイン フォームである HomeActivity があります。成功したら MainActivity を起動する必要がありますが、シミュレーターの任意の場所をクリックするとすぐに HomeActivity(ログイン フォーム)が表示されます。

MainActivity を起動する HomeActivity のコードは次のとおりです。

public void signIn(View V)
   {
        final Dialog dialog = new Dialog(HomeActivity.this);
        dialog.setContentView(R.layout.login);
        dialog.setTitle("Login");

        // get the Refferences of views
        final  EditText editTextUserName=(EditText)dialog.findViewById(R.id.editTextUserNameToLogin);
        final  EditText editTextPassword=(EditText)dialog.findViewById(R.id.editTextPasswordToLogin);

        Button btnSignIn=(Button)dialog.findViewById(R.id.buttonSignIn);

        // Set On ClickListener
        btnSignIn.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                // get The User name and Password
                String userName=editTextUserName.getText().toString();
                String password=editTextPassword.getText().toString();

                // fetch the Password form database for respective user name
                String storedPassword=loginDataBaseAdapter.getSinlgeEntry(userName);

                // check if the Stored password matches with  Password entered by user
                if(password.equals(storedPassword))
                {               

                    Intent intent = new Intent(getApplicationContext(), MainActivity.class);
                    HomeActivity.this.startActivity(intent);

                }
                else
                {
                    Toast.makeText(HomeActivity.this, "User Name or Password does not match", Toast.LENGTH_LONG).show();
                }
            }
        });

        dialog.show();
}`

そして、これが私のマニフェストの外観です。

        <meta-data android:name="android.support.PARENT_ACTIVITY"
            android:value="za.co.app.mycity.mainactivity.loginandregister.HomeActivity"></meta-data>
          </activity>`

また、MainActivity は単なる Facebook のようなサイド メニューです。念のため、`public class MainActivity extends Activity {

// Within which the entire activity is enclosed
DrawerLayout mDrawerLayout;

// ListView represents Navigation Drawer
ListView mDrawerList;

// ActionBarDrawerToggle indicates the presence of Navigation Drawer in the action bar
ActionBarDrawerToggle mDrawerToggle;    

// Title of the action bar
String mTitle="";

@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


    mTitle = (String) getTitle();       


    // Getting reference to the DrawerLayout
    mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);


    mDrawerList = (ListView) findViewById(R.id.drawer_list);

    // Getting reference to the ActionBarDrawerToggle
    mDrawerToggle = new ActionBarDrawerToggle(  this, 
                                                mDrawerLayout, 
                                                R.drawable.ic_drawer, 
                                                R.string.drawer_open,
                                                R.string.drawer_close){

        /** Called when drawer is closed */
        public void onDrawerClosed(View view) {
            getActionBar().setTitle(mTitle);
            invalidateOptionsMenu();

        }

        /** Called when a drawer is opened */
        public void onDrawerOpened(View drawerView) {
            getActionBar().setTitle("Select a river");
            invalidateOptionsMenu();
        }

    };

    // Setting DrawerToggle on DrawerLayout
    mDrawerLayout.setDrawerListener(mDrawerToggle);

    // Creating an ArrayAdapter to add items to the listview mDrawerList
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(
                getBaseContext(), 
                R.layout.drawer_list_item  , 
                getResources().getStringArray(R.array.rivers) 
            );

    // Setting the adapter on mDrawerList
    mDrawerList.setAdapter(adapter);

    // Enabling Home button
    getActionBar().setHomeButtonEnabled(true);

    // Enabling Up navigation
    getActionBar().setDisplayHomeAsUpEnabled(true);

    // Setting item click listener for the listview mDrawerList
    mDrawerList.setOnItemClickListener(new OnItemClickListener() {


        @Override
        public void onItemClick(AdapterView<?> parent,
                        View view,
                        int position,
                        long id) {          

            // Getting an array of rivers
            String[] rivers = getResources().getStringArray(R.array.rivers);

            //Currently selected river
            mTitle = rivers[position];              


            // Creating a fragment object
            RiverFragment rFragment = new RiverFragment();

            // Creating a Bundle object
            Bundle data = new Bundle();

            // Setting the index of the currently selected item of mDrawerList
            data.putInt("position", position);

            // Setting the position to the fragment
            rFragment.setArguments(data);

            // Getting reference to the FragmentManager
            FragmentManager fragmentManager  = getFragmentManager();

            // Creating a fragment transaction
            FragmentTransaction ft = fragmentManager.beginTransaction();

            // Adding a fragment to the fragment transaction
            ft.replace(R.id.content_frame, rFragment);

            // Committing the transaction
            ft.commit();

            // Closing the drawer
            mDrawerLayout.closeDrawer(mDrawerList);             

        }
    }); 
}


 @Override
 protected void onPostCreate(Bundle savedInstanceState) {
     super.onPostCreate(savedInstanceState);         
     mDrawerToggle.syncState(); 
 }

/** Handling the touch event of app icon */
@Override
public boolean onOptionsItemSelected(MenuItem item) {     
    if (mDrawerToggle.onOptionsItemSelected(item)) {
      return true;
    }
    return super.onOptionsItemSelected(item);
}


/** Called whenever we call invalidateOptionsMenu() */
@Override
public boolean onPrepareOptionsMenu(Menu menu) {
    // If the drawer is open, hide action items related to the content view
    boolean drawerOpen = mDrawerLayout.isDrawerOpen(mDrawerList);

    menu.findItem(R.id.action_settings).setVisible(!drawerOpen);
    return super.onPrepareOptionsMenu(menu);
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

} `

私はAndroid開発に非常に慣れていないので、とても興奮しています。

4

1 に答える 1

0

あなたの家の活動としてこれを試してください:

public void signIn(View V)
 {
    final Dialog dialog = new Dialog(HomeActivity.this);
    dialog.setContentView(R.layout.login);
    dialog.setTitle("Login");
    dialog.setCancelable(false);
    dialog.setCanceledOnTouchOutside(false);
    // get the Refferences of views
    final  EditText editTextUserName=(EditText)dialog.findViewById(R.id.editTextUserNameToLogin);
    final  EditText editTextPassword=(EditText)dialog.findViewById(R.id.editTextPasswordToLogin);

    Button btnSignIn=(Button)dialog.findViewById(R.id.buttonSignIn);

    // Set On ClickListener
    btnSignIn.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            // get The User name and Password
            String userName=editTextUserName.getText().toString();
            String password=editTextPassword.getText().toString();

            // fetch the Password form database for respective user name
            String storedPassword=loginDataBaseAdapter.getSinlgeEntry(userName);

            // check if the Stored password matches with  Password entered by user
            if(password.equals(storedPassword))
            {               

                Intent intent = new Intent(getApplicationContext(), MainActivity.class);
                HomeActivity.this.startActivity(intent);

            }
            else
            {
                Toast.makeText(HomeActivity.this, "User Name or Password does not match", Toast.LENGTH_LONG).show();
            }
        }
    });

    dialog.show();
}

コードにdialog.setCancelable(false)andを追加しました。dialog.setCanceledOnTouchOutside(false)

于 2013-06-11T03:21:00.187 に答える