按下按钮时如何更改图像视图的图标?
How can I change icon of image view when a button is pressed?
我的 Android 项目中有 3 个 imageview
。单击按钮时应将其更改为图标。当释放点击时,它应该是默认图标。在我的代码中,它会在单击按钮时更改图标,但它会永久更改图标,直到单击此 Activity
的后退按钮。它应该只在单击按钮时将 mainpage_acil_icon_p
替换为 mainpage_acil_icon
。
我该如何解决这个问题?
activity_mainpage.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/Linear_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#000000"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="10dp"
android:alpha=".85"
android:background="@drawable/mainpage_background"
android:orientation="vertical"
android:weightSum="60">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_weight="40"
android:orientation="vertical">
<TextView
android:id="@+id/textView2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="EMAS"
android:textAlignment="center"
android:textColor="@color/colorWhite"
android:textSize="18sp"
android:textStyle="bold" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_weight="20"
android:orientation="vertical">
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#c7c7c8"/>
<include layout="@layout/mainpage_inner_part" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
MainPageActivity.java
public class MainPageActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_mainpage);
ListView list;
final String[] web = {
getString(R.string.mainpage_acil),
getString(R.string.mainpage_bildirim),
getString(R.string.mainpage_isguveligi),
};
Integer[] imageId = {
R.drawable.mainpage_acil_icon_p,
R.drawable.mainpage_bildirim_icon_p,
R.drawable.mainpage_isguvenligi_icon_p,
};
CustomListMainPage adapter = new
CustomListMainPage(MainPageActivity.this, web, imageId);
list = (ListView) findViewById(R.id.list);
list.setAdapter(adapter);
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Log.v("Pozisyon", String.valueOf(position));
Intent intentPos;
ImageView imageView= (ImageView) view.findViewById(R.id.img);
boolean flag=false;
switch (position) {
case 0 :
//Toast.makeText(MainPageActivity.this, "You Clicked at " + web[+position], Toast.LENGTH_SHORT).show();
imageView.setImageResource(R.drawable.mainpage_acil_icon);
intentPos=new Intent(MainPageActivity.this,EmergencyActivity.class);
startActivity(intentPos);
break;
case 1 :
imageView.setImageResource(R.drawable.mainpage_bildirim_icon);
intentPos=new Intent(MainPageActivity.this,NotificationPage.class);
startActivity(intentPos); break;
case 2 :
imageView.setImageResource(R.drawable.mainpage_isguvenligi_icon);
intentPos=new Intent(MainPageActivity.this,JobSecurityActivity.class);
startActivity(intentPos);
break;
default :
System.out.println("Hatali secim! 1, 2 ya da 3'e basiniz.");
break;
}
}
});
}
}
我认为最简单的方法是使用选择器。
在您的 activity 布局文件中,将 ImageView 替换为 Button 并将选择器设置为背景。
在您的 activity 布局中:
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button"
android:background="@drawable/btn_selector"
android:id="@+id/button"/>
这里是 btn_selector.xml 文件
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true" android:state_pressed="true" android:drawable="@drawable/mainpage_acil_icon" />
<item android:state_pressed="true" android:drawable="@drawable/mainpage_acil_icon"/>
<item android:state_checked="true" android:drawable="@drawable/mainpage_acil_icon"/>
<item android:drawable="@drawable/mainpage_acil_icon_p"/>
</selector>
将此选择器文件放入您的 res/drawable 文件夹中。我想这会对你有所帮助。
我的 Android 项目中有 3 个 imageview
。单击按钮时应将其更改为图标。当释放点击时,它应该是默认图标。在我的代码中,它会在单击按钮时更改图标,但它会永久更改图标,直到单击此 Activity
的后退按钮。它应该只在单击按钮时将 mainpage_acil_icon_p
替换为 mainpage_acil_icon
。
我该如何解决这个问题?
activity_mainpage.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/Linear_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#000000"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="10dp"
android:alpha=".85"
android:background="@drawable/mainpage_background"
android:orientation="vertical"
android:weightSum="60">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_weight="40"
android:orientation="vertical">
<TextView
android:id="@+id/textView2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="EMAS"
android:textAlignment="center"
android:textColor="@color/colorWhite"
android:textSize="18sp"
android:textStyle="bold" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_weight="20"
android:orientation="vertical">
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#c7c7c8"/>
<include layout="@layout/mainpage_inner_part" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
MainPageActivity.java
public class MainPageActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_mainpage);
ListView list;
final String[] web = {
getString(R.string.mainpage_acil),
getString(R.string.mainpage_bildirim),
getString(R.string.mainpage_isguveligi),
};
Integer[] imageId = {
R.drawable.mainpage_acil_icon_p,
R.drawable.mainpage_bildirim_icon_p,
R.drawable.mainpage_isguvenligi_icon_p,
};
CustomListMainPage adapter = new
CustomListMainPage(MainPageActivity.this, web, imageId);
list = (ListView) findViewById(R.id.list);
list.setAdapter(adapter);
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Log.v("Pozisyon", String.valueOf(position));
Intent intentPos;
ImageView imageView= (ImageView) view.findViewById(R.id.img);
boolean flag=false;
switch (position) {
case 0 :
//Toast.makeText(MainPageActivity.this, "You Clicked at " + web[+position], Toast.LENGTH_SHORT).show();
imageView.setImageResource(R.drawable.mainpage_acil_icon);
intentPos=new Intent(MainPageActivity.this,EmergencyActivity.class);
startActivity(intentPos);
break;
case 1 :
imageView.setImageResource(R.drawable.mainpage_bildirim_icon);
intentPos=new Intent(MainPageActivity.this,NotificationPage.class);
startActivity(intentPos); break;
case 2 :
imageView.setImageResource(R.drawable.mainpage_isguvenligi_icon);
intentPos=new Intent(MainPageActivity.this,JobSecurityActivity.class);
startActivity(intentPos);
break;
default :
System.out.println("Hatali secim! 1, 2 ya da 3'e basiniz.");
break;
}
}
});
}
}
我认为最简单的方法是使用选择器。
在您的 activity 布局文件中,将 ImageView 替换为 Button 并将选择器设置为背景。
在您的 activity 布局中:
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button"
android:background="@drawable/btn_selector"
android:id="@+id/button"/>
这里是 btn_selector.xml 文件
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true" android:state_pressed="true" android:drawable="@drawable/mainpage_acil_icon" />
<item android:state_pressed="true" android:drawable="@drawable/mainpage_acil_icon"/>
<item android:state_checked="true" android:drawable="@drawable/mainpage_acil_icon"/>
<item android:drawable="@drawable/mainpage_acil_icon_p"/>
</selector>
将此选择器文件放入您的 res/drawable 文件夹中。我想这会对你有所帮助。