当前位置: 动力学知识库 > 问答 > 编程问答 >

android - Getting data from Custom EditText fields when click on button

问题描述:

I have created a custom EditText . Now the problem is I don't know how to get data from this EditText when user clicks on button.

Here's the Login.xml code

<com.amar.MyCustomEditText

android:id="@+id/etmob"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:ems="10"

android:hint="Enter your Mobile no"

android:phoneNumber="true">

</com.amar.MyCustomEditText>

<Button

android:id="@+id/loginbutton"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_gravity="right"

android:background="@drawable/submit" />.

And this is my java code

@Override

protected void onCreate(Bundle savedInstanceState) {

// TODO Auto-generated method stub

super.onCreate(savedInstanceState);

setContentView(R.layout.login);

Button submit = (Button) findViewById(R.id.loginbutton);

submit.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View view) {

// TODO Auto-generated method stub

EditText et = (EditText) findViewById(R.id.etmob);

String mobileno = et.getText().toString();

if(mobileno == 1234567890)

{

Intent iv = new Intent(getApplicationContext(),Verify.class);

startActivity(iv);

}

}

});

And this is MyCustomEditText.java

 public class MyCustomEditText extends EditText {

private Rect mRect;

private Paint mPaint;

public MyCustomEditText(Context context, AttributeSet attrs) {

super(context, attrs);

mRect = new Rect();

mPaint = new Paint();

// define the style of line

mPaint.setStyle(Paint.Style.FILL_AND_STROKE);

// define the color of line

mPaint.setColor(Color.parseColor("#B1B0B0"));

}

@Override

protected void onDraw(Canvas canvas) {

int height = getHeight();

int lHeight = getLineHeight();

// the number of line

int count = height / lHeight;

if (getLineCount() > count) {

// for long text with scrolling

count = getLineCount();

}

Rect r = mRect;

Paint paint = mPaint;

// first line

int baseline = getLineBounds(0, r);

// draw the remaining lines.

for (int i = 0; i < count; i++) {

canvas.drawLine(r.left, baseline + 10, r.right, baseline + 10,

paint);

// next line

baseline += getLineHeight();

}

super.onDraw(canvas);

}

}

网友答案:

Problem is, you are not initializing your own custom Edittext, you are using Android default Edittext.

submit.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            // TODO Auto-generated method stub
            com.amar.MyCustomEditText et = (com.amar.MyCustomEditText) findViewById(R.id.etmob);
            String mobileno = et.getText().toString();
            if(mobileno.equalsIgnoreCase("1234567890"))
           {
            Intent iv = new Intent(getApplicationContext(),Verify.class);
               startActivity(iv);
           }
             }
    });
网友答案:
MyCustomEditText _et = (MyCustomEditText)findviewbyid(R.id.etmob);
String _enteredvalue = _et.gettext.tostring();
sysyem.out.println(_enteredvalue);
网友答案:

please try with

MyCustomEditText editText = (MyCustomEditText) findViewById(R.id.etmob);
String mobileno = editText.getText().toString();
if(mobileno.equalsIgnoreCase("1234567890"))
{
...
}

code

submit.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            // TODO Auto-generated method stub
            MyCustomEditText et = (MyCustomEditText) findViewById(R.id.etmob);
            String mobileno = et.getText().toString();
            if(mobileno.equalsIgnoreCase("1234567890"))
           {
            Intent iv = new Intent(getApplicationContext(),Verify.class);
               startActivity(iv);
           }
             }
    });   
网友答案:

Try this code

MyCustomEditText editText = (MyCustomEditText)findViewById(id of the view);
String value = editText.getText().toString();
if(value=="1234567890"){
           //ToDo something
 }else{
           //ToDo something
 }
分享给朋友:
您可能感兴趣的文章:
随机阅读: