package io.github.neelkamath.happylist;
import android.content.Context;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RelativeLayout;
import android.widget.TextView;
import java.util.Set;
public class MainActivity extends AppCompatActivity {
int idCount; // Used to give unique IDs to Views.
EditText newActivityEditText;
SharedPreferences neededSharedPref;
SharedPreferences wantedSharedPref;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
idCount = 0;
newActivityEditText = findViewById(R.id.newActivityEditText);
neededSharedPref = getSharedPreferences("needed", Context.MODE_PRIVATE);
wantedSharedPref = getSharedPreferences("wanted", Context.MODE_PRIVATE);
newActivityEditText.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
boolean isExisting = false;
if (neededSharedPref.contains(charSequence.toString())
|| wantedSharedPref.contains(charSequence.toString())) {
isExisting = true;
}
Button create = findViewById(R.id.createButton);
create.setText(isExisting ? R.string.exists : R.string.create);
create.setEnabled(!charSequence.toString().equals("") || isExisting);
}
@Override
public void afterTextChanged(Editable editable) {
}
});
addActivities(true);
addActivities(false);
}
public void createActivity(View view) {
boolean isNeeded = ((RadioButton) findViewById(R.id.neededRadioButton)).isChecked();
SharedPreferences.Editor e = isNeeded ? neededSharedPref.edit() : wantedSharedPref.edit();
e.putBoolean(newActivityEditText.getText().toString(), false);
e.apply();
newActivityEditText.setText("");
}
void addActivities(boolean isNeeded) {
SharedPreferences sharedPreferences = isNeeded ? neededSharedPref : wantedSharedPref;
Set<String> strings = sharedPreferences.getAll().keySet();
int id = isNeeded ? R.id.neededRelativeLayout : R.id.wantedRelativeLayout;
RelativeLayout layout = findViewById(id);
TextView prevTextView = null;
CheckBox prevCheckBox = null;
for (String s : strings) {
TextView textView = new TextView(this);
textView.setText(s);
textView.setId(++idCount);
RelativeLayout.LayoutParams txtParams = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT
);
txtParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
CheckBox checkBox = new CheckBox(this);
checkBox.setChecked(sharedPreferences.getBoolean(s, false));
checkBox.setId(++idCount);
RelativeLayout.LayoutParams checkBoxParams = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT
);
checkBoxParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
if (prevTextView != null && prevCheckBox != null) {
txtParams.addRule(RelativeLayout.BELOW, prevTextView.getId());
checkBoxParams.addRule(RelativeLayout.BELOW, prevCheckBox.getId());
}
layout.addView(textView, txtParams);
layout.addView(checkBox, checkBoxParams);
prevTextView = textView;
prevCheckBox = checkBox;
}
}
}
addActivities
方法会添加RelativeLayout
,但在向其添加新TextView
时,布局不会调整大小。我也尝试自己设置布局的高度,但我无法找到计算高度的数据(只有TextView.getLayoutParams().getLineHeight()
返回一个正整数,而且不是整个TextView的实际高度) 。我搜索了2个小时,没有任何效果。