Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/377.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何通过单击更改文本视图中的文本?_Java_Android - Fatal编程技术网

Java 如何通过单击更改文本视图中的文本?

Java 如何通过单击更改文本视图中的文本?,java,android,Java,Android,我有一个通过单击按钮并选择颜色来添加任务的代码。 我希望在添加任务后,长时间单击以更改其内容,然后打开带有edittext的对话框。 我该怎么做? 对不起,我的英语不好 LinearLayout container; EditText inputText; TextView txt; RadioGroup colorPick; int checkedColor; //Counter int i=1; int id=1; //Colors int red = Color.parseColor("#

我有一个通过单击按钮并选择颜色来添加任务的代码。 我希望在添加任务后,长时间单击以更改其内容,然后打开带有edittext的对话框。 我该怎么做? 对不起,我的英语不好

LinearLayout container;
EditText inputText;
TextView txt;
RadioGroup colorPick;
int checkedColor;
//Counter
int i=1;
int id=1;
//Colors
int red = Color.parseColor("#EF9A9A");
int blue = Color.parseColor("#90CAF9");
int white = Color.parseColor("#FFFFFF");

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    inputText = (EditText)findViewById(R.id.inputText);
    container=(LinearLayout)findViewById(R.id.container);
    colorPick=(RadioGroup)findViewById(R.id.colorPick);
    colorPick.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {
            switch (checkedId){
                case R.id.colorBlue:
                    checkedColor=blue;

                    break;
                case R.id.colorRed:
                    checkedColor=red;
                    break;
                case R.id.colorWhite:
                    checkedColor=white;
                    break;
                default:
                    checkedColor=white;
            }
        }
    });


}
public void add(View v){

    final String input=inputText.getText().toString();
    txt = new TextView(this);

    if(input.matches("")){
        inputText.setBackgroundColor(-65536);
    }else{
        txt.setHeight(50);
        txt.setText(i+". "+input);
        txt.setBackgroundColor(checkedColor);
        i++;
        container.addView(txt);
        colorPick.clearCheck();
        inputText.setText("");
        //Add a edit option
        txt.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                id=txt.getId();
                dialogCustom();
                return false;
            }
        });

    }



}
public void remove(View v){
    container.removeAllViews();
}
public void removeEditColor(View v){
    inputText.setBackgroundColor(0x00000000);
}
private void dialogCustom(){
    AlertDialog.Builder editTxtDialog = new AlertDialog.Builder(this);
    LinearLayout layout = new LinearLayout(this);
    layout.setOrientation(LinearLayout.VERTICAL);
    EditText input1 = new EditText(this);
    editTxtDialog.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {

        }
    });
    input1.setHint("Change the task..");
    layout.addView(input1);
    editTxtDialog.setView(layout);
    editTxtDialog.show();
}

将参数添加到对话框创建方法中,以便可以设置文本:

private void dialogCustom(final TextView v) {
    AlertDialog.Builder editTxtDialog = new AlertDialog.Builder(this);
    LinearLayout layout = new LinearLayout(this);
    layout.setOrientation(LinearLayout.VERTICAL);
    EditText input1 = new EditText(this);
    editTxtDialog.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            v.setText("Some text");
        }
    });
    input1.setHint("Change the task..");
    layout.addView(input1);
    editTxtDialog.setView(layout);
    editTxtDialog.show();
}
并按如下方式调用该方法:

public void add(View v){
    final String input = inputText.getText().toString();
    txt = new TextView(this);

    if(input.matches("")){
        inputText.setBackgroundColor(-65536);
    } else{
        txt.setHeight(50);
        txt.setText(i+". "+input);
        txt.setBackgroundColor(checkedColor);
        i++;
        container.addView(txt);
        colorPick.clearCheck();
        inputText.setText("");
        //Add a edit option
        txt.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                id=txt.getId();
                dialogCustom((TextView) v);
                return false;
            }
        });
    }
}

看看这个:非常感谢你,你帮了我很多!