我正在使用Android上的信用卡/借记卡表单。
我想要实现的目标:
我有一个名为EditText
的{{1}}字段,用户应输入他/她的卡号。在收到卡号的所有数字后,另外两个CardNumber
字段EditText
& ExpiryDate
滑出去了。如果用户删除CVV
字段中的任何数字,请CardNumber
& ExpiryDate
字段应消失或滑入&删除写在他们身上的文字。
当用户再次输入CVV
时输入所需的所有数字,CardNumber
和& ExpiryDate
应该再次出现在之前出现的同一个地方。
但这就是我得到的
他们没有出现在同一个地方。我如何实现这一目标?如何从CVV
&中删除文本ExpiryDate
他们失踪后。{/ p>
这是我到目前为止所做的工作
MainActivity.java
CVV
content_main.xml
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.KeyEvent;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.EditText;
import java.security.Key;
public class MainActivity extends AppCompatActivity {
private EditText cardNumber, expiryDate, CVV, nameOnCard;
private boolean flag = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LOCKED);//Locks the screen orientation
init(); //Initializes the variables
typefunc();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public void init()
{
cardNumber = (EditText) findViewById(R.id.cardNumber); //Field for storing the card number
expiryDate = (EditText) findViewById(R.id.expiryDate); //For storing the Expiry Date
CVV = (EditText) findViewById(R.id.CVV); //For storing the CVV
nameOnCard = (EditText) findViewById(R.id.nameOnCard); //For storing the name of the Cardholder
}
public void typefunc()
{
//FORMATTING CARD NUMBER FIELD
cardNumber.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after)
{
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count)
{
if(s.length()==19)
{
//MAKING EXPIRY DATE VISIBLE & PUTTING FOCUS ON IT
expiryDate.setVisibility(View.VISIBLE);
expiryDate.requestFocus();
//SLIDE OUT ANIMATION (FADE IN --> FADE OUT)
expiryDate.setAlpha(0.0f);//(INVISIBLE)
expiryDate.animate().translationY(expiryDate.getHeight()).alpha(1.0f);//(VISIBLE)
//MAKING CVV VISIBLE
CVV.setVisibility(View.VISIBLE);
//SLIDE OUT ANIMATION (FADE IN --> FADE OUT)
CVV.setAlpha(0.0f); //(INVISIBLE)
CVV.animate().translationY(CVV.getHeight()).alpha(1.0f); //(VISIBLE)
}
if(s.length()<19)
{
//DISAPPEARING THE EXPIRY DATE 7 CVV
expiryDate.setVisibility(View.GONE);
CVV.setVisibility(View.GONE);
//CLEARING THE FOCUS
expiryDate.clearFocus();
//SLIDE IN ANIMATION (FADE OUT --> FADE IN)
expiryDate.animate().translationY(0).alpha(0.0f);
CVV.animate().translationY(0).alpha(0.0f);
}
}
@Override
public void afterTextChanged(Editable s)
{
char space = ' ';
if(s.length()>0 && (s.length()%5) == 0)
{
char c = s.charAt(s.length()-1);
if(Character.isDigit(c))
{
s.insert(s.length()-1,String.valueOf(space));
}
}
}
});
//FORMATTING THE EXPIRY DATE FIELD
expiryDate.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count)
{
if(s.length()==5)
CVV.requestFocus();
}
@Override
public void afterTextChanged(Editable s)
{
char slash = '/';
if(s.length()>0 && (s.length()%3) == 0)
{
char c = s.charAt(s.length()-1);
if(Character.isDigit(c))
s.insert(s.length()-1,String.valueOf(slash));
}
}
});
CVV.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count)
{
if(s.length()==3) {
nameOnCard.setVisibility(View.VISIBLE);
}
}
@Override
public void afterTextChanged(Editable s)
{
if(s.length()==3)
nameOnCard.requestFocus();
}
});
}
}
我错过了什么?
感谢您的时间!!
答案 0 :(得分:1)
你可能不想翻译EditText,因为这就是为什么它放在屏幕上比想要的低。
如果你想要使用翻译动画,你必须使用View.INVISIBLE而不是View.GONE,否则初始的getHeight()将返回0.你还需要为s.length()==翻译为0 19和-expiryDate.getHeight()用于s.length()&lt; 19
if(s.length()==19)
{
//MAKING EXPIRY DATE VISIBLE & PUTTING FOCUS ON IT
expiryDate.setVisibility(View.VISIBLE);
expiryDate.requestFocus();
//SLIDE OUT ANIMATION (FADE IN --> FADE OUT)
expiryDate.setAlpha(0.0f);//(INVISIBLE)
expiryDate.animate().translationY(0).alpha(1.0f);//(VISIBLE)
//MAKING CVV VISIBLE
CVV.setVisibility(View.VISIBLE);
//SLIDE OUT ANIMATION (FADE IN --> FADE OUT)
CVV.setAlpha(0.0f); //(INVISIBLE)
CVV.animate().translationY(0).alpha(1.0f); //(VISIBLE)
}
else if(s.length()<19 && expiryDate.getVisibility() == View.VISIBLE)
{
//DISAPPEARING THE EXPIRY DATE 7 CVV
expiryDate.setVisibility(View.INVISIBLE);
CVV.setVisibility(View.INVISIBLE);
//CLEARING THE FOCUS
expiryDate.clearFocus();
//SLIDE IN ANIMATION (FADE OUT --> FADE IN)
expiryDate.animate().translationY(-expiryDate.getHeight()).alpha(0.0f);
CVV.animate().translationY(-CVV.getHeight()).alpha(0.0f);
}
此外,您还希望在init方法中翻译视图,如此
expiryDate.getViewTreeObserver().addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
@Override
public boolean onPreDraw() {
expiryDate.animate().translationY(-expiryDate.getHeight());
expiryDate.getViewTreeObserver().removeOnPreDrawListener(this);
return true;
}
});
答案 1 :(得分:0)
尝试使用View.INVISIBLE
代替View.GONE
。
View.GONE此视图不可见,并且不需要任何空间用于布局。
View.INVISIBLE此视图不可见,但仍占用空间 布局目的。
同时将视图XML从android:visibility="gone"
更新为android:visibility="invisible"
,看看它是否有效。
答案 2 :(得分:0)
你从Editext.getHeight()获得高度。未绘制视图时,它返回零。所以第二次得到一些xx值。所以只有第二次你的幻灯片动画才会发生。
如果滑动动画不是您的首选项,请对视图组使用animatelayoutchanges = true。它将为一些标准动画制作动画。那会更好。
或者你可以从onPredrawListener获得高度。
由于