0

当我去运行应用程序时,一切似乎都很好,但是当我按下开始按钮时,它不显示 Hello 我什至尝试在线程之前设置文本,但它仍然没有工作。为什么会发生这种情况?

这是代码:

public class MainActivity extends Activity implements OnClickListener {
Handler mHandler;
Button enter;
Button start;

文本视图显示;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    enter = (Button)findViewById(R.id.enter);
    start = (Button)findViewById(R.id.start);
    display =(TextView)findViewById(R.id.Display);

    mHandler = new Handler(){
        public void handleMessage(Message msg) {
            // TODO Auto-generated method stub
            super.handleMessage(msg);
            Bundle bundle = msg.getData();
            String string = bundle.getString("myKey");
            display.setText(string);

        }
    };
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

@Override
public void onClick(View v) {
    // TODO Auto-generated method stub
    switch(v.getId()){
    case R.id.enter:
        break;
    case R.id.start:
         Thread setText = new Thread(){

            @Override
            public void run() {
                // TODO Auto-generated method stub
                super.run();
                Message msg= Message.obtain();
                Bundle bundle = new Bundle();
                String dateString;
                dateString = "Hello";
                bundle.putString("myKey", dateString);
                msg.setData(bundle);
                mHandler.sendMessage(msg);
            }



        };
        setText.start();
        break;
    }
}

}
4

1 回答 1

1

您没有在 onCreate() 方法的输入/开始按钮上注册侦听器。只需调用 fe:

enter.setOnClickListener(this);
于 2013-06-26T14:53:17.137 回答