我创建了一个自定义EditText
。现在的问题是,当用户点击按钮时,我不知道如何从EditText
获取数据。
这是Login.xml代码
<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" />.
这是我的java代码
@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);
}
}
});
这是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);
}
}
答案 0 :(得分:1)
MyCustomEditText _et = (MyCustomEditText)findviewbyid(R.id.etmob);
String _enteredvalue = _et.gettext.tostring();
sysyem.out.println(_enteredvalue);
答案 1 :(得分:0)
请尝试
MyCustomEditText editText = (MyCustomEditText) findViewById(R.id.etmob);
String mobileno = editText.getText().toString();
if(mobileno.equalsIgnoreCase("1234567890"))
{
...
}
<强>码强>
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);
}
}
});
答案 2 :(得分:0)
问题是,您没有初始化自己的自定义Edittext
,而是使用Android默认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);
}
}
});
答案 3 :(得分:0)
试试此代码
MyCustomEditText editText = (MyCustomEditText)findViewById(id of the view);
String value = editText.getText().toString();
if(value=="1234567890"){
//ToDo something
}else{
//ToDo something
}