Automatic focus on next edittext for digit password entry - android-widget

From my app, I expect more like that implemented on IPhone with four digits for password entry, which means with masked dot. I'm already done with the correct entry. Using the concrete implementation for TextWatcher in addTextChangedListener, however, when I entered 4 wrong digits and the focus was returned back to the first one and every digit was cleared as well. In this case, the original soft keyboard for digit input switched back to the qwert one and any letter or digit entered became visible without masking with dot.
myEditText1.addTextChangedListener(new MyTextWatcher(mPasswordEditText){
#Override
public void afterTextChanged(Editable arg0) {
}
#Override
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
s = mPasswordEditText.getText();
int cacheH = UnlockScreen.this.mPasswordEditText.getHeight();
if(s.length() == 1){
UnlockScreen.this.mPasswordEditText1.requestFocus();
mPasswordEditText.setInputType(InputType.TYPE_TEXT_VARIATION_PASSWORD);
mPasswordEditText.setTransformationMethod(PasswordTransformationMethod.getInstance());
mPasswordEditText.setMinHeight(cacheH);
}
}
});
But it didn't work well as expected.
XML layout is:
<EditText
android:id="#+id/unlock_screen_password_edittext_1"
style="#style/passcodeStyle"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight=".20"
android:layout_marginRight="10dip"
android:digits="#string/digits_only"
android:gravity="center"
android:inputType="number"
android:password="true"
android:maxLength="1"/>
Any idea, folks?

I'm using setText(s.toString().replace(s.toString(), '.')); instead of mPasswordEditText.setInputType(InputType.TYPE_TEXT_VARIATION_PASSWORD);and mPasswordEditText.setTransformationMethod(PasswordTransformationMethod.getInstance());
And set flags for textChange status in each edittext.
I'm not sure it's the best solution but for my app, this works fine and so far I've had no idea of a better way.

Related

Unity Dynamic UI number selection with plus and minus buttons

while programming a game in Unity, I had troubles with incrementing this new feature: a dynamic UI number selection with plus and minus buttons.
What I want precisely:
1. three buttons, one blank in the middle displays the number, two with plus and minus signs which, when clicked, increment or decrement number by 1. WORKS OK!
image of what I did
2. (this is where it gets tricky) When user presses for more than, say, .2s, then it increments the value of the central button pretty fast as long as the user is still pressing on the button. Would avoid player from pressing hundred times the button because it increments only by 1.
3. Eventually add an acceleration phase of the increase (at the start increases by 3/s for example and at the max by 20/s or something like that)
Some help on this would be really great, thanks for those who will take time answering :)
edit: found somebody who asked same question on another post -->https://stackoverflow.com/questions/22816917/faster-increments-with-longer-uibutton-hold-duration?rq=1 but I don't understand a single inch of code...(doesn't look like c#) ;( help!
Are you using the button's OnClick() Event? It only calls the function once even if the user is holding down the key.
If you are not sure how to configure it. Here is a tutorial, you can use.
https://vionixstudio.com/2022/05/21/making-a-simple-button-in-unity/
Also the middle button can be a text field.
edit: found the solution myself. The principal issue I encountered when trying to make the button was a way to know if the button was being pressed (a bool variable). I already knew when it was clicked and when it was released with the OnPointerUp and OnPointerDown methods in the event trigger component. I then found the Input.GetButton() funct, which returns true if the button passed in parameter (here the mouse's left click) is pressed.
Here's the code (I didn't make an acceleration phase 'cause I was bored but it can be done pretty easily once you've got the Input.GetButton statement):
public class PlusAndMinus : MonoBehaviour
{
[SerializeField] private GameManager gameManagerScript;
[SerializeField] private int amount;
private bool isPressedForLong=false;
[SerializeField] private float IncreasePerSecWhenPressingLonger;
public void PointerDown()
{
if (!gameManagerScript.isGameActive)
{
StartCoroutine(PointerDownCoroutine());
}
}
private IEnumerator PointerDownCoroutine()
{
yield return new WaitForSeconds(.1f);#.2f might work better
if (Input.GetMouseButton(0))
{
isPressedForLong = true;
}
}
public void PointerUp()
{
if (!gameManagerScript.isGameActive)
{
isPressedForLong = false;
gameManagerScript.UpdateNumberOfBalls("Expected", amount);
}
}
private void Update()
{
if (isPressedForLong)
{
gameManagerScript.UpdateNumberOfBalls("Expected", amount * IncreasePerSecWhenPressingLonger * Time.deltaTime);
}
}
}
The PointerDown event in the event trigger component calls the PointerDown function in the script (same for PointerUp).
The value of "amount" var is set to 1 for the plus button and -1 for the minus button.
The two if statements checking if game is not active are for my game's use but aren't necessary.
Finally, the gameManagerScript.UpdateNumberOfBalls("expected",amount); statements call a function that updates the text in the middle by the amount specified. Here's my code ("expected" argument is for my game's use):
#inside the function
else if (startOrEndOrExpected == "Expected")
{
if (!((numberOfBallsExpected +amount) < 0) & !((numberOfBallsExpected +amount) > numberOfBallsThisLevel))
{
if (Math.Round(numberOfBallsExpected + amount) != Math.Round(numberOfBallsExpected))
{
AudioManager.Play("PlusAndMinus");
}
numberOfBallsExpected += amount;
numberOfBallsExpectedText.text = Math.Round(numberOfBallsExpected).ToString();
}
}
0 and numberOfBallsThisLevel are the boundaries of the number displayed.
Second if statement avoids the button click sound to be played every frame when the user presses for long on the button (only plays when number increments or decrements by 1 or more).
Hope this helps!

Can't load another level

I was following an online tutorial to make this little game in Unity and when I got to the end, I wound up with two different levels. The only problem is that I can only play one of them. I tried adding it in the build settings but for some reason, it just isn't working. Maybe there's a problem with the code? I don't know. This is my first-ever working with Unity so I'm still figuring things out.
public class LevelControllerScript : MonoBehaviour
{
private static int _nextLevelIndex = 1;
private NewBehaviourScript1[] _enemies;
private void OnEnable()
{
_enemies = FindObjectsOfType<NewBehaviourScript1>();
}
// Update is called once per frame
void Update()
{
foreach(NewBehaviourScript1 NewBehaviourScript1 in _enemies)
{
if (NewBehaviourScript1 != null)
return;
}
Debug.Log("You killed all enemies!");
_nextLevelIndex++;
string nextLevelName = "Level" + _nextLevelIndex;
SceneManager.LoadScene(nextLevelName);
}
}
string nextLevelName = "Level" + _nextLevelIndex;
This line of code makes the string nextLevelName something like Level1, or Level2, however, you are trying to match up to the string leveltwo.
To fix this you need to match up the strings, I believe you can just change the scene leveltwo to Level2 (and change all of the other scenes accordingly).

I have button that execute my method but ignoring the if statement

i have a problem. iam making a game that have button for buy item , and the button have no issue, it run normally, but the problem is the button immediately executes the method without seeing that the method contains an if statement, so what is wrong here Here's the code :
public void TakeMoney()
{
int minusCoin = PlayerInfo.coin -= 5;
PlayerPrefs.SetInt("MyCoinAmount", minusCoin);
}
public void DisableBuyButton()
{
disableBuyButtonRocket3 = 1;
PlayerPrefs.SetInt("DisableBuyButtonRocket3", disableBuyButtonRocket3);
}
public void Buy()
{
int getPlayerCoin = PlayerInfo.coin;
if (getPlayerCoin > 4)
{
if (MyRocket._rocket3 == 1)
{
GetComponent<AudioSource>().Play();
MyRocket._rocket3 = 1;
PlayerPrefs.SetInt("Rocket3Bought", MyRocket._rocket3);
}
}
}
so the button will perform all of the 3 functions above, but as can be seen that I make an if statement so the item can only be purchased if we already have enough money, but when I try I try to reset all values, so my money by default it is 0, but I can buy the item and my money becomes a minus.
Btw, PlayerInfo above is a script from another scene.
Sorry for my bad english.
Your problem is that you do everything in separated methods. It's basically not a problem - but, when you execute them concurrently, it is. Also, your methods are completely independent of each other - an if statement in one will not affect the others' execution. The solution is that you first remove the event listeners to TakeMoney and DisableBuyButton. Then, call them from Buy. If you think it through, it is more logical. Also, remove the temporary variables, as they slow down your code a bit and also make it less readable. Final code could be something like this:
public void TakeMoney()
{
PlayerInfo.coin -= 5;
PlayerPrefs.SetInt("MyCoinAmount", PlayerInfo.coin);
}
public void DisableBuyButton()
{
PlayerPrefs.SetInt("DisableBuyButtonRocket3", 1);
}
public void Buy()
{
if (PlayerInfo.coin > 4) {
TakeMoney();
DisableBuyButton();
if (MyRocket._rocket3 != 1 /* changed this, because it seemed that you want to check whether it is NOT one */)
{
GetComponent<AudioSource>().Play();
// I changed the if statement because this
MyRocket._rocket3 = 1;
PlayerPrefs.SetInt("Rocket3Bought", MyRocket._rocket3);
}
}
}
the playerinfo.coin value would be more than 4 because it will decrease coin at click so it will minus only 5 at a click you could check playerinfo.coin value

How do i set int value of inputfield with Content Type set to Integer in Unity?

I am working with Unity 2018.
I have inputfield and 2 buttons.
When i click a button the expected behaviour is for the counter inside the field to change accordingly. For example:
{
int current_value = int.Parse(this.craftOrderInput.text);
if(current_value>0)
this.craftOrderInput.text = (current_value - 1) + "";
}
But this obviously fails because the (current_value - 1) + ""; is a string, but input field expects an integer. Setting an integer like so this.craftOrder.text = (current_value - 1) ; obviously cannot compile because text component expects a string.
I wanted to try Unity forums first but i cannot ask questions there because my style of writing reminds them of spam.
tldr; How do i set int value of inputfield with Content Type set to Integer?
The content type only affects which characters the user is allowed to type in the inputfield. Even when the content type is set to integer, changing the input field is still done by changing the text as a string. I made a little demo to test this and the following script worked for me.
using UnityEngine;
using UnityEngine.UI;
public class MakeSmaller : MonoBehaviour {
InputField inputField;
private void Awake()
{
inputField = GetComponent<InputField>();
}
public void Subtract()
{
int orig = int.Parse(inputField.text);
inputField.text = (orig - 1).ToString();
}
}

Mutil monitor mouse tracking

I have a need to track mouse position. While I have attempted several ways of doing this, I never am able to follow/capture the position if the mouse is on another monitor.
[DllImport("user32.dll")]
public static extern bool GetCursorPos(ref Point pt);
[DllImport("user32.dll")]
public static extern bool GetCursorInfo(out CURSORINFO pci);
public void GetPosition(out int X, out int Y)
{
Point pt = new Point(0, 0);
X = Y = 0;
if (MouseMonitor.GetCursorPos(ref pt))
{
X = pt.X;
Y = pt.Y;
}
This works but only on one screen. I also read that I might try GetCursorInfo. I have attempted this but it always comes back false.
[DllImport("user32.dll")]
public static extern bool GetCursorInfo(out CURSORINFO pci);
Any suggestions? My goal is to track mouse position (outside of my own app) regardless of what screen it is on.
Your sample code works for me on my dual-monitor system...
You can actually simplify things quite a bit by using the .NET Framework: the System.Windows.Forms.Cursor class has a static Position property.
For example, I created a new Windows Forms project and then dragged a System.Windows.Forms.Timer onto the form. I set the Enabled property to true, and added this code to the Tick event:
this.Text = string.Format("{0}, {1}", Cursor.Position.X, Cursor.Position.Y);
Ran the project and it worked as expected on both my monitors...
Use DWORD GetMessagePos() - it gives you the last windows message mouse position. But be careful, it returns DWORD, but inside there are two SHORTS (16-bit signed ints) packed. So LOWORD/HIWORD macros (or C# respective) won't work.
http://msdn.microsoft.com/en-us/library/ms644938(VS.85).aspx