2014-01-28 17 views
0

中將按鈕標籤顯示爲帶星號的密碼我有一個要求,我的登錄表單具有按鈕形式的pasword條目。 這是我的佈局xml。可以在Android

<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:paddingBottom="@dimen/activity_vertical_margin" 
     android:paddingLeft="@dimen/activity_horizontal_margin" 
     android:paddingRight="@dimen/activity_horizontal_margin" 
     android:paddingTop="@dimen/activity_vertical_margin" 
     android:background= "@android:color/black" 
     tools:context=".LoginActivity" > 



     <TextView 
      android:id="@+id/textView3" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_alignLeft="@+id/textView2" 
      android:layout_below="@+id/textView2" 
      android:layout_marginTop="26dp" 
      android:text="Password" 
      android:textColor="@android:color/darker_gray" 
      android:textAppearance="?android:attr/textAppearanceLarge" /> 

     <Button 
      android:id="@+id/button1" 
      android:layout_width="65sp" 
      android:layout_height="65sp" 
      android:layout_alignLeft="@+id/textView3" 
      android:layout_below="@+id/textView3" 
      android:layout_marginTop="36dp" 
      android:text="0" 
      android:textColor="#00ff10" 
      android:textSize="45sp" 

      /> 

     <Button 
      android:id="@+id/Button02" 
      android:layout_width="65sp" 
      android:layout_height="65sp" 
      android:layout_alignBaseline="@+id/Button01" 
      android:layout_alignBottom="@+id/Button01" 
      android:layout_toRightOf="@+id/Button01" 
      android:textColor="#00ff10" 
      android:textSize="45sp" 
      android:text="0" /> 

     <Button 
      android:id="@+id/Button03" 
      android:layout_width="65sp" 
      android:layout_height="65sp" 
      android:layout_alignBaseline="@+id/Button02" 
      android:layout_alignBottom="@+id/Button02" 
      android:layout_toRightOf="@+id/Button02" 
      android:textColor="#00ff10" 
      android:textSize="45sp" 
      android:text="0" /> 

     <Button 
      android:id="@+id/Button01" 
      android:layout_width="65sp" 
      android:layout_height="65sp" 
      android:layout_alignBaseline="@+id/button1" 
      android:layout_alignBottom="@+id/button1" 
      android:layout_toRightOf="@+id/button1" 
      android:textColor="#00ff10" 
      android:textSize="45sp"  
      android:text="0" /> 

     <Button 
      android:id="@+id/button2" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_alignRight="@+id/textView3" 
      android:layout_below="@+id/button1" 
      android:layout_marginTop="76dp" 
      android:textColor="@android:color/darker_gray" 
      android:text="Login" /> 

     <Button 
      android:id="@+id/Button04" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_alignBaseline="@+id/button2" 
      android:layout_alignBottom="@+id/button2" 
      android:layout_alignLeft="@+id/Button02" 
      android:layout_marginLeft="18dp" 
      android:textColor="@android:color/darker_gray" 
      android:text="Reset" /> 

    </RelativeLayout> 

這是活動的代碼片段

switch (v.getId()) { 
    case R.id.button1: 
    b3 = (Button)findViewById(R.id.button1); 

    int i3 = Integer.parseInt(b3.getText().toString()); 
    if(i3<=8) 
    i3 = i3+1; 
    else 
    i3 = 0; 
    b3.setText(String.valueOf(i3)); 

    break; 
上的按鈕多次將顯示等的對置的點擊數

enter image description here

點擊。 所以如果密碼是1234,點擊第一個按鈕一次,第二個按鈕兩次 ,第三個按鈕三次和第四個按鈕四次。 我有這個實現,但試圖找出一種方法來隱藏標籤使用星號* 用戶點擊按鈕後,看到*之前顯示輸入的值。 我想知道這是否可以通過這種方法完成?

+2

您可以在每個按鈕的OnClickListener更改按鈕的文本。 – akshay

+0

嗨yahska,可以延遲點擊誘導,使用戶可以看到一段時間的價值被選中?不知道如何使用setText更改文本,它顯示*時,我試過。我使用Activity代碼片段更新了我的問題。 –

+0

現在根據我對問題的理解,您可以改爲使用文本框。當用戶在TextWatcher的使用中輸入一位數字並檢查onTextChanged時。如果他插入了一位數字,那麼禁用該文本以便它更方便。如果用戶插入了錯誤的數字,那麼你已經有了重置功能。因此,這將是沒有問題的。 – akshay

回答

0

你可以做的是對任何此按鈕設置其他按鈕*的文本的點擊。

in Activity創建4個int變量和4個按鈕變量。

int pass1,pass2,pass3,pass4; 
Button button1,button2,button3,button4; 

初始化onCreate中的所有按鈕。

中的onClick

做:

if(v.getId()==R.id.button1){ 
    pass1=(pass1+1)%10; 
    button1.setText(""+pass1); 
}else{ 
    if(pass1!=0) 
     button1.setText("*"); 
} 

if(v.getId()==R.id.button2){ 
    pass2=(pass2+1)%10; 
    button2.setText(""+pass2); 
}else{ 
    if(pass2!=0) 
    button2.setText("*"); 
} 

同所有按鈕

您的密碼是:

String password=""+pass1+pass2+pass3+pass4; 
+0

嗨Vipul,這個建議接近我的要求,謝謝!儘管我仍然希望在2秒的時間間隔後將標籤淡化爲*。這可行嗎? –

0

嘗試實施此代碼爲EditText上

editText.setHint("Password"); 
editText.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);  
+0

嗨約翰,我沒有在我的代碼中使用ant editText。你在哪裏建議我使用你提供的這個代碼?請參考我添加的UI圖像。 –

+0

嗨,我會嘗試用edittext替換標籤並隱藏鍵盤。 – JAPK