有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java单击活动中的一个按钮将同时打开两个活动

这里我有三个activities,其中一个是login activity。每当我单击login button时,两个活动同时显示。另外两个activities是:VendorAccount.javaPromotion.java。代码如下:

这里我使用了一个intent,每当logging in的任务成功时,它就会启动Promotion.class

登录。java

public class Login extends AppCompatActivity {

private static final String TAG = "Login";
private FirebaseAuth mAuth;
private Button loginButton,signupButton;
EditText eml,pass;
String email,password;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_login);

    mAuth = FirebaseAuth.getInstance();

    loginButton=(Button)findViewById(R.id.btn_login);
    signupButton=(Button)findViewById(R.id.newuser);
    loginButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            eml=findViewById(R.id.edt_email);
            pass=findViewById(R.id.edt_password);

            email=eml.getText().toString();
            password=pass.getText().toString();

           // startActivity(new Intent(Login.this,Promotion.class));
            mAuth.signInWithEmailAndPassword(email,password)
                    .addOnCompleteListener(Login.this, new OnCompleteListener<AuthResult>() {
                        @Override
                        public void onComplete(@NonNull Task<AuthResult> task) {
                            if (task.isSuccessful()) {
                                // Sign in success, update UI with the signed-in user's information
                                Log.d(TAG, "signInWithEmail:success");
                                startActivity(new Intent(Login.this,Promotion.class));
                                FirebaseUser user = mAuth.getCurrentUser();
                                //updateUI(user);
                            } else {
                                // If sign in fails, display a message to the user.
                                Log.w(TAG, "signInWithEmail:failure", task.getException());
                                Toast.makeText(Login.this, "Authentication failed.",
                                        Toast.LENGTH_SHORT).show();
                                //updateUI(null);
                            }

                            // ...
                        }
                    });
        }
    });

    signupButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            startActivity(new Intent(Login.this,Signup.class));
        }
    });
}

@Override
public void onStart() {
    super.onStart();
    // Check if user is signed in (non-null) and update UI accordingly.
    FirebaseUser currentUser = mAuth.getCurrentUser();
    //updateUI(currentUser);
}
}

现在,当我单击signin按钮时,它应该会打开Promotion.class,它确实打开了VendorAccount.class,但也打开了

促销。java

public class Promotion extends AppCompatActivity {

private BottomBar bottomBar;
private Button coupons,scanqr;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.promotion);

    Toast.makeText(getApplicationContext(),
            "Promotion called",Toast.LENGTH_LONG).show();

    coupons=(Button)findViewById(R.id.coupons);
    scanqr=(Button)findViewById(R.id.scanqr);

    bottomBar=BottomBar.attach(this,savedInstanceState);
    bottomBar.setItems(R.menu.bottombars_menu);
    bottomBar.setOnMenuTabClickListener(new OnMenuTabClickListener() {
        @Override
        public void onMenuTabSelected(int menuItemId) {
            if (menuItemId==R.id.tab_account){
                //Toast.makeText(getApplicationContext(),"Accounts",Toast.LENGTH_SHORT).show();
                startActivity(new Intent(Promotion.this,VendorAccount.class));
            } else if (menuItemId==R.id.tab_details){
                //Toast.makeText(getApplicationContext(),"Customer Details",Toast.LENGTH_SHORT).show();
                startActivity(new Intent(getApplicationContext(),ShowCustomer.class));
            } else if (menuItemId==R.id.tab_coupons) {
                //Toast.makeText(getApplicationContext(),"Coupons",Toast.LENGTH_SHORT).show();
                startActivity(new Intent(Promotion.this,ShowCoupons.class));
            }
        }

        @Override
        public void onMenuTabReSelected(int menuItemId) {

        }
    });

    // Setting colors for different tabs when there's more than three of them.
    // You can set colors for tabs in three different ways as shown below.
    bottomBar.mapColorForTab(0, ContextCompat.getColor(this, R.color.colorAccent));
    bottomBar.mapColorForTab(1, 0xFF5D4037);
    bottomBar.mapColorForTab(2, "#7B1FA2");

    coupons.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            startActivity(new Intent(Promotion.this,Coupons.class));
        }
    });

    scanqr.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Toast.makeText(getApplicationContext(),"QR scan",Toast.LENGTH_SHORT).show();
            configure_button();
        }
    });

}

@Override
protected void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);

    // Necessary to restore the BottomBar's state, otherwise we would
    // lose the current tab on orientation change.
    bottomBar.onSaveInstanceState(outState);
}

private void configure_button() {
    if (ActivityCompat.checkSelfPermission(this, 安卓.Manifest.permission.CAMERA) !=
            PackageManager.PERMISSION_GRANTED ) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            requestPermissions(new String[]{安卓.Manifest.permission.CAMERA}
                    , 0);
        }
        return;
    }
    if (ActivityCompat.checkSelfPermission(this, 安卓.Manifest.permission.CAMERA) ==
            PackageManager.PERMISSION_GRANTED ){
        startActivity(new Intent(Promotion.this,QRScan.class));
    }
}

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
    if(requestCode == 0){
        if(grantResults.length>0 && grantResults[0]==PackageManager.PERMISSION_GRANTED ){

            new Thread(new Runnable() {
                @Override
                public void run() {
                    configure_button();
                    startActivity(new Intent(Promotion.this,QRScan.class));
                }
            }).start();
        }else{
            Toast.makeText(getApplicationContext(), "Access Denied ! Plesae Choose Camera Access Manually ", Toast.LENGTH_SHORT).show();

        }
    }
}

@Override
public void onBackPressed() {
    AlertDialog alertDialog = new AlertDialog.Builder(Promotion.this).create();
    alertDialog.setTitle("System Message!!");
    alertDialog.setMessage("Hey There,!!"+"\n"+"Do Tou Really Want to Leave?");
    alertDialog.setButton(AlertDialog.BUTTON_POSITIVE, "Yes",
            new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                    dialog.dismiss();
                    finish();
                }
            });
    alertDialog.setButton(AlertDialog.BUTTON_NEGATIVE, "No",
            new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                    dialog.dismiss();

                }
            });
    alertDialog.show();
}
}

卖方账户。java

public class VendorAccount extends AppCompatActivity {

private ImageView displayPic;
private EditText name,addr1,addr2,phno;
private Button save;
private Button image1,image2,image3,image4;
String vendorname,vendoraddr1,vendoraddr2,vendorphno;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_vendor_account);

    Toast.makeText(getApplicationContext(),
            "VendorAccount called",Toast.LENGTH_LONG).show();

    displayPic=(ImageView)findViewById(R.id.displaypicture);
    image1=(Button) findViewById(R.id.image1);
    image2=(Button) findViewById(R.id.image2);
    image3=(Button) findViewById(R.id.image3);
    image4=(Button) findViewById(R.id.image4);
    save=(Button)findViewById(R.id.saveButton);
    name=(EditText)findViewById(R.id.vendorname);
    addr1=(EditText)findViewById(R.id.vendoradd1);
    addr2=(EditText)findViewById(R.id.vendoradd2);
    phno=(EditText)findViewById(R.id.vendorphno);

    displayPic.setOnClickListener(choosePic);
    image1.setOnClickListener(choosePic);
    image2.setOnClickListener(choosePic);
    image3.setOnClickListener(choosePic);
    image4.setOnClickListener(choosePic);

    save.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Toast.makeText(getApplicationContext(),"Saved in database",Toast.LENGTH_SHORT).show();
            vendorname=name.getText().toString();
            vendoraddr1=addr1.getText().toString();
            vendoraddr2=addr2.getText().toString();
            vendorphno=phno.getText().toString();
        }
    });
}

public View.OnClickListener choosePic=new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        Intent i = new Intent(Intent.ACTION_PICK,
                安卓.provider.MediaStore.Images.Media.INTERNAL_CONTENT_URI);
        final int ACTIVITY_SELECT_IMAGE = 1234;
        startActivityForResult(i, ACTIVITY_SELECT_IMAGE);
    }
};

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    switch(requestCode) {
        case 1234:
            if(resultCode == RESULT_OK){
                Uri selectedImage = data.getData();
                String[] filePathColumn = {MediaStore.Images.Media.DATA};

                Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null);
                cursor.moveToFirst();
                int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
                String filePath = cursor.getString(columnIndex);
                cursor.close();
                Bitmap yourSelectedImage = BitmapFactory.decodeFile(filePath);
            }
        }
}
}

如您所见,我在每个类的onCreate方法中放置了Toast消息,以便知道调用了哪个class。因此,当我单击signin按钮时,将显示Promotion.javaToast消息,然后首先显示VendorAccount.javaToast消息和VendorAccount.java的布局

有人能帮我解决这个问题吗

manifestactiviy_login文件如下:

AndroidManifest。xml

<manifest xmlns:安卓="http://schemas.安卓.com/apk/res/安卓"
package="com.example.vendorapp">

<uses-permission 安卓:name="安卓.permission.INTERNET" />
<uses-permission 安卓:name="安卓.permission.CAMERA" />

<application
    安卓:allowBackup="true"
    安卓:icon="@mipmap/ic_launcher"
    安卓:label="@string/app_name"
    安卓:roundIcon="@mipmap/ic_launcher_round"
    安卓:supportsRtl="true"
    安卓:theme="@style/AppTheme">
    <activity
        安卓:name=".Coupons"
        安卓:theme="@style/AppThemeNoBar" />
    <activity
        安卓:name=".Promotion"
        安卓:label="Promotion"
        安卓:theme="@style/AppThemeNoBar" />
    <activity
        安卓:name=".Login"
        安卓:label="Login"
        安卓:theme="@style/AppThemeNoBar">
        <intent-filter>
            <action 安卓:name="安卓.intent.action.MAIN" />

            <category 安卓:name="安卓.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity
        安卓:name=".Signup"
        安卓:label="SignUp"
        安卓:theme="@style/AppThemeNoBar" />
    <activity
        安卓:name=".CouponsNew"
        安卓:theme="@style/AppThemeNoBar" />
    <activity
        安卓:name=".QRScan"
        安卓:theme="@style/AppThemeNoBar" />
    <activity 安卓:name=".ShowCoupons"></activity>
    <activity 安卓:name=".qrinfo"></activity>
    <activity
        安卓:name=".ShowCustomer"
        安卓:theme="@style/AppThemeNoBar" />
    <activity
        安卓:name=".VendorAccount"
        安卓:theme="@style/AppThemeNoBar">

    </activity>
</application>

活动\u登录。xml

<RelativeLayout xmlns:安卓="http://schemas.安卓.com/apk/res/安卓"
xmlns:app="http://schemas.安卓.com/apk/res-auto"
xmlns:tools="http://schemas.安卓.com/tools"
安卓:layout_width="match_parent"
安卓:layout_height="match_parent"
安卓:background="#ff0000"
tools:context="com.example.vendorapp.Login">

<LinearLayout
    安卓:id="@+id/layout1"
    安卓:layout_width="match_parent"
    安卓:layout_height="match_parent"
    安卓:layout_margin="20sp"
    安卓:gravity="center"
    安卓:orientation="vertical">

    <LinearLayout
        安卓:layout_width="match_parent"
        安卓:layout_height="wrap_content"
        安卓:orientation="vertical">

        <EditText
            安卓:id="@+id/edt_email"
            安卓:layout_width="match_parent"
            安卓:layout_height="50sp"
            安卓:drawablePadding="10sp"
            安卓:drawableStart="@drawable/ic_account_circle_white_24dp"
            安卓:drawableLeft="@drawable/ic_account_circle_white_24dp"
            安卓:hint="Email"
            安卓:inputType="textEmailAddress"
            安卓:paddingEnd="10sp"
            安卓:paddingStart="10sp"
            安卓:textColor="@color/white"
            安卓:textColorHint="@color/white"
            安卓:backgroundTint="@color/white" />

        <EditText
            安卓:id="@+id/edt_password"
            安卓:layout_width="match_parent"
            安卓:layout_height="50sp"
            安卓:layout_marginTop="15sp"
            安卓:drawablePadding="10sp"
            安卓:drawableStart="@drawable/ic_lock_white_24dp"
            安卓:drawableLeft="@drawable/ic_lock_white_24dp"
            安卓:hint="Password"
            安卓:inputType="textPassword"
            安卓:paddingEnd="10sp"
            安卓:paddingStart="10sp"
            安卓:textColor="@color/white"
            安卓:textColorHint="@color/white"
            安卓:backgroundTint="@color/white" />

    </LinearLayout>

    <LinearLayout
        安卓:layout_width="match_parent"
        安卓:layout_height="wrap_content"
        安卓:orientation="vertical">

        <LinearLayout
            安卓:id="@+id/buttonPanel"
            安卓:layout_width="match_parent"
            安卓:layout_height="wrap_content"
            安卓:orientation="vertical"
            安卓:layout_marginTop="15sp"
            安卓:background="@color/white">

            <Button
                安卓:id="@+id/btn_login"
                安卓:layout_width="match_parent"
                安卓:layout_height="match_parent"
                安卓:background="?attr/selectableItemBackground"
                安卓:text="Login"
                安卓:textColor="#000000"
                安卓:textSize="17sp"
                安卓:textStyle="bold" />

        </LinearLayout>

        <Button
            安卓:id="@+id/newuser"
            安卓:layout_width="match_parent"
            安卓:layout_height="wrap_content"
            安卓:fontFamily="sans-serif"
            安卓:gravity="center"
            安卓:background="?attr/selectableItemBackground"
            安卓:text="New User? Register Here."
            安卓:textColor="@安卓:color/white"
            安卓:textSize="15sp"/>

    </LinearLayout>

</LinearLayout>

<ProgressBar
    安卓:id="@+id/progressBar"
    安卓:layout_width="30sp"
    安卓:layout_height="30sp"
    安卓:layout_gravity="center"
    安卓:layout_marginBottom="20sp"
    安卓:visibility="gone" />


共 (2) 个答案

  1. # 1 楼答案

    在onCreate期间初始化Promotiion.class时,会触发onMenuTabSelected

    尝试设置一个int变量来覆盖第一个onMenuTabSelected

    public class Promotion extends AppCompatActivity {
    private int itemSelectedCounter; 
    bottomBar.setOnMenuTabClickListener(new OnMenuTabClickListener() {
        @Override
        public void onMenuTabSelected(int menuItemId) {
           if(++itemSelectedCounter > 1) {
              // your onMenuTabSelectedCode
           }
        }
    
  2. # 2 楼答案

    你用的是拉夫维克的底部栏库吗setOnMenuTabClickListener()是版本1.3.9的方法,但最新版本是2.3.1。API已经改变了很多。我想知道该版本是否存在导致在初始设置时调用setOnMenuTabClickListener()的错误(默认情况下选择第一个选项卡是否正确?)这就解释了为什么Promotion一发射,它就立即发射VendorAccount。尝试升级到2.3.1,文档是here