Asked  6 Months ago    Answers:  5   Viewed   16 times

I am learning how to create UI elements. I have created a few EditText input fields. On the click of a Button I want to capture the content typed into that input field.

<EditText android:id="@+id/name" android:width="220px" />

That's my field. How can I get the content?

 Answers

90

By using getText():

Button   mButton;
EditText mEdit;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    mButton = (Button)findViewById(R.id.button);
    mEdit   = (EditText)findViewById(R.id.edittext);

    mButton.setOnClickListener(
        new View.OnClickListener()
        {
            public void onClick(View view)
            {
                Log.v("EditText", mEdit.getText().toString());
            }
        });
}
Tuesday, June 1, 2021
 
Rudie
answered 6 Months ago
56

For theWC()->customer->billing_country_zone:

  • First since Woocommerce 3, properties can be accessed on most all Woocommerce instances objects.
  • And "billing_country_zone" is no a default property of WC_Customer instance object.

As it's about checkout fields, instead you should use $checkout argument which is the instance of the WC_Checkout Object. Then there is the appropriated method get_value() to be used on it...

What is that for?
Once the customer has submitted at least one order, the selected value for "billing_country_zone" will be displayed on checkout page.

So you will have to replace the line:

), WC()->customer->billing_country_zone );

by this one:

), $checkout->get_value('billing_country_zone') );

If $checkout variable argument is not defined, you will use WC()->checkout like:

), WC()->checkout->get_value('billing_country_zone') );

Now when you will save this custom checkout field value, you will need to save it:

  1. As order meta data
  2. And also as User meta data

So Here is the complete code (commented):

// Display custom checkout field
add_action( 'woocommerce_before_order_notes', 'display_custom_checkout_field' );
function display_custom_checkout_field( $checkout ) {
    $options = array_merge( [ '' => __( 'Nothing to select' ), ], city_zone() );
    woocommerce_form_field( 'billing_country_zone', array(
            'type'     => 'select',
            'class'    => array( 'form-row-wide', 'address-field', 'update_totals_on_change' ),
            'label'    => __( 'City zone' ),
            'required' => true,
            'options'  => $options
    ), $checkout->get_value('billing_country_zone') );
}

// custom checkout field validation
add_action( 'woocommerce_checkout_process', 'custom_checkout_field_validation' );
function custom_checkout_field_validation() {
    if ( isset( $_POST['billing_country_zone'] ) && empty( $_POST['billing_country_zone'] ) )
        wc_add_notice( __( 'Please select a <strong>"City zone"</strong>.', 'woocommerce' ), 'error' );
}

// Save custom checkout field value as custom order meta data and user meta data too
add_action( 'woocommerce_checkout_create_order', 'custom_checkout_field_update_order_meta', 20, 2 );
function custom_checkout_field_update_order_meta( $order, $data ) {
    if ( isset( $_POST['billing_country_zone'] ) ) {
        // Save custom checkout field value
        $order->update_meta_data( '_billing_country_zone', esc_attr( $_POST['billing_country_zone'] ) );

        // Save the custom checkout field value as user meta data
        if( $order->get_customer_id() )
            update_user_meta( $order->get_customer_id(), 'billing_country_zone', esc_attr( $_POST['billing_country_zone'] ) );
    }
}

Code goes in function.php file of your active child theme (or active theme). tested and works.

Wednesday, March 31, 2021
 
Gigamegs
answered 9 Months ago
54

Actually the hint moves to the parent view TextInputLayout that surrounds the EditText view:

You can get the hint like this:

android.support.design.widget.TextInputLayout parent = (android.support.design.widget.TextInputLayout) yourEditText.getParent();
String hint = parent.getHint().toString();

And if you want to add * make it like this:

parent.setHint(parent.getHint() + "*");

Happy codding! :)

Wednesday, August 4, 2021
 
Juriy
answered 4 Months ago
66

You are looking for TextWatcher:

    youredittext.addTextChangedListener(new TextWatcher()
    {
        @Override
        public void afterTextChanged(Editable mEdit) 
        {
            text = mEdit.toString();
        }

        public void beforeTextChanged(CharSequence s, int start, int count, int after){}

        public void onTextChanged(CharSequence s, int start, int before, int count){}
    });
Saturday, October 9, 2021
 
freeMagee
answered 2 Months ago
24

this code picks textbox1 from tableLayoutPanel1, cast it from Control to TextBox and takes Text property:

string s = ((TextBox)tableLayoutPanel1.Controls["TxtBox1"]).Text;

if you need them all, then iterate over textboxes:

string[] t = new string[4];
for(int i=0; i<4; i++)
    t[i] = ((TextBox)tableLayoutPanel1.Controls["TxtBox"+(i+1).ToString()]).Text;
Wednesday, November 24, 2021
 
Gili
answered 5 Days ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :  
Share