当我在文件中保存数据时,如何启动所需的Activity?

时间:2016-02-12 12:38:40

标签: java android file

当app首先启动时,使用两个按钮启动Activity:Create File and Settings。然后,如果我单击按钮创建文件 - 启动活动,我写文本并保存在文件中。 如果我们在文件保存时出错 - 用两个按钮启动活动。否则,使用三个按钮启动另一个Activity:Look file,Edit File,Settings。

如果文件已经保存,如何组织此活动与如何使用三个按钮启动活动之间的正确转换?

public class MainWithFileActivity extends AppCompatActivity {

public static final String MY_SETTINGS = "MY_SETTINGS";

private Button lookFile;
private Button editFile;
private Button settings;

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

    lookFile = (Button) findViewById(R.id.look_file);
    settings = (Button) findViewById(R.id.settings);
    editFile = (Button) findViewById(R.id.edit_file);

    lookFile.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(MainWithFileActivity.this, LookFileActivity.class);
            startActivity(intent);
        }
    });

    editFile.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(MainWithFileActivity.this,CreateFileActivity.class);
            startActivity(intent);
        }
    });

    settings.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(MainWithFileActivity.this, SettingActivity.class);
            startActivity(intent);
        }
    });

}
}

MainWithFileActivity

#ifWinActive ahk_class AcrobatSDIWindow
~LButton::
now := A_TickCount
while GetKeyState("LButton", "P")
    continue
if (A_TickCount-now > 600 )
{   
    Send ^c
    copied := true
}
return

OnClipboardChange:
if !copied
    return

copied := false
ToolTip % Clipboard := RegExReplace(Clipboard, "\r\n", " ")
SetTimer, ToolTipOff, -1000
return

ToolTipOff:
ToolTip
return

1 个答案:

答案 0 :(得分:0)

只需检查文件是否存在。如果没有,请隐藏lookFile按钮

MainWithFileActivity onCreate方法

中添加此内容
File f = new File(CreateFileActivity.FILE_NAME);
if(!f.exists() && !f.isDirectory()) {
    lookFile.setVisibility(View.GONE);
}