簡體   English   中英

如何通過單擊更改文本視圖中的文本?

[英]How can i change the text in text view by click?

我有一個通過單擊按鈕並選擇顏色來添加任務的代碼。
我希望在添加任務后通過長單擊打開帶有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;
            }
        });
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM