我正在创建一个带数字键盘的应用。
XML CODE
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/colorPrimaryDark">
<ImageView
android:id="@+id/appIco"
android:layout_width="70dp"
android:layout_height="70dp"
android:layout_marginStart="74dp"
android:layout_marginTop="79dp"
android:contentDescription="app image"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="@android:color/transparent" />
<TextView
android:id="@+id/appName"
android:layout_width="71dp"
android:layout_height="18dp"
android:layout_marginStart="74dp"
android:layout_marginTop="6dp"
android:text="TextView"
android:shadowColor="@android:color/black"
android:textAlignment="center"
android:textColor="@color/white"
android:textSize="16sp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toBottomOf="@+id/appIco" />
<EditText
android:id="@+id/pinField"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="266dp"
android:layout_marginEnd="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginStart="8dp"
android:backgroundTint="@android:color/transparent"
android:ems="10"
android:inputType="numberPassword"
android:textAlignment="center"
android:textColor="@color/custom_color"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="33dp"
android:layout_marginRight="33dp"
android:layout_marginTop="79dp"
android:orientation="vertical"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent">
<TextView
android:id="@+id/timeField"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:fontFamily="sans-serif-smallcaps"
android:gravity="center_vertical"
android:shadowColor="#80000000"
android:shadowDx="1"
android:shadowDy="1"
android:shadowRadius="2"
android:text="12:34"
android:textColor="@color/white"
android:textSize="60sp" />
<TextView
android:id="@+id/dayField"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:fontFamily="sans-serif-smallcaps"
android:shadowColor="#80000000"
android:shadowDx="1"
android:shadowDy="1"
android:shadowRadius="2"
android:text="TextView"
android:textColor="@color/white"
android:textSize="18sp"
tools:text="SATURDAY," />
</LinearLayout>
<TextView
android:id="@+id/timer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="0dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:textAlignment="center"
android:textColor="@color/custom_color"
android:visibility="invisible"
app:layout_constraintBottom_toTopOf="@+id/pinField"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent" />
<TableLayout
android:id="@+id/tb"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:visibility="visible"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@+id/pinField"
app:layout_constraintVertical_bias="0.491">
<TableRow
android:id="@+id/tr1"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageButton
android:id="@+id/one"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@android:color/transparent"
app:srcCompat="@drawable/ic_one_48dp" />
<ImageButton
android:id="@+id/two"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@android:color/transparent"
app:srcCompat="@drawable/ic_two_48dp" />
<ImageButton
android:id="@+id/three"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@android:color/transparent"
app:srcCompat="@drawable/ic_three_48dp" />
</TableRow>
<TableRow
android:id="@+id/tr2"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageButton
android:id="@+id/four"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@android:color/transparent"
app:srcCompat="@drawable/ic_four_48dp" />
<ImageButton
android:id="@+id/five"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@android:color/transparent"
app:srcCompat="@drawable/ic_five_48dp" />
<ImageButton
android:id="@+id/six"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@android:color/transparent"
app:srcCompat="@drawable/ic_six_48dp" />
</TableRow>
<TableRow
android:id="@+id/tr3"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageButton
android:id="@+id/eight"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@android:color/transparent"
app:srcCompat="@drawable/ic_eight_48dp" />
<ImageButton
android:id="@+id/seven"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@android:color/transparent"
app:srcCompat="@drawable/ic_seven_48dp" />
<ImageButton
android:id="@+id/nine"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@android:color/transparent"
app:srcCompat="@drawable/ic_nine_48dp" />
</TableRow>
<TableRow
android:id="@+id/tr4"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageButton
android:id="@+id/backSp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@android:color/transparent"
app:srcCompat="@drawable/ic_backspace_white_48dp" />
<ImageButton
android:id="@+id/zero"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@android:color/transparent"
app:srcCompat="@drawable/ic_zero_48dp" />
<ImageButton
android:id="@+id/done"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@android:color/transparent"
app:srcCompat="@drawable/ic_check_white_48dp" />
</TableRow>
</TableLayout>
</android.support.constraint.ConstraintLayout>
这是java代码:
ImageButton one,two,three,four,five,six,seven,eight,nine,zero,done,backSp;
TableLayout tb;
TableRow tr1,tr2,tr3,tr4;
EditText password;
TextView time,day,appName,timer;
ImageView appIco;
Intent i;
Receiver r;
int wrongCounter,c,maxWrong,maxLength;
boolean quick_unlock = true;
String pass = "1234";
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.pin_app_lock);
setup();
Setter();
passwordInputHandler();
}
private void setup(){
tb = findViewById(R.id.tb);
tr1 = tb.findViewById(R.id.tr1);
tr2 = tb.findViewById(R.id.tr2);
tr3 = tb.findViewById(R.id.tr3);
tr4 = tb.findViewById(R.id.tr4);
one = tr1.findViewById(R.id.one);
two = tr1.findViewById(R.id.two);
three = tr1.findViewById(R.id.three);
four = tr2.findViewById(R.id.four);
five = tr2.findViewById(R.id.five);
six = tr2.findViewById(R.id.six);
seven = tr3.findViewById(R.id.seven);
eight = tr3.findViewById(R.id.eight);
nine = tr3.findViewById(R.id.nine);
zero = tr4.findViewById(R.id.zero);
done = tr4.findViewById(R.id.done);
backSp = tr4.findViewById(R.id.backSp);
appIco = findViewById(R.id.appIco);
appName = findViewById(R.id.appName);
timer = findViewById(R.id.timer);
password = findViewById(R.id.pinField);
time = findViewById(R.id.timeField);
day = findViewById(R.id.dayField);
i=getIntent();
r=new Receiver();
wrongCounter=0;
c=0;
maxWrong = 3;
maxLength = 4;
password.setShowSoftInputOnFocus(false);
}
//Sets Time,App Icon
private void Setter(){
final Handler someHandler = new Handler(getMainLooper());
someHandler.postDelayed(new Runnable() {
@Override
public void run() {
time.setText(new SimpleDateFormat("hh:mm", Locale.US).format(new Date()));
// time.setText(new SimpleDateFormat("HH:mm", Locale.US).format(new Date()));//HH for 24 hrs clock
someHandler.postDelayed(this, 1000);
}
}, 10);
someHandler.postDelayed(new Runnable() {
@Override
public void run() {
String str = new SimpleDateFormat("EEE", Locale.US).format(new Date());
if(str.equalsIgnoreCase("sun")){str="SUNDAY, ";}
else if(str.equalsIgnoreCase("mon")){str="MONDAY, ";}
else if(str.equalsIgnoreCase("tue")){str="TUESDAY, ";}
else if(str.equalsIgnoreCase("wed")){str="WEDNESDAY, ";}
else if(str.equalsIgnoreCase("thu")){str="THURSDAY, ";}
else if(str.equalsIgnoreCase("fri")){str="FRIDAY, ";}
else{str="SATURDAY, ";}
String str1 = new SimpleDateFormat("MMM dd", Locale.US).format(new Date()).toUpperCase();
if(str.substring(0,3).equalsIgnoreCase("mar")){str+="MARCH"+str1.substring(3);}
else if(str.substring(0,3).equalsIgnoreCase("apr")){str+="APRIL"+str1.substring(3);}
else if(str.substring(0,3).equalsIgnoreCase("jun")){str+="JUNE"+str1.substring(3);}
else if(str.substring(0,3).equalsIgnoreCase("jul")){str+="JULY"+str1.substring(3);}
else if(str.substring(0,3).equalsIgnoreCase("aug")){str+="AUGUST"+str1.substring(3);}
else{str+=str1;}
day.setText(str);
someHandler.postDelayed(this, 1000);
}
}, 10);
try
{
Drawable d = getPackageManager().getApplicationIcon(i.getStringExtra("pname"));
appIco.setImageDrawable(d);
}
catch (PackageManager.NameNotFoundException e)
{
Log.e("PACKAGE NOT FOUND","PACKAGE NOT FOUND");
}
appName.setText(i.getStringExtra("appName"));
}
//Password Edittext Handling
private void passwordInputHandler(){
one.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
password.setText(password.getText()+"1");
}
});
two.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
password.setText(password.getText()+"2");
}
});
three.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
password.setText(password.getText()+"3");
}
});
four.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
password.setText(password.getText()+"4");
}
});
five.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
password.setText(password.getText()+"5");
}
});
six.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
password.setText(password.getText()+"6");
}
});
seven.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
password.setText(password.getText()+"7");
}
});
eight.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
password.setText(password.getText()+"8");
}
});
nine.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
password.setText(password.getText()+"9");
}
});
zero.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
password.setText(password.getText()+"0");
}
});
backSp.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
password.setText(password.getText().toString().substring(0, password.getText().toString().length() - 1));
}
});
}
所以有人可以告诉我这个问题吗?