如何从其他活动中读取SharedPreferences值?
当我选择哪个活动首先开始工作时,我有setup.calss ...
如何从其他活动中阅读偏好?
final SharedPreferences settings = getPreferences(MODE_PRIVATE);
int choice = settings.getInt("language", -1);
Run Code Online (Sandbox Code Playgroud)
打开Dialog 3活动:
String[] activities = { "Activity 1", "Activity 2", "Activity 3" };
Run Code Online (Sandbox Code Playgroud)
的OnClick:
@Override
public void onClick(DialogInterface dialog, int which) {
SharedPreferences.Editor editor = settings.edit();
editor.putInt("language", which);
editor.commit();
launchActivity(which);
}
}).show();
} else {
// start the activity and close this activity
launchActivity(choice);
}
}
Run Code Online (Sandbox Code Playgroud) 大家好,为什么这段代码不起作用?我让 TextView 从 sqlite 数据库获取值,我想检查它是否为空隐藏 TextView。
mTel1 = (TextView) findViewById(R.id.tv_tel1);
String ed_text = mTel1.getText().toString().trim();
if(ed_text.length() == 0 || ed_text.equals("") || ed_text == null)
{
mTel1.setVisibility(View.VISIBLE);
}
else
{
mTel1.setVisibility(View.GONE);
}
Run Code Online (Sandbox Code Playgroud)
XML
<TextView
android:id="@+id/tv_tel1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="3dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_marginTop="3dp"
android:background="@drawable/border"
android:clickable="true"
android:padding="10dp"
android:textColor="#0066cc"
android:textSize="18sp" />
Run Code Online (Sandbox Code Playgroud)