简体   繁体   English

单击时Android Java按钮图像更改

[英]Android java button image changes when clicked

I cant seem to get my code to work. 我似乎无法使我的代码正常工作。 I keep getting a error. 我不断收到错误消息。

I made a selector.xml with this code 我用这段代码制作了一个selector.xml

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_selected="true"
        android:drawable="@drawable/loginbuttondn" />
    <item android:state_selected="false"
        android:drawable="@drawable/loginbutton" />
</selector>

heres my actual code 这是我的实际代码

package monaiz.net.periscope.periscope;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.MotionEvent;
import android.view.View.OnTouchListener;



public class MainActivity extends AppCompatActivity implements OnTouchListener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


    }
    public boolean onTouch(View v, MotionEvent event) {
        int action = event.getAction();
        switch (action) {
            v.setOnTouchListener(new OnTouchListener() {
                @Override
                public boolean onTouch(View arg0, MotionEvent arg1) {

                    v.setSelected(arg1.getAction()==MotionEvent.ACTION_DOWN);
                    return true;
                }
            });

        }
        return true;
    }


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

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

Im trying to get it so that when i click the button it has a click down effect showing the other image 我正在尝试获取它,以便当我单击按钮时它具有显示其他图像的点击效果

Im getting a error here: 我在这里得到一个错误:

Im still getting a error on this line v.setSelected(arg1.getAction()==MotionEvent.ACTION_DOWN); 我在这行仍然收到错误v.setSelected(arg1.getAction()==MotionEvent.ACTION_DOWN);

on the "v." 在“ v。”上

code for my button 我的按钮的代码

 <ImageView
        android:layout_width="280dp"
        android:layout_height="90dp"
        android:layout_marginTop="830px"
        android:layout_marginLeft="55dp"
        android:src="@drawable/loginbutton"/>

Use state_pressed : 使用state_pressed

drawable/selector.xml drawable / selector.xml

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true"
        android:drawable="@drawable/loginbuttondn" />
    <item
        android:drawable="@drawable/loginbutton" />
</selector>

<Button
    android:layout_width="280dp"
    android:layout_height="90dp"
    android:background="@drawable/selector"/>

Your selector needs to look like this: 您的选择器应如下所示:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true"
        android:drawable="@drawable/loginbuttondn" />
    <item android:drawable="@drawable/loginbutton" />
</selector>

Call this login_button_selector.xml and put it in your /res/drawable folder 调用此login_button_selector.xml并将其放在您的/ res / drawable文件夹中
Now use it like this: 现在像这样使用它:

<ImageView
        android:layout_width="280dp"
        android:layout_height="90dp"
        android:layout_marginTop="830px"
        android:layout_marginLeft="55dp"
        android:src="@drawable/login_button_selector"/>

When the view is in "pressed" state, it will match the first item in the selector. 当视图处于“按下”状态时,它将与选择器中的第一项匹配。 When it is not in "pressed" state, it will just match the second item in the selector. 当它不处于“按下”状态时,它将与选择器中的第二项匹配。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM