在Android按钮中实现Motion Event,在Edittext上显示密码并点击备用按钮

时间:2017-06-05 07:25:56

标签: android motionevent

我在android中有一个用于密码字段的编辑文本。编辑文本的输入类型是password.To,编辑文本的右边,我有一个按钮,当点击按钮时会显示密码。我已实施"显示密码"在android.Below中使用Motion Event的部分是正常工作的代码:

<?php
    $dbhost =   'localhost';
    $dbuser =   'xxx'; 
    $dbpwd  =   'xxx'; 
    $dbname =   'xxx';
    $db     =   new mysqli( $dbhost, $dbuser, $dbpwd, $dbname );
?>
<!doctype html>
<html>
    <head>
        <title>Basic Pagination</title>
        <style>
            #paging{border-top:1px solid black;margin:1rem auto;float:none;clear:both;}
            #paging *{margin:0.5rem 1rem;clear:none!important;display:inline-block;}
            .minimal{margin:0.5rem 0.1rem!important;}
        </style>
    </head>
    <body>
        <div id='results'>
            <?php
                /* Single user supplied parameter - the PAGE */
                $page = isset( $_GET['page'] ) ? filter_input( INPUT_GET, 'page', FILTER_SANITIZE_NUMBER_INT ) : 0;


                if( is_numeric( $page ) && $page >= 0 ){

                    $rpp = 10;  /* Records Per Page */
                    $offset = $page * $rpp; /* paging offset */

                    /*
                        In order to determine the paging you need to know the total number of records available 
                        that are returned by the query with any given WHERE clause. The sub-query therefore uses
                        the same WHERE clause and returns a number to be used later.

                        Edit the sql to suit your needs and edit the data that is rendered in the while loop.
                    */
                    $sql="select
                        ( select count(*) from `maps`  where `location_enabled`=1 ) as 'count',
                        `id`,
                        `location_name` as 'name'
                        from `maps`
                        where `location_enabled`=1
                        limit {$offset},{$rpp}";



                    /*
                        Ignoring the possible SQL injection vulnerability - run the query
                    */
                    $results=$db->query( $sql );

                    if( $results && $results->num_rows > 0 ){

                        /* Process the recordset however is appropriate for your use-case */
                        while( $rs=$results->fetch_object() ){

                            /* From sql query, the total number of records that satisfy the "WHERE" clause */
                            $count=$rs->count;

                            /* output data - thumbnails etc etc */
                            echo "<div>{$rs->id} - {$rs->name}</div>";
                        }


                    } else {
                        echo "Error! Unable to fetch results";
                    }
                } else {
                    echo "Error... Bad foo!";
                }
            ?>
        </div>
        <div id='paging'>
            <?php
                /*
                    Calculate links for basic pagination
                    ( First,Previous,Next,Last )
                */
                if( is_numeric( $page ) ){

                    $total_pages = floor( $count / $rpp );

                    $text='First';
                    if( $page == 0 ){
                        echo "<div>$text</div>";
                    } else {
                        echo "<a href='?page=0'>$text</a>";
                    }

                    $text='Previous';
                    if( $page > 0 ){
                        echo "<a href='?page=".max( 0, $page - 1 )."'>$text</a>";
                    } else {
                        echo "<div>$text</div>";
                    }

                    $text='Next';
                    if( $page >= $total_pages ){
                        echo "<div>$text</div>";
                    } else {
                        echo "<a href='?page=".min( $total_pages, $page + 1 )."'>$text</a>";
                    }

                    $text='Last';
                    if( $page==$total_pages ){
                        echo "<div>$text</div>";
                    } else {
                        echo "<a href='?page={$total_pages}'>$text</a>";
                    }


                    /* Generate basic hyperlink for each possible page */
                    for( $i=0; $i <= $total_pages; $i++ ) {
                        echo "<a class='minimal' href='?page={$i}'>".( $i + 1 )."</a>";
                    }
                }
            ?>
        </div>
    </body>
</html>
<?php

    $db->close();

?>

上述代码适用于用户点击按钮,显示密码并且删除按钮触摸事件时,文本再次成为密码字段的情况。但是,我想要的是,如果单击按钮一次,编辑文本将显示密码,密码将一直显示,直到再次单击该按钮。因此,再次单击该按钮时,编辑文本将更改其状态再次输入密码。任何人都可以告诉我怎么办?

1 个答案:

答案 0 :(得分:0)

使用以下代码:

定义一个标志,

    boolean isPassWordShowing = false;

点击ShowPassword按钮:

showPassword.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            if(isPassWordShowing)
            {
                passwordET.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
                isPassWordShowing = false;
            }
            else
            {
                passwordET.setInputType(InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
                isPassWordShowing = true;
            }


        }
    });