为我的英语而烦恼.. 在我的Home_Activity应用程序中,我单击一个按钮并显示自定义AlertDialog。在这个AlertDialog(DialogAdd)中,我点击ImageView说iv1(或ImageButton - 两者都没有工作)和另一个自定义AlertDialog打开(选择) - 到目前为止一切顺利。现在我打开了第二个AlertDialog并看到,说了4张小照片。当我点击其中一张图片时,第二个AlertDialog应该关闭,ImageView(iv1)应该更改为AlertDialog 2中的选择图标。我已经运行了一切,但没有更改此ImageView / ImageButton ... 我把这个行为分成了一个小应用程序..
Home_Activity
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class Home_Activity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button = (Button) findViewById(R.id.btn_showDialog);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
DialogAdd addEntrieActivity = new DialogAdd(Home_Activity.this);
addEntrieActivity.createDialogAdd(Home_Activity.this);
}
});
}
}
DialogAdd
import android.content.Context;
import android.support.v7.app.AlertDialog;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
public class DialogAdd implements View.OnClickListener, Choose.Choose_Interface {
private LayoutInflater inflater;
private View view;
private Context context;
private AlertDialog dialog;
private String selectetSmile = null;
private ImageView ib_dialog_smile;
public DialogAdd(final Context context) {
this.context = context;
inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.dialog_line, null);
ib_dialog_smile = (ImageView) view.findViewById(R.id.ib_dialog_smile);
ib_dialog_smile.setOnClickListener(this);
}
public void createDialogAdd(Context context) {
AlertDialog.Builder builder = new AlertDialog.Builder(context);
inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
builder.setView(view);
dialog = builder.create();
dialog.getWindow().setBackgroundDrawableResource(android.R.color.transparent);
dialog.show();
}
@Override
public void onClick(View v) {
if(v.getId() == R.id.ib_dialog_smile){
Choose choose = new Choose(context);
choose.createDialogChoose();
}
}
@Override
public void selectedSmile(String smile) {
selectetSmile = smile;
if(selectetSmile.equals("oh")){
ib_dialog_smile.setImageResource(R.drawable.oh);
}else if(selectetSmile.equals("oh_nooo")){
ib_dialog_smile.setImageResource(R.drawable.oh_nooo);
}else if(selectetSmile.equals("oh_what")){
ib_dialog_smile.setImageResource(R.drawable.oh_what);
}else if(selectetSmile.equals("oh_yes")){
ib_dialog_smile.setImageResource(R.drawable.oh_yes);
}
}
}
选择
public class Choose {
public interface Choose_Interface {
void selectedSmile(String flag);
}
public Choose_Interface mCallback;
private View view;
private Context context;
private LayoutInflater inflater;
private AlertDialog dialog;
private ImageView oh, oh_nooo, oh_what, oh_yes;
public Choose(final Context context) {
this.context = context;
inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.choose, null);
mCallback = new DialogAdd(context);
oh = (ImageView) view.findViewById(R.id.oh);
oh.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mCallback.selectedSmile("oh");
Toast.makeText(context, "oh klicked", Toast.LENGTH_SHORT).show();
dialog.dismiss();
}
});
oh_nooo = (ImageView) view.findViewById(R.id.oh_nooo);
oh_nooo.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mCallback.selectedSmile("oh_nooo");
Toast.makeText(context, "oh_nooo klicked", Toast.LENGTH_SHORT).show();
dialog.dismiss();
}
});
oh_what = (ImageView) view.findViewById(R.id.oh_what);
oh_what.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mCallback.selectedSmile("oh_what");
Toast.makeText(context, "oh_what klicked", Toast.LENGTH_SHORT).show();
dialog.dismiss();
}
});
oh_yes = (ImageView) view.findViewById(R.id.oh_yes);
oh_yes.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mCallback.selectedSmile("oh_yes");
Toast.makeText(context, "oh_yes klicked", Toast.LENGTH_SHORT).show();
dialog.dismiss();
}
});
}
public void createDialogChoose() {
AlertDialog.Builder builder = new AlertDialog.Builder(context);
inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
builder.setView(view);
dialog = builder.create();
dialog.getWindow().setBackgroundDrawableResource(android.R.color.transparent);
dialog.show();
}
}
我尝试使用无效的ib_dialog_smile,我使用了ImageButton以及ImageView,AppCompatImageView,setImageDrawable,setImageResource ..但是图像没有得到更新。 有人可以帮我改变ib_dialog_smile图像吗?
谢谢!
答案 0 :(得分:0)
我认为你的代码有点复杂。您不需要单独的类来创建两个自定义AlertDialog。只需为每个对话框创建两个XML布局,并按照您想要的方式设置样式(在第一个XML布局中放置“选择的”ImageView和按钮,在第二个布局中放置四个图像)。
我们称之为layout_1(选择图标)和layout_2(四个选项)
然后在你的home_activity中对视图var中的那些进行膨胀,并将它们选为alertDialog视图,如下所示:
static defaultProps = {
disableVirtualization: false,
getItem: (data: any, index: number) => data[index],
...
完美地为我的投票和评论对话框。希望这对您有所帮助,并且代码可读。