小编Rah*_*are的帖子

如何替换片段中textView的文本

我想在单击按钮时更改 TextView 的文本。我的片段中有 2 个按钮,并且都有一个onClick用于相同 TextView的方法。这是我的片段中发生的事情的详细信息。

我将默认文本设置为 TextView 表示第一个文本。单击第一个按钮时,第二个文本将出现在 TextView 上。当第二个按钮再次单击时,第一个文本将出现在 TextView 上。

这是我的片段代码

@Override
public void onCreate(Bundle savedInstanceState) {
    Log.d("Rahul", "On Ganpati 1 fragment On Create ");
    super.onCreate(savedInstanceState);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
    Log.d("Rahul", "On Ganpati 1 fragment On Create View ");
    // Inflate the layout for this fragment

    final TextView textView = (TextView)getView().findViewById(R.id.g1TextView);
    textView.setText(getString(R.string.Ganpati_1));

    Button marathi = (Button)getView().findViewById(R.id.g1Marathi);
    marathi.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            textView.setText(getString(R.string.Ganpati_1));
        }
    }); …
Run Code Online (Sandbox Code Playgroud)

android textview android-fragments onclicklistener

2
推荐指数
1
解决办法
3013
查看次数