Example usage for android.widget EditText setSelection

List of usage examples for android.widget EditText setSelection

Introduction

In this page you can find the example usage for android.widget EditText setSelection.

Prototype

public void setSelection(int index) 

Source Link

Document

Convenience for Selection#setSelection(Spannable,int) .

Usage

From source file:com.KittleApps.app.TransCalc.EventListener.java

@Override
public void onClick(View view) {
    View v;/* w  w  w.  java 2 s . c o m*/
    EditText active;
    int id = view.getId();
    switch (id) {
    case R.id.del:
        mHandler.onDelete();
        break;

    case R.id.clear:
        mHandler.onClear();
        break;

    case R.id.equal:
        if (mHandler.getText().contains(mX) || mHandler.getText().contains(mY)) {
            if (!mHandler.getText().contains("=")) {
                mHandler.insert("=");
                returnToBasic();
            }
            break;
        }
        mHandler.onEnter();
        break;

    case R.id.hex:
        mHandler.setText(mHandler.mBaseModule.setMode(Mode.HEXADECIMAL));
        view.setBackgroundResource(R.color.pressed_color);
        ((View) view.getParent()).findViewById(R.id.bin).setBackgroundResource(R.drawable.btn_function);
        ((View) view.getParent()).findViewById(R.id.dec).setBackgroundResource(R.drawable.btn_function);
        for (int i : mHandler.mBaseModule.bannedResourceInBinary) {
            if (mPager != null) {
                v = mPager.findViewById(i);
            } else {
                v = mSmallPager.findViewById(i);
                if (v == null)
                    v = mLargePager.findViewById(i);
            }
            v.setEnabled(true);
        }
        break;

    case R.id.bin:
        mHandler.setText(mHandler.mBaseModule.setMode(Mode.BINARY));
        view.setBackgroundResource(R.color.pressed_color);
        ((View) view.getParent()).findViewById(R.id.hex).setBackgroundResource(R.drawable.btn_function);
        ((View) view.getParent()).findViewById(R.id.dec).setBackgroundResource(R.drawable.btn_function);
        for (int i : mHandler.mBaseModule.bannedResourceInBinary) {
            if (mPager != null) {
                v = mPager.findViewById(i);
            } else {
                v = mSmallPager.findViewById(i);
                if (v == null)
                    v = mLargePager.findViewById(i);
            }
            v.setEnabled(false);
        }
        break;

    case R.id.dec:
        mHandler.setText(mHandler.mBaseModule.setMode(Mode.DECIMAL));
        view.setBackgroundResource(R.color.pressed_color);
        ((View) view.getParent()).findViewById(R.id.bin).setBackgroundResource(R.drawable.btn_function);
        ((View) view.getParent()).findViewById(R.id.hex).setBackgroundResource(R.drawable.btn_function);
        for (int i : mHandler.mBaseModule.bannedResourceInBinary) {
            if (mPager != null) {
                v = mPager.findViewById(i);
            } else {
                v = mSmallPager.findViewById(i);
                if (v == null)
                    v = mLargePager.findViewById(i);
            }
            v.setEnabled(true);
        }
        for (int i : mHandler.mBaseModule.bannedResourceInDecimal) {
            if (mPager != null) {
                v = mPager.findViewById(i);
            } else {
                v = mSmallPager.findViewById(i);
                if (v == null)
                    v = mLargePager.findViewById(i);
            }
            v.setEnabled(false);
        }
        break;

    case R.id.matrix:
        mHandler.insert(MatrixView.PATTERN);
        returnToBasic();
        break;

    case R.id.matrix_inverse:
        mHandler.insert(MatrixInverseView.PATTERN);
        returnToBasic();
        break;

    case R.id.matrix_transpose:
        mHandler.insert(MatrixTransposeView.PATTERN);
        returnToBasic();
        break;

    case R.id.plus_row:
        v = mHandler.mDisplay.getActiveEditText();
        if (v instanceof MatrixEditText)
            ((MatrixEditText) v).getMatrixView().addRow();
        break;

    case R.id.minus_row:
        v = mHandler.mDisplay.getActiveEditText();
        if (v instanceof MatrixEditText)
            ((MatrixEditText) v).getMatrixView().removeRow();
        break;

    case R.id.plus_col:
        v = mHandler.mDisplay.getActiveEditText();
        if (v instanceof MatrixEditText)
            ((MatrixEditText) v).getMatrixView().addColumn();
        break;

    case R.id.minus_col:
        v = mHandler.mDisplay.getActiveEditText();
        if (v instanceof MatrixEditText)
            ((MatrixEditText) v).getMatrixView().removeColumn();
        break;

    case R.id.next:
        active = mHandler.mDisplay.getActiveEditText();
        if (active.getSelectionStart() == active.getText().length()) {
            v = mHandler.mDisplay.getActiveEditText().focusSearch(View.FOCUS_FORWARD);
            if (v != null)
                v.requestFocus();
            active = mHandler.mDisplay.getActiveEditText();
            active.setSelection(0);
        } else {
            active.setSelection(active.getSelectionStart() + 1);
        }
        break;

    case R.id.sign:
        active = mHandler.mDisplay.getActiveEditText();
        int selection = active.getSelectionStart();
        if (active.getText().toString().matches(Logic.NUMBER)) {
            if (active.getText().toString().startsWith(String.valueOf(Logic.MINUS))) {
                active.setText(active.getText().toString().substring(1));
                selection--;
            } else {
                active.setText(Logic.MINUS + active.getText().toString());
                selection++;
            }
            if (selection > active.length())
                selection--;
            if (selection < 0)
                selection = 0;
            active.setSelection(selection);
        }
        break;

    case R.id.parentheses:
        if (mHandler.getText().equals(mErrorString))
            mHandler.setText("");
        if (mHandler.getText().contains("=")) {
            String[] equation = mHandler.getText().split("=");
            if (equation.length > 1) {
                mHandler.setText(equation[0] + "=(" + equation[1] + ")");
            } else {
                mHandler.setText(equation[0] + "=()");
            }
        } else {
            mHandler.setText("(" + mHandler.getText() + ")");
        }
        returnToBasic();
        break;

    case R.id.mod:
        if (mHandler.getText().equals(mErrorString))
            mHandler.setText("");
        if (mHandler.getText().contains("=")) {
            String[] equation = mHandler.getText().split("=");
            if (equation.length > 1) {
                mHandler.setText(equation[0] + "=" + mModString + "(" + equation[1] + ",");
            } else {
                mHandler.insert(mModString + "(");
            }
        } else {
            if (mHandler.getText().length() > 0) {
                mHandler.setText(mModString + "(" + mHandler.getText() + ",");
            } else {
                mHandler.insert(mModString + "(");
            }
        }
        returnToBasic();
        break;

    case R.id.easter:
        Toast.makeText(mContext, R.string.easter_egg, Toast.LENGTH_SHORT).show();
        break;

    default:
        if (view instanceof Button) {
            if (mHandler.getText().equals(mErrorString))
                mHandler.setText("");
            String text = ((Button) view).getText().toString();
            if (text.equals(mDX) || text.equals(mDY)) {
                // Do nothing
            } else if (text.length() >= 2) {
                // Add paren after sin, cos, ln, etc. from buttons
                text += "(";
            }
            mHandler.insert(text);
            returnToBasic();
        }
    }
}

From source file:com.sentaroh.android.SMBExplorer.SMBExplorerMain.java

public void scanRemoteNetworkDlg(final NotifyEvent p_ntfy, String port_number) {
    //??/*from   w ww  .ja  v  a 2s .  c  o m*/
    final Dialog dialog = new Dialog(mContext);
    dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
    dialog.setCanceledOnTouchOutside(false);
    dialog.setContentView(R.layout.scan_remote_ntwk_dlg);
    final Button btn_scan = (Button) dialog.findViewById(R.id.scan_remote_ntwk_btn_ok);
    final Button btn_cancel = (Button) dialog.findViewById(R.id.scan_remote_ntwk_btn_cancel);
    final TextView tvmsg = (TextView) dialog.findViewById(R.id.scan_remote_ntwk_msg);
    final TextView tv_result = (TextView) dialog.findViewById(R.id.scan_remote_ntwk_scan_result_title);
    tvmsg.setText(mContext.getString(R.string.msgs_scan_ip_address_press_scan_btn));
    tv_result.setVisibility(TextView.GONE);

    final String from = getLocalIpAddress();
    String subnet = from.substring(0, from.lastIndexOf("."));
    String subnet_o1, subnet_o2, subnet_o3;
    subnet_o1 = subnet.substring(0, subnet.indexOf("."));
    subnet_o2 = subnet.substring(subnet.indexOf(".") + 1, subnet.lastIndexOf("."));
    subnet_o3 = subnet.substring(subnet.lastIndexOf(".") + 1, subnet.length());
    final EditText baEt1 = (EditText) dialog.findViewById(R.id.scan_remote_ntwk_begin_address_o1);
    final EditText baEt2 = (EditText) dialog.findViewById(R.id.scan_remote_ntwk_begin_address_o2);
    final EditText baEt3 = (EditText) dialog.findViewById(R.id.scan_remote_ntwk_begin_address_o3);
    final EditText baEt4 = (EditText) dialog.findViewById(R.id.scan_remote_ntwk_begin_address_o4);
    final EditText eaEt4 = (EditText) dialog.findViewById(R.id.scan_remote_ntwk_end_address_o4);
    baEt1.setText(subnet_o1);
    baEt2.setText(subnet_o2);
    baEt3.setText(subnet_o3);
    baEt4.setText("1");
    baEt4.setSelection(1);
    eaEt4.setText("254");
    baEt4.requestFocus();

    final CheckBox cb_use_port_number = (CheckBox) dialog.findViewById(R.id.scan_remote_ntwk_use_port);
    final EditText et_port_number = (EditText) dialog.findViewById(R.id.scan_remote_ntwk_port_number);

    CommonDialog.setDlgBoxSizeLimit(dialog, true);

    if (port_number.equals("")) {
        et_port_number.setEnabled(false);
        cb_use_port_number.setChecked(false);
    } else {
        et_port_number.setEnabled(true);
        et_port_number.setText(port_number);
        cb_use_port_number.setChecked(true);
    }
    cb_use_port_number.setOnCheckedChangeListener(new OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            et_port_number.setEnabled(isChecked);
        }
    });

    final NotifyEvent ntfy_lv_click = new NotifyEvent(mContext);
    ntfy_lv_click.setListener(new NotifyEventListener() {
        @Override
        public void positiveResponse(Context c, Object[] o) {
            dialog.dismiss();
            p_ntfy.notifyToListener(true, o);
        }

        @Override
        public void negativeResponse(Context c, Object[] o) {
        }
    });

    final ArrayList<ScanAddressResultListItem> ipAddressList = new ArrayList<ScanAddressResultListItem>();
    //      ScanAddressResultListItem li=new ScanAddressResultListItem();
    //      li.server_name=mContext.getString(R.string.msgs_ip_address_no_address);
    //      ipAddressList.add(li);
    final ListView lv = (ListView) dialog.findViewById(R.id.scan_remote_ntwk_scan_result_list);
    final AdapterScanAddressResultList adap = new AdapterScanAddressResultList(mContext,
            R.layout.scan_address_result_list_item, ipAddressList, ntfy_lv_click);
    lv.setAdapter(adap);
    lv.setScrollingCacheEnabled(false);
    lv.setScrollbarFadingEnabled(false);

    //SCAN?
    btn_scan.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            ipAddressList.clear();
            NotifyEvent ntfy = new NotifyEvent(mContext);
            ntfy.setListener(new NotifyEventListener() {
                @Override
                public void positiveResponse(Context c, Object[] o) {
                    if (ipAddressList.size() < 1) {
                        tvmsg.setText(mContext.getString(R.string.msgs_scan_ip_address_not_detected));
                        tv_result.setVisibility(TextView.GONE);
                    } else {
                        tvmsg.setText(mContext.getString(R.string.msgs_scan_ip_address_select_detected_host));
                        tv_result.setVisibility(TextView.VISIBLE);
                    }
                    //                   adap.clear();
                    //                   for (int i=0;i<ipAddressList.size();i++) 
                    //                      adap.add(ipAddressList.get(i));
                }

                @Override
                public void negativeResponse(Context c, Object[] o) {
                }

            });
            if (auditScanAddressRangeValue(dialog)) {
                tv_result.setVisibility(TextView.GONE);
                String ba1 = baEt1.getText().toString();
                String ba2 = baEt2.getText().toString();
                String ba3 = baEt3.getText().toString();
                String ba4 = baEt4.getText().toString();
                String ea4 = eaEt4.getText().toString();
                String subnet = ba1 + "." + ba2 + "." + ba3;
                int begin_addr = Integer.parseInt(ba4);
                int end_addr = Integer.parseInt(ea4);
                scanRemoteNetwork(dialog, lv, adap, ipAddressList, subnet, begin_addr, end_addr, ntfy);
            } else {
                //error
            }
        }
    });

    //CANCEL?
    btn_cancel.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            dialog.dismiss();
            p_ntfy.notifyToListener(false, null);
        }
    });
    // Cancel?
    dialog.setOnCancelListener(new Dialog.OnCancelListener() {
        @Override
        public void onCancel(DialogInterface arg0) {
            btn_cancel.performClick();
        }
    });
    dialog.show();

}

From source file:com.mario22gmail.license.nfc_project.FragmentPinDialog.java

@Nullable
@Override/* w ww  .ja va 2s . c o  m*/
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    final View view = inflater.inflate(R.layout.fragment_pin_dialog, container);
    getDialog().requestWindowFeature(STYLE_NO_TITLE);
    final EditText pinTextDialog = (EditText) view.findViewById(R.id.editTextPinDialog);
    final NavigationDrawerActivity mainActivity = (NavigationDrawerActivity) getActivity();

    disableSoftInputFromAppearing(pinTextDialog);

    Button buttonEnter = (Button) view.findViewById(R.id.buttonEnterPinDialog);
    buttonEnter.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            String pinNumberText = pinTextDialog.getText().toString();
            AuthResponse cardResponse = mainActivity.AuthenticateOnDesfire(pinNumberText);
            if (cardResponse.isValid()) {
                Toast.makeText(getContext(), "Autentificat", Toast.LENGTH_SHORT).show();
                getDialog().dismiss();
                Log.i("nfc_debug", "Card is valid");
                Intent goToWebCredentialPage = new Intent("goToWebCredentialPage");
                NavigationDrawerActivity.getAppContext().sendBroadcast(goToWebCredentialPage);
            } else {
                Toast.makeText(getDialog().getContext(), "Pin invalid", Toast.LENGTH_SHORT).show();

            }

        }
    });

    Button cancelDialog = (Button) view.findViewById(R.id.buttonPinDialogCancel);
    cancelDialog.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            getDialog().dismiss();
        }
    });

    Button buttonNumarul1 = (Button) view.findViewById(R.id.buttonNumarul1);
    buttonNumarul1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            int position = pinTextDialog.getSelectionStart();
            pinTextDialog.getText().insert(position, "1");
        }
    });

    Button buttonNumarul2 = (Button) view.findViewById(R.id.buttonNumarul2);
    buttonNumarul2.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            int position = pinTextDialog.getSelectionStart();
            pinTextDialog.getText().insert(position, "2");
        }
    });

    Button buttonNumarul3 = (Button) view.findViewById(R.id.buttonNumarul3);
    buttonNumarul3.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            int position = pinTextDialog.getSelectionStart();
            pinTextDialog.getText().insert(position, "3");
        }
    });

    Button buttonNumarul4 = (Button) view.findViewById(R.id.buttonNumarul4);
    buttonNumarul4.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            int position = pinTextDialog.getSelectionStart();
            pinTextDialog.getText().insert(position, "4");
        }
    });

    Button buttonNumarul5 = (Button) view.findViewById(R.id.buttonNumarul5);
    buttonNumarul5.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            int position = pinTextDialog.getSelectionStart();
            pinTextDialog.getText().insert(position, "5");
        }
    });

    Button buttonNumarul6 = (Button) view.findViewById(R.id.buttonNumarul6);
    buttonNumarul6.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            int position = pinTextDialog.getSelectionStart();
            pinTextDialog.getText().insert(position, "6");
        }
    });

    Button buttonNumarul7 = (Button) view.findViewById(R.id.buttonNumarul7);
    buttonNumarul7.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            int position = pinTextDialog.getSelectionStart();
            pinTextDialog.getText().insert(position, "7");
        }
    });

    Button buttonNumarul8 = (Button) view.findViewById(R.id.buttonNumarul8);
    buttonNumarul8.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            int position = pinTextDialog.getSelectionStart();
            pinTextDialog.getText().insert(position, "8");
        }
    });

    Button buttonNumarul9 = (Button) view.findViewById(R.id.buttonNumarul9);
    buttonNumarul9.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            int position = pinTextDialog.getSelectionStart();
            pinTextDialog.getText().insert(position, "9");
        }
    });

    Button buttonNumarul0 = (Button) view.findViewById(R.id.buttonNumarul0);
    buttonNumarul0.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            int position = pinTextDialog.getSelectionStart();
            pinTextDialog.getText().insert(position, "0");
        }
    });

    ImageButton buttonDeleteLastChar = (ImageButton) view.findViewById(R.id.buttonDeleteLastCharPinDialog);
    buttonDeleteLastChar.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            int position = pinTextDialog.getSelectionStart();
            if (position > 0) {
                Log.i("nfc_debug", "pozitia e " + position);
                pinTextDialog.getText().delete(position - 1, position);
            }

        }
    });

    final Button pinVisibility = (Button) view.findViewById(R.id.pinVisibility);
    pinTextDialog.setInputType(InputType.TYPE_CLASS_NUMBER);
    pinVisibility.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (isPinVisible) {
                int position = pinTextDialog.getSelectionStart();
                pinVisibility.setBackgroundResource(R.drawable.ic_visibility_black_24dp);
                pinTextDialog
                        .setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_VARIATION_PASSWORD);
                pinTextDialog.setTypeface(Typeface.DEFAULT);
                pinTextDialog.setSelection(position);
                isPinVisible = false;
            } else {
                int position = pinTextDialog.getSelectionStart();
                pinVisibility.setBackgroundResource(R.drawable.ic_visibility_off_black_24dp);
                pinTextDialog.setInputType(InputType.TYPE_CLASS_NUMBER);
                pinTextDialog.setSelection(position);
                isPinVisible = true;
            }
        }
    });

    return view;
}