我使用MPAndroidChart库创建了这个Android应用程序,它允许用户使用ArrayList创建一个快速的PieChart(我添加了一些截图,以便澄清)。
我的问题是我想允许用户从饼图(和数组列表)中删除一个条目,然后应该呈现饼图而不删除条目。
有什么方法可以通过允许用户长按一个项目来实现这一点,这会打开某种是/否对话框?
代码:
package com.example.quickpie;
public class Main extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final List<PieEntry> entries = new ArrayList<>();
final EditText editText = (EditText) findViewById(R.id.editText);
final EditText editText2 = (EditText) findViewById(R.id.editText2);
final EditText editText3 = (EditText) findViewById(R.id.editText3);
final Button button = (Button) findViewById(R.id.button);
final Button button2 = (Button) findViewById(R.id.button2);
final TextView textView = (TextView) findViewById(R.id.textView);
final TextView textView2 = (TextView) findViewById(R.id.textView2);
final TextView textView3 = (TextView) findViewById(R.id.textView3);
editText.requestFocus();
button.setOnClickListener(new View.OnClickListener() {
public void onClick (View v) {
//if(editText.getFreezesText())
if (editText.getText().toString().matches("")) {
Toast.makeText(getBaseContext(), "You did not enter a Valid Item ID", Toast.LENGTH_LONG).show();
return;
}
else if (editText2.getText().toString().matches("")) {
Toast.makeText(getBaseContext(), "You did not enter a Valid Quantitiy", Toast.LENGTH_LONG).show();
return;
}
else {
String nomen = (editText.getText().toString());
float number = Float.parseFloat(editText2.getText().toString());
final PieChart piechart = (PieChart) findViewById(R.id.piechart);
entries.add(new PieEntry(number, nomen));
editText.setText("");
editText2.setText("");
textView3.setSingleLine(false);
textView3.append("\n" + nomen + " " + number);
editText.requestFocus();
PieDataSet set = new PieDataSet(entries, "");
//colors
getResources().getColor(R.color.violp);
getResources().getColor(R.color.bluep);
getResources().getColor(R.color.redp);
getResources().getColor(R.color.greenp);
getResources().getColor(R.color.yellowp);
getResources().getColor(R.color.orangep);
getResources().getColor(R.color.lightbluep);
getResources().getColor(R.color.purplep);
getResources().getColor(R.color.darkredp);
set.setColors(new int[]{R.color.bluep, R.color.greenp, R.color.violp, R.color.redp, R.color.yellowp, R.color.orangep, R.color.lightbluep, R.color.purplep, R.color.darkredp}, getApplicationContext());
PieData data = new PieData(set);
piechart.setData(data);
Description description = new Description();
description.setText(getResources().getString(R.string.besch));
piechart.setDescription(description);
Toast.makeText(getBaseContext(), (R.string.loading),
Toast.LENGTH_SHORT).show();
String mitte = (editText3.getText().toString());
piechart.setCenterText(mitte);
piechart.setCenterTextSize(30);
piechart.setNoDataText(String.valueOf(R.string.nodata));
piechart.notifyDataSetChanged();
piechart.animateX(2000, Easing.EasingOption.EaseInExpo);
piechart.animateY(2000, Easing.EasingOption.EaseInExpo);
piechart.invalidate();
editText3.setVisibility(View.VISIBLE);
editText3.setOnEditorActionListener(new TextView.OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_DONE) {
String mitte = (editText3.getText().toString());
piechart.setCenterText(mitte);
editText3.setText("");
editText.requestFocus(); //editText3.setVisibility(View.INVISIBLE);
InputMethodManager imm = (InputMethodManager) getSystemService(Main.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(editText.getWindowToken(), 0);
//do something
}
return false;
}
});
editText3.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if (MotionEvent.ACTION_UP == event.getAction()) {
piechart.setCenterText("");
textView3.setHint(R.string.hint1);
}
return false; // return is important...
}
});
button.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
Toast.makeText(getBaseContext(), R.string.longpresshint, Toast.LENGTH_LONG).show();
return true;
}
});
final Button button2 = (Button) findViewById(R.id.button2);
button2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String mitte = (editText3.getText().toString());
piechart.setCenterText(mitte);
//piechart.setVisibility(View.VISIBLE);
//editText.setVisibility(View.INVISIBLE);
//textView2.setVisibility(View.INVISIBLE);
//textView3.setVisibility(View.INVISIBLE);
//editText3.setVisibility(View.INVISIBLE);
// editText2.setVisibility(View.INVISIBLE);
//button.setVisibility(View.INVISIBLE);
//button2.setVisibility(View.INVISIBLE);
Toast.makeText(getBaseContext(), (R.string.loading),
Toast.LENGTH_SHORT).show();
piechart.animateX(2000, Easing.EasingOption.EaseInExpo);
piechart.animateY(2000, Easing.EasingOption.EaseInExpo);
InputMethodManager imm = (InputMethodManager) getSystemService(Main.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(editText.getWindowToken(), 0);
}
});
}}
}
)
;
}
@Override
public void onBackPressed(){
super.onBackPressed();
Intent intent = getIntent();
finish();
startActivity(intent);
}
}
答案 0 :(得分:0)
请试试这个
button.setOnClickListener(new View.OnClickListener() {
public void onClick (View v) {
//if(editText.getFreezesText())
if (editText.getText().toString().matches("")) {
Toast.makeText(getBaseContext(), "You did not enter a Valid Item ID", Toast.LENGTH_LONG).show();
return;
}
else if (editText2.getText().toString().matches("")) {
Toast.makeText(getBaseContext(), "You did not enter a Valid Quantitiy", Toast.LENGTH_LONG).show();
return;
}
else {
piechart.invalidate();
}