我有80个按钮,用户必须能够通过长按事件重命名并保存新的按钮文本。当用户关闭应用程序时,它必须保持相同的文本,并且不能返回默认文本。这就是我对第一个按钮的看法。我将如何做到这一点,以便它知道长按哪个按钮并重命名。 (这个编码在我外出时不会保存它)
public class MainActivity extends AppCompatActivity {
private Button btn1 , btn2 , btn3 , btn4 ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
View.OnLongClickListener listener = new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
showPopup(v);
return true; //Nothing shows error, but this part where i tried to set a onLongClick listener.
}
};
btn1.setOnLongClickListener(listener);
btn2.setOnLongClickListener(listener);
btn3.setOnLongClickListener(listener);
btn4.setOnLongClickListener(listener);
SharedPreferences prefs = getSharedPreferences("btnID", Context.MODE_PRIVATE);
Map<String,?> keys = prefs.getAll();
for(Map.Entry<String,?> entry : keys.entrySet()){
Button button = (Button) findViewById(Integer.parseInt(entry.getKey()));
button.setText(entry.getValue().toString());
}
}
private void showPopup(View v) {
//here you have your button
final Button currentButton = (Button)v;
v.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
showPopup(v);
return true;
}
});
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Rename to?");
View view = LayoutInflater.from(this).inflate(R.layout.dialog_view, null);
final EditText edit_dialog = (EditText)view.findViewById(R.id.edit_dialog);
edit_dialog.setText(currentButton.getText().toString());
builder.setView(view);
builder.setPositiveButton("confirm", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
currentButton.setText(edit_dialog.getText().toString());
SharedPreferences prefs = getSharedPreferences("btnID", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = prefs.edit();
editor.putString(String.valueOf(currentButton.getId()), currentButton.getText().toString());
editor.apply();
}
});
builder.show();
}
}
答案 0 :(得分:0)
您还必须传递按钮的ID,例如:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn1 = (Button) findViewById(R.id.btn1);
btn2 = (Button) findViewById(R.id.btn2);
btn3 = (Button) findViewById(R.id.btn3);
btn4 = (Button) findViewById(R.id.btn4);
//HERE you load the button texts from SharedPrefs or SQLite and you set the text to the buttons from ids
btn1.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
showDialog("", v.getId());
return true;
}
});
}
private void showDialog(String str, int btnId) {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Rename to?");
View view = LayoutInflater.from(this).inflate(R.layout.dialog_view, null);
final EditText edit_dialog = (EditText) view.findViewById(R.id.edit_dialog);
edit_dialog.setText(str);
builder.setView(view);
builder.setPositiveButton("confirm", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
btn1.setText(edit_dialog.getText().toString());
//HERE you save the text and the relative id.
//you can use both SharedPrefs or SQLite
}
});
builder.show();
}
检查注释以查看何时保存/加载数据
您必须自定义onClick方式以检索单击按钮的ID。您可以保存在sharedPreferences
中private void showDialog(View v) {
//here you have your button
Button currentButton = (Button)v;
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Rename to?");
View view = LayoutInflater.from(this).inflate(R.layout.dialog_view, null);
final EditText edit_dialog = (EditText)view.findViewById(R.id.edit_dialog);
edit_dialog.setText(currentButton.getText().toString());
builder.setView(view);
builder.setPositiveButton("confirm", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
currentButton.setText(edit_dialog.getText().toString());
SharedPreferences prefs = this.getSharedPreferences("sharedPrefName", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = prefs.edit();
editor.putString(String.valueOf(currentButton.getId), currentButton.getText().toString());
editor.apply();
}
});
builder.show();
}
你可以在xml中使用onclick调用这个方法:
<Button
android:id="@+id\myId"
android:layout-width="wrap-content"
android:layout-height="wrap-content"
android:onclick="showDialog"
/>
通过这种方式,您可以为每个按钮使用单一方法。
PS:
你必须在onCreate
中添加一些内容来实现按钮的初始化:
SharedPreferences prefs = this.getSharedPreferences("sharedPrefName", Context.MODE_PRIVATE);
Map<String,?> keys = prefs.getAll();
for(Map.Entry<String,?> entry : keys.entrySet()){
Button button = findViewById(Integer.Parse(entry.getHey()));
button.setText(entry.getValue().toString());
}
很抱歉,如果它有任何错误,但我手写了,所以我没有编译器。
希望这有帮助
答案 1 :(得分:0)
我也遇到了与@Bernard相同的问题。然后我做了一些研究并将所有ID的按钮存储在一个数组中。我刚刚对@Pier Giorgio Misley给出的解决方案做了一些修改。非常感谢Misley的帮助。我可以用你的解决方案来解决这个问题。
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home_page);
setText();
buttons = new ArrayList<Button>(BUTTON_IDS.length);
SharedPreferences prefs = getSharedPreferences("btnID", Context.MODE_PRIVATE);
Map<String,?> keys = prefs.getAll();
for(Map.Entry<String,?> entry : keys.entrySet()){
Button button = (Button) findViewById(Integer.parseInt(entry.getKey()));
button.setText(entry.getValue().toString());
}
for(int id : BUTTON_IDS) {
Button button = (Button)findViewById(id);
button.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
renameButton("",v);
return true;
}
});
}
}
private void renameButton(String str,View v) {
final Button currentButton = (Button)v;
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Rename to?");
View view = LayoutInflater.from(this).inflate(R.layout.dialog_view, null);
final EditText edit_dialog = (EditText)view.findViewById(R.id.edit_text);
edit_dialog.setText(str);
builder.setView(view);
builder.setPositiveButton("confirm", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
currentButton.setText(edit_dialog.getText().toString());
SharedPreferences prefs = getSharedPreferences("btnID", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = prefs.edit();
editor.putString(String.valueOf(currentButton.getId()), currentButton.getText().toString());
editor.apply();
}
});
builder.show();
}
在onCreate方法
上创建一个按钮ID数组 private List<Button> buttons;
private static final int[] BUTTON_IDS = {
R.id.btn1,
R.id.btn2,
R.id.btn3,
R.id.btn4,
};