android - 如何在弹出对话框中保存 textview 值?
问题描述
我在弹出对话框中有 7 个文本视图和 7 个按钮。对话框弹出的背面有一个按钮,可以增加计数。
我的目标是选择对话屏幕上的任何按钮,并从对话屏幕背面的按钮增加计数,然后在对话屏幕上写入文本。
我可以在我选择的按钮旁边的文本视图中输入数据,但是当我再次打开 x 屏幕并想要为不同的按钮选择数据时,数据会丢失。
这是对话框屏幕后面的按钮(主 xml)
btn_yellow.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
view.startAnimation(buttonClick);
myDialog.setContentView(R.layout.layout_zikirsec);
TextView txtclose;
txtclose =(TextView) myDialog.findViewById(R.id.txtclose);
final TextView tv_1_sayi=(TextView)myDialog.findViewById(R.id.tv_1_sayi);
TextView tv_2_sayi=(TextView)myDialog.findViewById(R.id.tv_2_sayi);
TextView tv_3_sayi=(TextView)myDialog.findViewById(R.id.tv_3_sayi);
TextView tv_4_sayi=(TextView)myDialog.findViewById(R.id.tv_4_sayi);
TextView tv_5_sayi=(TextView)myDialog.findViewById(R.id.tv_5_sayi);
TextView tv_6_sayi=(TextView)myDialog.findViewById(R.id.tv_6_sayi);
TextView tv_7_sayi=(TextView)myDialog.findViewById(R.id.tv_7_sayi);
final String sayi1=String.valueOf(count);
txtclose.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
tv_1_sayi.setText(sayi1);
myDialog.dismiss();
}
});
if (tv_secilenzikir.getText().toString()=="mytext1"){
tv_1_sayi.setText(sayi1);
}
else if (tv_secilenzikir.getText().toString()=="mytext2"){
tv_1_sayi.setText(sayi1);
tv_2_sayi.setText(sayi1);
}
else if (tv_secilenzikir.getText().toString()=="mytext3"){
tv_3_sayi.setText(sayi1);
}
else if (tv_secilenzikir.getText().toString()=="mytext4"){
tv_4_sayi.setText(sayi1);
}
else if (tv_secilenzikir.getText().toString()=="mytext5"){
tv_5_sayi.setText(sayi1);
}
else if (tv_secilenzikir.getText().toString()=="mytext6"){
tv_6_sayi.setText(sayi1);
}
else if (tv_secilenzikir.getText().toString()=="mytext"){
tv_7_sayi.setText(sayi1);
}
myDialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
myDialog.show();
}
});
这是对话框屏幕中的 7 个按钮之一
public void btn_3_onclk(View view)
{
myDialog.setContentView(R.layout.layout_zikirsec);
tv_secilenzikir.setText("mytext3");
myDialog.dismiss();
TextView txtclose;
txtclose =(TextView) myDialog.findViewById(R.id.txtclose);
txtclose.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
myDialog.dismiss();
}
});
myDialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
};
解决方案
您应该创建一个自定义对话框,并将这些文本视图的状态保存在 ArrayList 中。之后,当要关闭对话框时获取此 arrayList,并且当您想再次显示时,请将此 arrayList 设置为对话框。
以下是一些可能有用的代码:
public class CustomDialog extends Dialog {
private ArrayList<String> states;
public CustomDialog(ArrayList<String> states) {
this.states = states;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
//Initialize your textViews from arrayList here
}
public ArrayList<String> getStates() {
return this.states;
}
}
第二种方法是将这些值保存在 sharedPrefs 中,并且当您想要实例化从 SharedPrefs 读取的对话框时。
推荐阅读
- html - 是否可以使用存储在 AWS S3 中的私有视频播放 HTML 视频?
- liquibase - SQLServer DB 上的 Liquibase generateChangeLog 在 INFORMATION_SCHEMA.CONSTRAINTS 上失败
- java - 计算特定时区中两个 ZonedDateTime 对象之间的天数?
- authentication - 使用 Azure Bot Bearer Token 向外部 API 发出 GET 请求
- android - 无法在移动设备上访问 javascript 控制台并且由于缺少计算机任何替代方案而无法使用 USB 调试?
- android - 在 Android 中使用签名 URL 从 Amazon S3 + CloudFront 播放 m3u8
- uwp - UWP 应用启动时 RuntimeBroker 中的异常
- git - 多个 github 帐户的权限被拒绝
- python - 无法登录 Django 管理页面
- tcl - 如何在 VHDL 中检查所有组件层次结构级别的信号值