Textview的文字不变

时间:2019-09-25 16:30:13

标签: android textview

因此,我创建了一个方法,每当单击肯定按钮时就调用它,但它不会更改TextView中的文本。我已经尝试过runOnUiThread()并更改ID,但没有一个起作用。 我一直在努力寻找解决方案,但很抱歉,如果这是一个愚蠢的问题,但我正在学习,Android可能非常愚蠢和烦人,专门为像我这样的新手程序员开发。

XML:

<ImageView
        android:id="@+id/imageView5"
        android:layout_width="50dp"
        android:layout_height="50dp"
        app:srcCompat="@drawable/friendly" />

    <TextView
        android:id="@+id/whatmyname"
        android:layout_width="150dp"
        android:layout_height="wrap_content"
        android:onClick="dialog"
        android:text="What's your name?" />

Java:

public class MainActivity extends AppCompatActivity {


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        if (Build.VERSION.SDK_INT >= 21)
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);

        startActivity(new Intent(this, Intro.class));


        Toolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        getSupportActionBar().setTitle("");


        DrawerLayout drawerLayout = findViewById(R.id.drawer_layout);
        ActionBarDrawerToggle drawerToggle = new ActionBarDrawerToggle(this, drawerLayout, toolbar, R.string.open_drawer, R.string.close_drawer);

        drawerLayout.addDrawerListener(drawerToggle);
        drawerToggle.syncState();







    }

    public void dialog(View v) {


        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        LayoutInflater inflatere = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
        LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
        final View view = inflatere.inflate(R.layout.ask_name_dialog,null);
        builder.setView(view);
        final View contentView = inflater.inflate(R.layout.menu_header, null);



        builder.create();
                builder.setPositiveButton("Ok", new DialogInterface.OnClickListener() {

                    EditText ask_name = view.findViewById(R.id.edittext);
                    public void onClick(DialogInterface dialog, int id) {
                        final TextView header_menu_name = contentView.findViewById(R.id.whatmyname);
                        String ask_name_text = ask_name.getText().toString();
                        String Empty = "";
                        if(Empty.equals(ask_name_text.trim()) || ask_name_text == null)
                            Toast.makeText(getApplicationContext(),"Invalid",Toast.LENGTH_SHORT).show();
                        else
                            header_menu_name.setText(ask_name_text);
                    }
                })
                        .setNegativeButton("no", new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int id) {
                                // User cancelled the dialog
                            }
                        });

        builder.show();



    }

1 个答案:

答案 0 :(得分:0)

由于没有人回答,所有人都拒绝了投票,因此我更改了代码,找到了解决方案。 我删除了标题视图 ,并在更改后 添加了它 ,一切都很好。

final NavigationView navigationView = findViewById(R.id.navigation);
runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    TextView header_menu_name = contentView.findViewById(R.id.whatmyname);
                                    header_menu_name.setText(ask_name_text);
                                    navigationView.removeHeaderView(navigationView.getHeaderView(0));
                                    navigationView.addHeaderView(contentView);
                                }
                            });