从一个活动连接到AndroidX中的其他活动

时间:2019-07-14 23:54:05

标签: java android-studio

我正在学习构建android应用程序,我需要一些具体帮助。 我想从FakeActivity连接到LogInActivity。 我的代码成功构建并运行,但是在应用程序中,当我单击FakeActivity中的按钮帮助以连接到LogInActivity时,该应用程序会从程序中输出我的消息,并对该消息进行<< >> 并说我正在使用AndroidX。

它是我的AndroidManifest.xml:

<uses-permission android:name="android.permission.SEND_SMS" />
    <uses-permission android:name="android.permission.RECEIVE_SMS" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/tic_tac_toe"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">


        <activity android:name=".MainActivity"></activity>

        <receiver
            android:name=".MySMSReceiver"
            android:enabled="true"
            android:exported="true">
            <intent-filter>
                <action android:name="android.provider.Telephony.SMS_RECEIVED" />
            </intent-filter>
        </receiver>

        <activity android:name=".FakeActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".LogInActivity"></activity>


        <service
            android:name=".MyFirebaseMessagingService">
            <intent-filter>
                <action android:name="com.google.firebase.MESSAGING_EVENT" />
            </intent-filter>
        </service>
    </application>

</manifest>

它是我的FakeActivity ::

package maa.tic_tac_to;

import android.content.Intent;
import android.graphics.Color;
import android.graphics.Typeface;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import static android.icu.lang.UCharacter.GraphemeClusterBreak.V;

public class FakeActivity extends AppCompatActivity implements View.OnClickListener{

    Button helpBtn;
    private Button [][] buttons = new Button[3][3];

    private boolean player1Turn = true;


    private int roundCount ;

    private int player1Points;
    private int player2Points;

    private TextView textViewPlayer1;
    private TextView textViewPlayer2;

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



        Typeface face = Typeface.createFromAsset(getAssets(),
                "fonts/BRoya.ttf");

        helpBtn =  (Button) findViewById(R.id.buttonHelp);
        helpBtn.setTypeface(face);



        textViewPlayer1 = findViewById(R.id.text_view_p1);
        textViewPlayer2 = findViewById(R.id.text_view_p2);


        for (int i = 0; i < 3 ; i++){
            for (int j = 0; j < 3; j++){
                String buttonID = "button_" + i + j;
                int resID = getResources().getIdentifier(buttonID,"id", getPackageName());
                buttons[i][j] = findViewById(resID);
                buttons[i][j].setOnClickListener(this);

            }
        }
        Button buttonReset = findViewById(R.id.button_reset);
        buttonReset.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
               resetGame();
            }
        });
    }

//////// Connect from FakeActivity to LogInActivity

    public void Btn_click1(View v){
        Intent GoActivity = new Intent(FakeActivity.this, LogInActivity.class);
        startActivity(GoActivity);
    }




    @Override
    public void onClick(View v) {
      if (!((Button) v).getText().toString().equals("")) {
          return;
      }
      if (player1Turn){
          ((Button) v).setText("X");
      }else {
          ((Button) v).setText("O");
      }

      roundCount++;

      if (checkForWin()){
          if (player1Turn) {
              player1Wins();
          } else {
              player2Wins();
          }
      }else if (roundCount == 9){
          draw();
      } else {
          player1Turn = !player1Turn;
      }
    }

    private boolean checkForWin(){
       String[][] field = new String[3][3];

        for (int i = 0; i < 3 ; i++) {
            for (int j = 0; j < 3; j++) {
                field[i][j] = buttons[i][j].getText().toString();
            }
        }

        for (int i = 0; i < 3; i++){
                if (field[i][0].equals(field[i][1])
                        && field[i][0].equals(field[i][2])
                        && !field[i][0].equals("")){
                    return true;
                }
        }

        for (int i = 0; i < 3; i++){
            if (field[0][i].equals(field[1][i])
                    && field[0][i].equals(field[2][i])
                    && !field[0][i].equals("")){
                return true;
            }
        }

        if (field[0][0].equals(field[1][1])
                && field[0][0].equals(field[2][2])
                && !field[0][0].equals("")){
            return true;
        }

        if (field[0][2].equals(field[1][1])
                && field[0][2].equals(field[2][0])
                && !field[0][2].equals("")){
            return true;
        }

        return false;
    }

    private void player1Wins() {
        player1Points++;
        Toast.makeText(this,"Player 1 Wins!", Toast.LENGTH_SHORT).show();
        updatePointsText();
        resetBoard();
    }

    private void player2Wins() {
        player2Points++;
        Toast.makeText(this,"Player 2 Wins!", Toast.LENGTH_SHORT).show();
        updatePointsText();
        resetBoard();
    }

    private void draw() {
        Toast.makeText(this,"Draw!", Toast.LENGTH_SHORT).show();
        resetBoard();
    }

    private void updatePointsText() {
        textViewPlayer1.setText("Player1: " + player1Points);
        textViewPlayer2.setText("Player2: " + player2Points);
    }



    private void resetBoard() {
        for (int i = 0; i < 3; i++){
            for (int j = 0; j < 3; j++){
                buttons[i][j].setText("");
            }
        }

        roundCount = 0;
        player1Turn = true;
    }


    private void resetGame() {
         player1Points = 0;
         player2Points = 0;
         updatePointsText();
         resetBoard();
    }



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

        outState.putInt("roundCount", roundCount);
        outState.putInt("player1Points", player1Points);
        outState.putInt("player2Points", player2Points);
        outState.putBoolean("player1Turn", player1Turn);
    }

    @Override
    protected void onRestoreInstanceState(Bundle savedInstanceState) {
        super.onRestoreInstanceState(savedInstanceState);

        roundCount = savedInstanceState.getInt("roundCount");
        player1Points = savedInstanceState.getInt("player1Points");

        player2Points = savedInstanceState.getInt("player2Points");
        player1Turn = savedInstanceState.getBoolean("player1Turn");

    }
}

和我的LogInActivity :::

package maa.tic_tac_to;

import android.app.PendingIntent;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInstaller;
import android.content.pm.PackageManager;
import android.graphics.Color;
import android.graphics.Typeface;
import android.net.Uri;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import java.io.IOException;

public class LogInActivity extends AppCompatActivity {

    int incorrectPass = 0;
    Button btnPass;
    EditText editText;
    TextView textView;

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

        getWindow().getDecorView().setBackgroundColor(Color.rgb(249, 247, 209));
        btnPass = (Button) findViewById(R.id.buttonCheckPass);
        editText = (EditText) findViewById(R.id.editTextPass);
        textView = (TextView) findViewById(R.id.textViewLogIn);

        Typeface face = Typeface.createFromAsset(getAssets(),
                "fonts/BRoya.ttf");

        btnPass.setTypeface(face);
        editText.setTypeface(face);
        textView.setTypeface(face);
    }

    public void Btn_clickPass(View V){

        String enteredText = editText.getText().toString();

        if(enteredText.matches(""))
            return;

        if(enteredText.matches("yahossein") == false )
        {
            incorrectPass++;
            Toast.makeText(getApplicationContext(),
                    "اشتباه است",
                    Toast.LENGTH_LONG).show();

            if(incorrectPass < 3)
            {
                return;
            }

            Toast.makeText(getApplicationContext(),
                    "uninstalling app...",
                    Toast.LENGTH_LONG).show();
            // if entered incorrect pass more than 3 times
            unInstallApp();
            return;
        }

        incorrectPass = 0;
        editText.setText("");
        Intent GoActivity = new Intent(this, MainActivity.class);
        startActivity(GoActivity);
    }

    private void unInstallApp()
    {


        Intent intent = new Intent(Intent.ACTION_DELETE);
        intent.setData(Uri.parse("package:maa.tic_tac_to"));
        startActivity(intent);


    }
}

任何人都可以帮助我吗?!?!!!?!!?!

0 个答案:

没有答案