共有設定を使用していますが、コードが機能しません。何が悪いのかわからない。何か間違ったことを実装していますか?
私の主なJavaアクティビティ(関連するコードのビット)は次のとおりです。
public class MainActivity extends AppCompatActivity {
private String s;
public static final String subjectKey = "SubjectID";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final SharedPreferences sharedPreferences = getSharedPreferences(subjectKey, Context.MODE_PRIVATE);
TextView calc_monday = (TextView) findViewById(R.id.monday_calc);
calc_monday.setOnClickListener(
new Button.OnClickListener(){
public void onClick(View v){
CustomDialogClass cdd = new CustomDialogClass(MainActivity.this);
cdd.show();
TextView text1 = (TextView) cdd.findViewById(R.id.Subject_ID);
String text = sharedPreferences.getString(subjectKey, " ");
if(text != " ")
{
text1.setText(text); /* Edit the value here*/
}
TextView text2 = (TextView) cdd.findViewById(R.id.Room_ID);
text2.setText("6 (SEECS)");
TextView text3 = (TextView) cdd.findViewById(R.id.Time_ID);
text3.setText("09:00am - 09:50am");
}
}
);
calc_monday.setOnLongClickListener(
new Button.OnLongClickListener() {
public boolean onLongClick(View v) {
SettingDialogClass SDC = new SettingDialogClass(MainActivity.this);
SDC.show();
EditText texii = (EditText) SDC.findViewById(R.id.set_Subject_ID);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString(subjectKey, texii.getText().toString());
editor.apply();
return true;
}
}
);
基本的には、テキスト ボックス (calc_monday) をロングクリックすると、ダイアログ ボックスが表示されるようにしたいと考えています。これで、このダイアログ ボックスに表示される EditText フィールドに何かを書き込むことができるはずです。私が書いたものはすべて保存され、同じテキストボックスをシングルクリックすると表示されます(calc_monday)
理解するには、onClick と onLongClick の 2 つのメソッドを参照してください。
コードが機能していません。つまり、テキストボックスをシングルクリックしても、onLongCLick ダイアログボックスの EditText フィールドに書いたテキストが表示されません。
コードの何が問題なのですか