在android

时间:2016-10-01 17:43:23

标签: android xml

我需要构建一个Passcode屏幕,该屏幕应该有4个数字字符,我在键盘上方的网格布局中有4个输入图像视图,而我在输入时,圆圈应该填充颜色以指示输入。 像这样它应该填充颜色:

Passcode_Screen.Java

    public class Passcode_Screen extends Activity implements View.OnClickListener{
    String userEntered;
    String userPin = "1234";
    final int PIN_LENGTH = 4;
    boolean keyPadLockedFlag = false;
    Context appContext;
    TextView titleView;
    TextView pinBox0;
    TextView pinBox1;
    TextView pinBox2;
    TextView pinBox3;
    TextView pinBox4;


    TextView statusView;

    Button button0;
    Button button1;
    Button button2;
    Button button3;
    Button button4;
    Button button5;
    Button button6;
    Button button7;
    Button button8;
    Button button9;
    Button button10;
    Button buttonExit;
    Button buttonDelete;
    EditText passwordInput;
    ImageView backSpace;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        appContext = this;
        userEntered = "";


        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);

        // set the view content
        setContentView(R.layout.activity_passcode__screen);

        //Typeface xpressive=Typeface.createFromAsset(getAssets(), "fonts/XpressiveBold.ttf");

        statusView = (TextView) findViewById(R.id.statusview);
        passwordInput = (EditText) findViewById(R.id.Result);
        backSpace = (ImageView) findViewById(R.id.imageView);
        buttonExit = (Button) findViewById(R.id.buttonexit);
        backSpace.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                passwordInput.setText(passwordInput.getText().toString().substring(0, passwordInput.getText().toString().length() - 2));
            }
        });
        buttonExit.setOnClickListener(new View.OnClickListener() {
                                          public void onClick(View v) {

//                                              //Exit app
//                                              Intent i = new Intent();
//                                              i.setAction(Intent.ACTION_MAIN);
//                                              i.addCategory(Intent.CATEGORY_HOME);
//                                              appContext.startActivity(i);
//                                              finish();

                                              Intent camera = new Intent(Passcode_Screen.this, MainActivity.class);
                                              startActivity(camera);

                                          }

                                      }
        );
        //buttonExit.setTypeface(xpressive);


        buttonDelete = (Button) findViewById(R.id.buttondelete);
        buttonDelete.setOnClickListener(new View.OnClickListener() {
                                            public void onClick(View v) {

                                                if (keyPadLockedFlag == true) {
                                                    return;
                                                }

                                                if (userEntered.length() > 0) {
                                                    userEntered = userEntered.substring(0, userEntered.length() - 1);
                                                    passwordInput.setText("");
                                                }


                                            }
                                        }
        );





        titleView = (TextView) findViewById(R.id.time);
        //titleView.setTypeface(xpressive);


        View.OnClickListener pinButtonHandler = new View.OnClickListener() {
            public void onClick(View v) {

                if (keyPadLockedFlag == true) {
                    return;
                }

                Button pressedButton = (Button) v;


                if (userEntered.length() < PIN_LENGTH) {
                    userEntered = userEntered + pressedButton.getText();
                    Log.v("PinView", "User entered=" + userEntered);

                    //Update pin boxes
                    passwordInput.setText(passwordInput.getText().toString() + "*");
                    passwordInput.setSelection(passwordInput.getText().toString().length());

                    if (userEntered.length() == PIN_LENGTH) {
                        //Check if entered PIN is correct
                        if (userEntered.equals(userPin))
                        {
                            Intent login = new Intent(Passcode_Screen.this,Site_screen.class);
                            startActivity(login);

                        }
                        else
                        {
                            Toast.makeText(getApplicationContext(), "PIN Invalid",Toast.LENGTH_SHORT).show();
                        }
                    }
                } else {
                    //Roll over
                    passwordInput.setText("");

                    userEntered = "";

                   // statusView.setText("");

                    userEntered = userEntered + pressedButton.getText();
                    Log.v("PinView", "User entered=" + userEntered);

                    //Update pin boxes
                    passwordInput.setText("8");

                }


            }
        };


        button0 = (Button) findViewById(R.id.button0);
        //button0.setTypeface(xpressive);
        button0.setOnClickListener(pinButtonHandler);

        button1 = (Button) findViewById(R.id.button1);
        //button1.setTypeface(xpressive);
        button1.setOnClickListener(pinButtonHandler);

        button2 = (Button) findViewById(R.id.button2);
        //button2.setTypeface(xpressive);
        button2.setOnClickListener(pinButtonHandler);


        button3 = (Button) findViewById(R.id.button3);
        //button3.setTypeface(xpressive);
        button3.setOnClickListener(pinButtonHandler);

        button4 = (Button) findViewById(R.id.button4);
        //button4.setTypeface(xpressive);
        button4.setOnClickListener(pinButtonHandler);

        button5 = (Button) findViewById(R.id.button5);
        //button5.setTypeface(xpressive);
        button5.setOnClickListener(pinButtonHandler);

        button6 = (Button) findViewById(R.id.button6);
        //button6.setTypeface(xpressive);
        button6.setOnClickListener(pinButtonHandler);

        button7 = (Button) findViewById(R.id.button7);
        //button7.setTypeface(xpressive);
        button7.setOnClickListener(pinButtonHandler);

        button8 = (Button) findViewById(R.id.button8);
        //button8.setTypeface(xpressive);
        button8.setOnClickListener(pinButtonHandler);

        button9 = (Button) findViewById(R.id.button9);
        //button9.setTypeface(xpressive);
        button9.setOnClickListener(pinButtonHandler);


        buttonDelete = (Button) findViewById(R.id.buttondelete);
        //buttonDelete.setTypeface(xpressive);


    }

    @Override
    public void onBackPressed() {
        // TODO Auto-generated method stub

        //App not allowed to go back to Parent activity until correct pin entered.
        return;
        //super.onBackPressed();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        //getMenuInflater().inflate(R.menu.activity_pin_entry_view, menu);
        return true;
    }

    @Override
    public void onClick(View view) {

    }


    private class LockKeyPadOperation extends AsyncTask<String, Void, String> {

        @Override
        protected String doInBackground(String... params) {
            for (int i = 0; i < 2; i++) {
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }

            return "Executed";
        }

        @Override
        protected void onPostExecute(String result) {
            statusView.setText("");

            //Roll over
            passwordInput.setText("");
            ;

            userEntered = "";

            keyPadLockedFlag = false;
        }

        @Override
        protected void onPreExecute() {
        }

        @Override
        protected void onProgressUpdate(Void... values) {
        }


    }
    }

activity_passcode_screen:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_centerInParent="true"
  >

    <View
        android:layout_width="200dp"
        android:layout_height="1dp"
        android:background="#FFF"
        android:layout_above="@+id/numericPad"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="20dp"
        android:id="@+id/view" />

    <ImageView
        android:layout_width="20dp"
        android:layout_height="20dp"
        android:layout_above="@+id/view"
        android:layout_marginBottom="10dp"
        android:id="@+id/imageView" />

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:columnCount="1"
        android:rowCount="4"
        android:id="@+id/glResult"
        android:layout_below="@+id/statusview"
        android:orientation="horizontal"
        android:layout_centerInParent="true"
        android:layout_marginTop="20dp"
        >

        <ImageView
            android:layout_width="20dp"
            android:layout_height="20dp"
            android:layout_marginBottom="10dp"
            android:orientation="horizontal"
            android:id="@+id/imvPin1"
            android:background="@drawable/round"
            android:layout_marginRight="10dp"
            />

        <ImageView
            android:layout_width="20dp"
            android:layout_height="20dp"
            android:layout_marginBottom="10dp"
            android:id="@+id/imvPin2"
            android:background="@drawable/round"
            android:orientation="horizontal"
            android:layout_marginRight="10dp"
            />

        <ImageView
            android:layout_width="20dp"
            android:layout_height="20dp"
            android:layout_marginBottom="10dp"
            android:id="@+id/imvPin3"
            android:background="@drawable/round"
            android:orientation="horizontal"
            android:layout_marginRight="10dp"
            />

        <ImageView
            android:layout_width="20dp"
            android:layout_height="20dp"
            android:layout_marginBottom="10dp"
            android:id="@+id/imvPin4"
            android:background="@drawable/round"
            android:orientation="horizontal"
            android:layout_marginRight="10dp"
            />

    </LinearLayout>

    <GridLayout
        android:id="@id/numericPad"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:columnCount="3"
        android:rowCount="4"
        android:orientation="horizontal"
        android:layout_centerVertical="true"
        android:layout_below="@+id/glResult"
        android:layout_marginTop="20dp"
        android:layout_centerInParent="true">

        <Button android:text="1"
            android:layout_row="0"
            android:layout_column="0"
            android:id="@+id/button1"
            android:background="@drawable/round"
            android:horizontalSpacing="16dp"
            android:verticalSpacing="16dp"
            android:padding="16dp"
            android:layout_width="48dp"
            android:layout_height="wrap_content"
            android:layout_marginRight="15dp"
            android:layout_marginBottom="15dp"
            />

        <Button android:text="2"
            android:layout_row="0"
            android:layout_column="1"
            android:id="@+id/button2"
            android:background="@drawable/round"
            android:horizontalSpacing="16dp"
            android:verticalSpacing="16dp"
            android:padding="16dp"
            android:layout_width="48dp"
            android:layout_marginRight="15dp"
            android:layout_marginBottom="15dp"/>

        <Button android:text="3"
            android:layout_row="0"
            android:layout_column="2"
            android:id="@+id/button3"
            android:background="@drawable/round"
            android:horizontalSpacing="16dp"
            android:verticalSpacing="16dp"
            android:padding="16dp"
            android:layout_width="48dp"
            android:layout_marginRight="15dp"
            android:layout_marginBottom="15dp"
            />



        <Button android:text="4"
            android:layout_row="1"
            android:layout_column="0"
            android:id="@+id/button4"
            android:background="@drawable/round"
            android:horizontalSpacing="16dp"
            android:verticalSpacing="16dp"
            android:padding="16dp"
            android:layout_width="48dp"
            android:layout_marginRight="15dp"
            android:layout_marginBottom="15dp"/>

        <Button android:text="5"
            android:layout_row="1"
            android:layout_column="1"
            android:id="@+id/button5"
            android:background="@drawable/round"
            android:horizontalSpacing="16dp"
            android:verticalSpacing="16dp"
            android:padding="16dp"
            android:layout_width="48dp" android:layout_marginRight="15dp"
            android:layout_marginBottom="15dp" />


        <Button android:text="6"
            android:layout_row="1"
            android:layout_column="2"
            android:id="@+id/button6"
            android:background="@drawable/round"
            android:horizontalSpacing="16dp"
            android:verticalSpacing="16dp"
            android:padding="16dp"
            android:layout_width="48dp"
            android:layout_marginRight="15dp"
            android:layout_marginBottom="15dp"
            />

        <Button android:text="7"
            android:layout_row="2"
            android:layout_column="0"
            android:id="@+id/button7"
            android:background="@drawable/round"
            android:horizontalSpacing="16dp"
            android:verticalSpacing="16dp"
            android:padding="16dp"
            android:layout_width="48dp"
            android:layout_marginRight="15dp"
            android:layout_marginBottom="15dp"/>

        <Button android:text="8"
            android:layout_row="2"
            android:layout_column="1"
            android:id="@+id/button8"
            android:background="@drawable/round"
            android:horizontalSpacing="16dp"
            android:verticalSpacing="16dp"
            android:padding="16dp"
            android:layout_width="48dp"
            android:layout_marginRight="15dp"
            android:layout_marginBottom="15dp"/>

        <Button android:text="9"
            android:layout_row="2"
            android:layout_column="2"
            android:id="@+id/button9"
            android:background="@drawable/round"
            android:horizontalSpacing="16dp"
            android:verticalSpacing="16dp"
            android:padding="16dp"
            android:layout_width="48dp"/>

        <Button

            android:text="0"
            android:layout_row="3"
            android:layout_column="1"
            android:id="@+id/button0"
            android:background="@drawable/round"
            android:horizontalSpacing="16dp"
            android:verticalSpacing="16dp"
            android:padding="16dp"
            android:layout_width="48dp"

            />


    </GridLayout>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:text="Enter Password"
        android:layout_centerInParent="true"
        android:textSize="20dp"
        android:textStyle="bold"
        android:id="@+id/statusview"
        android:layout_alignParentTop="true"
        android:layout_marginTop="50dp"
        android:gravity="center"/>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Back"
        android:id="@+id/buttonexit"
        android:background="#00000000"
        android:layout_alignBottom="@+id/buttondelete" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Reset"
        android:id="@+id/buttondelete"
        android:background="#00000000"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true" />


</RelativeLayout>

这里代替编辑文本(passcodeInput)我存储的输入值我删除了它,我需要添加图像视图字段,并在按下按钮时用颜色填充圆圈,应该从圆圈1到4填充线性地,当第四个输入填充时,它应该验证整个输入到默认的给定输入,如果它匹配它应该移动到下一个活动,否则应该显示错误信息。

我需要这样的形象:

https://play.google.com/store/apps/details?id=com.smart.mobile.lin.pin.locker

我对Android没有太多了解,所以请帮我解决这个问题。请分享上述问题的任何教程链接或示例代码。提前谢谢。

0 个答案:

没有答案