繁体   English   中英

单击时Android Java按钮图像更改

[英]Android java button image changes when clicked

我似乎无法使我的代码正常工作。 我不断收到错误消息。

我用这段代码制作了一个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>

这是我的实际代码

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);
    }
}

我正在尝试获取它,以便当我单击按钮时它具有显示其他图像的点击效果

我在这里得到一个错误:

我在这行仍然收到错误v.setSelected(arg1.getAction()==MotionEvent.ACTION_DOWN);

在“ v。”上

我的按钮的代码

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

使用state_pressed

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"/>

您的选择器应如下所示:

<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>

调用此login_button_selector.xml并将其放在您的/ res / drawable文件夹中
现在像这样使用它:

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

当视图处于“按下”状态时,它将与选择器中的第一项匹配。 当它不处于“按下”状态时,它将与选择器中的第二项匹配。

暂无
暂无

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

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