2017-10-12 85 views
0

我製作了圓角按鈕。但是當他們被按下時,他們並沒有感受到被按下的感覺。我想將這種效果添加到我的按鈕中,當他們被點擊時,他們應該感覺到他們被按下/點擊。 我在我的drawable文件夾中創建了一個button_rounded_corners_gradient.xml文件。這個文件包含使我的按鈕圓角和矩形的圓角的代碼。那麼我在xml中的按鈕代碼的android:background屬性中使用這個文件。如何讓我的圓角按鈕有被按下的感覺?

activity_mainn.xml

<?xml version="1.0" encoding="utf-8"?> 
<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" 
    tools:context="com.example.anonymous.fyplogin.MainActivity"> 


    <Button 
     android:id="@+id/fbbutton" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text=" Login with Facebook " 
     android:background="@drawable/button_rounded_corners_gradient" 
     android:layout_marginRight="8dp" 
     app:layout_constraintRight_toRightOf="parent" 
     android:layout_marginLeft="8dp" 
     app:layout_constraintLeft_toLeftOf="parent" 
     app:layout_constraintTop_toTopOf="parent" 
     android:layout_marginTop="8dp" 
     android:layout_marginBottom="8dp" 
     app:layout_constraintBottom_toTopOf="@+id/phbutton" 
     app:layout_constraintVertical_bias="0.919" /> 

    <Button 
     android:id="@+id/phbutton" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text=" Login with Phone " 
     android:background="@drawable/button_rounded_corners_gradient" 
     android:layout_marginRight="8dp" 
     app:layout_constraintRight_toRightOf="parent" 
     android:layout_marginLeft="8dp" 
     app:layout_constraintLeft_toLeftOf="parent" 
     app:layout_constraintBottom_toBottomOf="parent" 
     android:layout_marginBottom="71dp" /> 
</android.support.constraint.ConstraintLayout> 

button_rounded_corners_gradient.xml

<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android"> 

    <item> 
     <shape android:shape="rectangle"> 
      <!--make a gradient background--> 
      <gradient 
       android:type="linear" 
       android:startColor="#003333" 
       android:endColor="#003333" 
       android:centerColor="#003333" 
       android:angle="90" 
       android:gradientRadius="90" 
       /> 
      <!--apply a border around button--> 
      <stroke android:color="#ff0000" android:width="2dp" /> 
      <!-- make the button corners rounded--> 
      <corners android:radius="25dp"/> 
     </shape> 
    </item> 

</selector> 
+2

試試這個https://stackoverflow.com/a/7176006/3364266 –

+0

看看這篇文章https://開頭stacko versink.com/questions/3882064/how-to-change-color-of-button-in-android-when-clicked希望它能幫到你 –

回答

1

您可以添加一個android:statebutton_rounded_corners_gradient.xml

來自:

<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android"> 

    <item> 
     <shape android:shape="rectangle"> 
      <!--make a gradient background--> 
      <gradient 
       android:type="linear" 
       android:startColor="#003333" 
       android:endColor="#003333" 
       android:centerColor="#003333" 
       android:angle="90" 
       android:gradientRadius="90" 
       /> 
      <!--apply a border around button--> 
      <stroke android:color="#ff0000" android:width="2dp" /> 
      <!-- make the button corners rounded--> 
      <corners android:radius="25dp"/> 
     </shape> 
    </item> 

</selector> 

到:

<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android"> 

    <!-- The normal state of the button --> 
    <item android:state_pressed="false"> 
     <shape android:shape="rectangle"> 
      <solid android:color="@android:color/transparent" /> 
      <stroke android:color="#ff0000" android:width="1dp" /> 
      <!-- make the button corners rounded--> 
      <corners android:radius="25dp"/> 
     </shape> 
    </item> 

    <!-- The state when the button is being pressed or clicked. --> 
    <item android:state_pressed="true"> 
     <shape android:shape="rectangle"> 
      <solid android:color="@android:color/holo_red_dark"/> 
      <!--apply a border around button--> 
      <stroke android:color="#ff0000" android:width="1dp" /> 
      <!-- make the button corners rounded--> 
      <corners android:radius="25dp"/> 
     </shape> 
    </item> 
</selector> 

給振動效果,以您的按鈕點擊時。將此 <uses-permission android:name="android.permission.VIBRATE"/> 許可給您的AndroidManifest.xml 並對您的活動執行此操作。

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    //... 
    final Vibrator vibrator = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE); 

    Button button = findViewById(R.id.button1); 
    button.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View view) { 
      // Set the milliseconds of vibration you want. 
      vibrator.vibrate(500); 
     } 
    }); 
} 
+0

謝謝你這麼多。它工作正常。但問題是,它會產生非常小的按鈕效果。如果用戶充分注意,則用戶可以感覺到該按鈕被按下,否則不被按下。有什麼辦法可以讓這個效果更清晰。 –

+0

我編輯了我的答案,如果你想爲你的按鈕創建一個真棒效果,你可以播放''標籤。 – Jerrol

+0

非常感謝你這個東西幫助我分配。你能幫我一件事嗎?如果我想把這個效果放到我的按鈕中,當按鈕被點擊時,我的手機應該有點振動 –

0

xmldrawable創建使按鈕矩形 這一行添加到XML

<item android:state_pressed="true"> 
0

使用foregr ound屬性:

<Button 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:background="@drawable/button_rounded_corners_gradient" 
    android:foreground="?android:selectableItemBackground" /> 
0

你應該嘗試紋波擠壓從而得到一個按鈕按下的感覺上按鈕的波浪效果;)

只是做一個新的可繪製文件,並將此文件作爲背景的按鈕。 ..

<?xml version="1.0" encoding="utf-8"?> 
<ripple xmlns:android="http://schemas.android.com/apk/res/android" 
android:color="@color/colorPrimary"> 
</ripple> 

希望它可以幫助