亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何從另一個類為 TextView 設置文本?

如何從另一個類為 TextView 設置文本?

忽然笑 2021-08-25 15:10:17
我有一個警報對話框,我為此警報對話框使用了自定義布局,在這個自定義布局中,我有一個 TextView,那么如何從 MainActivity 類中為這個 TextView 設置文本?這是我的代碼:class MainActivity : AppCompatActivity() {        override fun onCreate(savedInstanceState: Bundle?) {            super.onCreate(savedInstanceState)            setContentView(R.layout.activity_main)            var btn_ShowAlert =findViewById<Button>(R.id.Button)            btn_ShowAlert.setOnClickListener {                       txtlyric.text ="this Textview is all the problem xD "                val dialog = AlertDialog.Builder(this)                val dialogView = layoutInflater.inflate(R.layout.lyric,null)                dialog.setView(dialogView)                dialog.setCancelable(true)                dialog.show()                            }    }
查看完整描述

2 回答

?
牛魔王的故事

TA貢獻1830條經驗 獲得超3個贊

我建議使用 DialogFragment 并將必要的值傳遞給構造函數


public class MyAlertDialogFragment extends DialogFragment {


    public static final String TITLE = "dataKey";


    public static MyAlertDialogFragment newInstance(String dataToShow) {

        MyAlertDialogFragment frag = new MyAlertDialogFragment();

        Bundle args = new Bundle();

        args.putString(TITLE, dataToShow);

        frag.setArguments(args);

        return frag;

    }


    @Override

    public Dialog onCreateDialog(Bundle savedInstanceState) {

        String mDataRecieved = getArguments().getString(TITLE,"defaultTitle");


        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());

        LayoutInflater inflater = getActivity().getLayoutInflater();

        View view = inflater.inflate(R.layout.alert_layout, null);


        TextView mTextView = (TextView) view.findViewById(R.id.textview);

        mTextView.setText(mDataRecieved);

        setCancelable(false);


        builder.setView(view);

        Dialog dialog = builder.create();


        dialog.getWindow().setBackgroundDrawable(

                new ColorDrawable(Color.TRANSPARENT));


        return dialog;


    }

}


查看完整回答
反對 回復 2021-08-25
  • 2 回答
  • 0 關注
  • 216 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號