How to alter the active text field in AS3? - android

I have 6 text fields on the stage along with a keypad with the numbers 0-9.
Ultimately this is going to be an Android app. And I want to be able the select a text field and then press the keypad buttons and have the numbers appear in the active text field.
I've been trying to Google active field and similar searches and can't seem to find a reference.
Keep in mind I'm fumbling around in the dark from what I've tried to gather from multiple tutorials. This code is probably complete garbage:
package {
public class main {
import flash.display.MovieClip;
import flash.events.MouseEvent;
import fl.managers.FocusManager;
var focus:FocusManager = new FocusManager(this);
btn_1.addEventListener(MouseEvent.MOUSE_DOWN, inputNumber);
public function inputNumber(m:MouseEvent){
focus.text = 1;
}
public function main() {
// constructor code
}
}
}
Current Errors:
C:\Users\Otaku\Documents\lottocount\main.as, Line 19, Column 35 1118: Implicit coercion of a value with static type flash.display:InteractiveObject to a possibly unrelated type flash.text:TextField.
C:\Users\Otaku\Documents\lottocount\main.as, Line 14, Column 44 1120: Access of undefined property onMyButtonClick.
C:\Users\Otaku\Documents\lottocount\main.as, Line 14, Column 3 1120: Access of undefined property btn_1.
C:\Users\Otaku\Documents\lottocount\main.as, Line 13, Column 47 1120: Access of undefined property onFocusIn.
C:\Users\Otaku\Documents\lottocount\main.as, Line 13, Column 3 1120: Access of undefined property stage.

If you access the focus object, you'll have a reference to your currently active (focused on) object.
But in reality, just having a TextField that allows input will open a keyboard view (only on mobile) on touching it.
You can also tell Flash not to automatically show the keyboard, if you plan to use your own, then you can use the focus or make your own system that would track the appropriate TextField depending on where the last touch was.
EDIT:
Here's a simplistic example of how it could be done.
In this example I have two TextFields: tf1 and tf2. Also I've added a button for the sake of covering a situation where you need to save the last focus on a concrete type of object. The buttons name is myButton.
var lastSelectedTextField:TextField; // this will hold our last selected TextField
// make sure stage exists. If you're writing script in a frame, don't mind, if you use OOP approach you can do so by adding an eventListener for the event ADDED_TO_STAGE
stage.addEventListener(FocusEvent.FOCUS_IN, onFocusIn); // onFocusIn will trigger every time a focus is changed.
myButton.addEventListener(MouseEvent.CLICK, onMyButtonClick);
function onFocusIn(e:FocusEvent):void
{
if (stage.focus is TextField)
lastSelectedTextField = stage.focus as TextField;
}
function onMyButtonClick(e:MouseEvent):void
{
trace("Text of the last selected text field is:", lastSelectedTextField.text);
}

Related

Recompose in Android Compose

I was implementing the outlinedTextField in android using the new compose library. But strangely the input data was not updated in the text field.
So I searched and found a topic called Recomposition in android compose. I didn't get it completely.
However, I did find the solution:
#Composable
fun HelloContent(){
var name:String by remember {mutableStateOf("")}
OutlinedTextField(
value = name,
onValueChange = {name = it},
label = {Text("Name")}
)
}
and I also read on the concept of State in jetpack compose. But I wasn't able to get it completely.
Can someone explain it in simple words?
Basically, recomposition is just an event in Compose, in which the Composable in concern is re-executed. In declarative coding, which is what Compose is based on, we write UI as functions (or methods, more commonly). Now, a recomposition is basically an event in which the UI is re-emitted, by executing the body of the said Composable "function" all over again. This is what recomposition is, at its core. Now on to when it is triggered.
Ok, so in order to trigger recompositions, we need a special type of variable. This type is built into compose and was specifically designed to let it know when to recompose. And the mentioned type is MutableState. As the name suggests, it is State, that can Mutate, i.e., change; vary.
So, we have a variable of type MutableState, what's next? Guess what, you DON'T have a variable of type MutableState because I didn't teach you how to create one! The most common assignment you will use in Compose is the mutableStateOf helper. This is a pre-defined method that returns a value of type MutableState, well, MutableState<T>, actually. T is the type of State here, see below
var a = mutableStateOf(999)
Above, as you can see, 999 is an Int, and so, mutableStateOf here will return a MutableState<Int> type value. Easy enough.
Now, we have a MutableState<Int> value, but honestly, that's kinda ugly. Every time you need to get the value out of the MutableState<T>, you would need to refer to a property conveniently named .value.
So, to get the 999 out of the above var a, you would need to call a.value. Now, this is fine for use at one or two places but calling this every time seems like a mess. That is where Kotlin Property Delegation Come In (I did not need to Capitalize the last two words, I know). We use the by keyword to retrieve the value out of the state, and assign that to our variable - That's all you should care about.
So, var a by mutableStateOf(999) will actually return 999 of type Int, and not of type MutableState<Int>, but the brilliant part is that Compose will still know that the variable a is a State-Holder. So basically mutableStateOf can be thought of as a registering-counter, which you just need to pass through once, in order to get registered in the list of State-Holders. As of when, a recomposition will trigger every time the value of one of the state-holders is changed. This is the rough idea, but let's get technical; Now on to the "how" of recomposition.
To trigger a recomposition, all you need to ensure is two things:
The Composable should be reading a variable, that is also a state-holder
The state-holder should experience a change in its current value
Everything's better with Perry Examples:-
var a by mutableStateOf(999)
Case 1: A Composable receives a as a parameter value, MyComposable(a), then I run a = 0,
Outcome 1: Recomposition Triggered
Case II: This declaration of variable a is actually inside a Composable itself, then I run a = 12344
Outcome II: Recomposition Triggered
Case III: I repeat cases 1 & II, but with a different variable, as follows: var b = 999
Outcome III: No Recompositions Triggered; Reason: b is not a state-holder
Great, we got the basics down now. So, this is the last phase of this lecture.
REMEMBER!!
You see when I say during recomposition, the entire Composable is re-executed, I mean the entire Composable is re-executed, that is, every single line and every single assignment, without exceptions. You see anything wrong with this yet? Lemme demonstrate
Let's say I want to have a Text Composable that displays a number, and increases that number when I click on it.
I could implement something as simple as this
#Composable
fun CountingText(){
var n = mutableStateOf(0) //Starts at 0
Text(
value = n.toString(), //The Composable only accepts strings, while n is of Int type
modifier = Modifier
.clickable { n++ }
)
}
Ok so this is the implementation that we might think would work. If you are unfamiliar with Modifiers, just leave that for now and trust me that it just triggers the code inside the clickable braces, when you actually click on the Text. Now, let's picture how this will be executed.
Firstly Compose will register the variable n as a state-holder. Then it will render the Text Composable with the initial value 0 of n.
Now, the Text is actually clicked. The block inside clicakble will be executed, which in this case is just n++, which will update the value of n. Compose sees that the value of n is updated, and runs through the list of state-holders. Compose finds that n is indeed a state-holder, and then decides to trigger a recomposition. Now, the entire Composable reading the value of n will be recomposed. In this case, that Composable is CountingText since a Text inside it is reading the value of n (To display it). Hence, CountingText will be "re-executed". Let's walk through the re-execution here.
First line in the Composable,
var n = mutableStateOf(0)
n became 0.
Next lines:-
Text(
value = n.toString(), //Just displays 0
modifier = Modifier
.clickable { n++ } //Just tells it to increase n upon click
)
So you see, the catch here is that upon re-execution, n is completely created from scratch as if it never existed before. It was removed from the Composable's memory. To counter this, we need the Composable to remember n. That way, Compose knows that this is a state-holder AND holds a value that needs to be re-assigned to it upon recomposition. So, here's the updated first line (the rest is the same, just the initialization is updated)
var n by remember { mutableStateOf(0) }
Now, upon first execution, n will receive 0, since it is actually the very first time n is created. Thanks to remember, n now has access to the Composable's memory, and thus will be stored in the memory for future usage.
So, during recomposition, this is what happens - When the executor (???) reaches the line where n is assigned,
var n by remember { mutableStateOf(0) }
remember actually acts as a gatekeeper, and does not allow the executor to enter the block contained in it. Instead, it passes it the previously remembered value and asks it to move on. Since when the user clicked the Text, it already incremented the value of n to 1, this was retained in memory and so, now this works as expected.
This is the same case for your TextField problem. The field initially reads an empty value and the value is updated every time the user types a letter, triggering a recomposition and finally displaying the correct value on the screen.
Could it get simpler enough? Let me know I spent half an hour typing this.
Recomposition is used in Compose to recompose (reload the parts that changed) the screen. In your example you have user input which changes the state of the screen. You have to use var name:String by remember {mutableStateOf("")}, by the way, you can leave the :String out because you set the type here: mutableStateOf("") anyway, you need to use remember that the composable remembers the old content and can then add the new content.
If you type in h e l l o everytime you type one letter it recomposes, it remembers the value h, then he, then hel and so on.
Try this
I know there is not difference but leaving imports
Compose_version 1.0.4
import androidx.compose.material.OutlinedTextField
import androidx.compose.material.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.setValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
#Composable
fun YourAnswer() {
var text by remember { mutableStateOf("") }
OutlinedTextField(
value = text,
onValueChange = { text = it },
label = { Text("Name") }
)
}

How to Repaint FMX Android components in Form in C++Builder 10.4?

I wrote a very simple FMX Adroid App, the function is:
Show Form 2 then write something to record(include title and detail text),
close Form 2 to Main Form, then make a checkbox in Main Form with the title we just recorded in Form 2.
if user check the checkbox, then press "del" buttn then delete the record file and checkbox.
the problem is:
when closed Form 2 and in MainForm::OnActivate we can add a new checkbox for the record.
if we checked checkbox then clicked delete, free the pointer of checked checkbox, the checkbox still in main form until I reopen the APP.
I tried:
Invalidate();
Application->ProcessMessages();
BeginUpdate();
EndUpdate();
Still can't work
does anyone know what's going on ? why FMX TForm member has no "Repaint()" or "Update()" "Refresh()" ? just like VCL has.
If you want your TCheckBox* (or any other control) disappear from a Form, you need to set its Parent property to nullptr before deleting it. If you created your control in runtime using new please remember to call delete.
//init
TCheckBox* checkBox = new TCheckBox(Form2);
//delete
checkBox->Parent = nullptr;
delete checkBox;
Answering the second part of your question, you can call Invalidate() function to repaint your whole Form (but first see first part of this answer). But I think it will run properly without calling this function.
Your controls have Repaint() member and it may be better to call them instead, ie. if your checkbox was placed in TPanel*, repainting only this panel is better idea than repainting whole form.

QML ListView indexAt() function always returns wrong value

We are developing QT cross platform application. We have one conversation ListView(Basically list of chats using qml ListView) and requirement is to perform some functionality depending on the first visible index in screen. to find first Visible index we are using function indexAt(real, real). Function call always returns -1.
Documentation which we refereed can be found here QML ListView
Here is sample code snippet
ListView
{
id: id_conversation_list
anchors.fill: parent
model: Conversations
spacing: 5
delegate: id_loader
clip: true
onContentYChanged:
{
console.log("====index===== " + id_conversation_list.indexAt(0, contentY));
}
}
Even i tested with fixed values like (0, 0) or (10, 50) but function always returns -1. If anybody already faced same issue please guide us. Is there any other possible way to find first visible index in qml Listview.

Android : How to get the dirty(changed) properties

How to get the dirty(changed) properties(any controls subclasses of view) amongst a number of properties on a layout. Does android has a dirty flag to mark if the layout has any field that has a changed content??
Actually, i have a layout that has a few edittexts,spinners,datewidgets.Now doing a save should make request to the server only if the user has modified anything .I wanted to know that how can i check if the user has changed anything in that layout or not.Does android has any flag or something that sets itself when the user modifies any input control?
Hmm..Blckberry Does have isDirty(){boolean net.rim.device.api.ui.Manager.isDirty()}method.
The activity is not tightly coupled to the elements in your layout, so you'll have to do this yourself. You could maintain a Map where the key is the id of the layout element, and the value is a boolean that signals if the element has been modified by the user. You would probably need to set up listeners on each element (such as OnKeyListener for your EditTexts) and additionally capture their initial values.
Does android has a dirty flag to mark
if the layout has any field that has a
changed content??
No, sorry.
Bit late with my answer, but the way I do it is to store the form (activity) fields in a container object (for validation, etc). This container object implements the
java.lang.Comparable interface, where T is the same class as the container.
The compareTo(T) method then returns
0 if both objects are equal (thereby form contents haven't changed).
-1 indicates that something has changed and therefore the data is dirty
You can always return other numeric values to indicate what exactly has changed if required.

How do I use InputType=numberDecimal with the "phone" soft keypad?

For an EditText box, the user should only be entering valid numbers, so I am using android:inputType="numberDecimal". Unfortunately, the soft keyboard that Android brings up has numbers only along the top row, while the next three rows have various other symbols (dollar sign, percent sign, exclamation mark, space, etc). Since the numberDecimal only accepts numbers 0-9, negative sign, and decimal point, it would make more sense to use the "phone" soft keyboard (0-9 in a 3x3 grid, plus some other symbols). This would make the buttons larger and easier to hit (since it's a 4x4 grid rather than a 10x4 grid in the same screen area). Unfortunately, using android:inputType="phone" allows non-numeric characters such as parentheses
I have attempted to use android:inputType="numberDecimal|phone", but the numberDecimal aspect of the bit flag seems to be ignored. I have also tried using android:inputType="phone" in combination with android:digits="0123456789-.", but that still allows multiple negative signs or decimal points (inputType="number" has really good error checking for things like that, and won't let the user even type it in). I have also tried using android:inputType="phone" in the xml layout file, while using a DigitsKeyListener in the java code, but then that just uses the default number soft keyboard (the one that has numbers only along top row) (it appears to set InputType.TYPE_CLASS_NUMBER, which voids the InputType.TYPE_CLASS_PHONE set by the xml layout).
Writing a custom IME wouldn't work, since the user would have to select the IME as a global option outside the app.
Is there any way to use the "phone" style soft keyboard while also using the "number" restrictions on what is entered?
I had the same problem. This works for me:
<item name="android:inputType">numberDecimal</item>
<item name="android:digits">0123456789.</item>
Hopefully this helps you.
So far, what I've decided to do is extend the DigitsKeyListener and override getInputType() so that it will return InputType.TYPE_CLASS_PHONE. This allows me to use the handy filter() in DigitsKeyListener, but at the same time use the TYPE_CLASS_PHONE soft keyboard. I'm new to Android programming, but this appears to work without breaking anything. Code is something like this:
import android.text.method.DigitsKeyListener;
import android.text.InputType;
public class CustomDigitsKeyListener extends DigitsKeyListener
{
public CustomDigitsKeyListener() {
super(false, false);
}
public CustomDigitsKeyListener(boolean sign, boolean decimal) {
super(sign, decimal);
}
public int getInputType() {
return InputType.TYPE_CLASS_PHONE;
}
}
Is there anything wrong in doing this (switching the return of getInputType() to something that the superclass didn't intend)?
Adam Dunn code works perfect, but didnt show how to implement the class
import android.text.method.DigitsKeyListener;
import android.text.InputType;
public class CustomDigitsKeyListener extends DigitsKeyListener
{
public CustomDigitsKeyListener() {
super(false, false);
}
public CustomDigitsKeyListener(boolean sign, boolean decimal) {
super(sign, decimal);
}
public int getInputType() {
return InputType.TYPE_CLASS_PHONE;
}
}
then you have to instance like this
MyTextView.setKeyListener(new CustomDigitsKeyListener(true,true));
android:inputType="phone"
android:digits="0123456789."
Add this to your EditText xml layout.
This will show phone layout, but only allow user to tap digits specified.
User tapping other characters will be ignored.
Try using android:numeric="integer" in your EditText view.
You just use the phone keyboard and check the input by yourself. It isn't a very big condition to test if the input is a valid digit between 0 and 9.

Categories

Resources