Alter text textbox Android

  • Replies:7
  • OpenNot stickiedUnanswered

Aug 27, 2013 2:41:26 PM

i am not sure about ur problem but still see if it can help u

Activity Class

import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends Activity implements OnClickListener {
WifiManager wifiManager;
ConnectivityManager mgr;
Button b;
TextView t;
private BluetoothAdapter mBluetoothAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
t = (TextView) findViewById(R.id.textView1);
b = (Button) findViewById(R.id.button1);
b.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch(v.getId()){
case R.id.button1:{
t.setText("text Changed");
}
}

}
}


XMl CLass

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >

<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />

<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView1"
android:layout_below="@+id/textView1"
android:layout_marginLeft="54dp"
android:layout_marginTop="36dp"
android:text="Button" />

</RelativeLayout>

Aug 27, 2013 8:38:32 PM

Now I'm with another doubt.

How can I use IF and ELSE where to receive the letter A Toast by bluetooth exibr one, is equally the letter B shows another Toast. My code does not work.

I am so my code

1byte[] msgBuffer = new byte[1];
2
3int bytes = inStream.read(msgBuffer);
4
5if(new String(msgBuffer) == "a")
6{
7 Toast.makeText(getApplicationContext(), "OK, 0).show();
8}
9else if(new String(msgBuffer) == "b")
10{
11 Toast.makeText(getApplicationContext(), "Error", 0).show();
12}

I did a test using a Toast, where it displays What did this cendo received by bluetooth, and it usually receives, I can receive the letter A and the letter B normally, but the IF and ELSE What do not compare this being received by bluetooth.

Code Test

1Toast.makeText(getApplicationContext(), new String(msgBuffer), 0).show();

Thanks

— modified on Aug 27, 2013 8:39:11 PM

Sep 3, 2013 11:22:25 AM

Pixelayer
How can I change the text of a textbox when clicking a button?

Hello..



I am agree with Christian Hempe that in java you have to compare the value of an String with the function ".equals()" and not with "==".