這樣的寫法看似正確,但是getText()並不是回傳String的型別,因此需要加上toString()的方法強制轉型為String,將上面的範例改為正確如下:
Button btn = (Button) findViewById(R.id.btn);
String str = btn.getText().toString();
Button btn = (Button) findViewById(R.id.btn);
String str = btn.getText().toString();
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="帳號" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:password="true"
android:hint="密碼" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="你最喜歡的程式語言是什麼?" />
<AutoCompleteTextView
android:id="@+id/autoCompleteTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.*;
public class MainActivity extends Activity {
final String[] language = {"C","C++","C#","Java","VB","JSP","Android","ASP.NET","PHP","Python"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.select_dialog_item,language);
AutoCompleteTextView autoCompleteTextView = (AutoCompleteTextView) findViewById
(R.id.autoCompleteTextView);
autoCompleteTextView.setThreshold(1);
autoCompleteTextView.setAdapter(adapter);
}
}
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="你最喜歡的程式語言是什麼?" />
<MultiAutoCompleteTextView
android:id="@+id/multiAutoCompleteTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.*;
public class MainActivity extends Activity {
final String[] language = {"C","C++","C#","Java","VB","JSP","Android","ASP.NET","PHP","Python"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.select_dialog_item,language);
MultiAutoCompleteTextView multiAutoCompleteTextView = (MultiAutoCompleteTextView) findViewById
(R.id.multiAutoCompleteTextView);
multiAutoCompleteTextView.setThreshold(1);
multiAutoCompleteTextView.setAdapter(adapter);
multiAutoCompleteTextView.setTokenizer(new MultiAutoCompleteTextView.CommaTokenizer());
}
}
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="你最喜歡的程式語言是什麼?" />
<Spinner
android:id="@+id/spinner"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.*;
public class MainActivity extends Activity {
final String[] language = {"C","C++","C#","Java","VB","JSP","Android","ASP.NET","PHP","Python"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item,language);
Spinner spinner = (Spinner) findViewById(R.id.spinner);
spinner.setAdapter(adapter);
}
}
<Button
android:id="@+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button"
android:drawableLeft="@drawable/ic_launcher"
android:onClick="function" />
<ImageButton
android:id="@+id/imgBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher" />
<ToggleButton
android:id="@+id/toggleButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textOn="on"
android:textOff="off" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="喜歡的活動:" />
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="電影" />
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="旅遊" />
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="逛街" />
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="運動" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="請選擇性別:" />
<RadioGroup
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:text="男" />
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="女" />
</RadioGroup>
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.*;
import android.view.*;
import android.content.*;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btn = (Button) findViewById(R.id.btn);
btn.setOnClickListener(new View.OnClickListener(){
public void onClick(View view) {
Intent intent = new Intent(MainActivity.this,ActivityB.class);
startActivity(intent);
}
});
}
#include<stdio.h>
int main(void) {
printf("%s%ld\n","char:",sizeof(char));
printf("%s%ld\n","int:",sizeof(int));
printf("%s%ld\n","short:",sizeof(short));
printf("%s%ld\n","long:",sizeof(long));
printf("%s%ld\n","long int:",sizeof(long int));
printf("%s%ld\n","long long:",sizeof(long long));
printf("%s%ld\n","float:",sizeof(float));
printf("%s%ld\n","double:",sizeof(double));
printf("%s%ld\n","unsigned int:",sizeof(unsigned int));
printf("%s%ld\n","unsigned char:",sizeof(unsigned char));
printf("%s%ld\n","unsigned short:",sizeof(unsigned short));
printf("%s%ld\n","unsigned long:",sizeof(unsigned long));
printf("%s%ld\n","unsigned long long:",sizeof(unsigned long long));
return 0;
}
#include<stdio.h>
int main(void) {
printf("%d\n",10);
printf("%f\n",10.0);
printf("%c\n",'a');
return 0;
}
#include<stdio.h>
int main(void) {
printf("hello world");
return 0;
}
<AbsoluteLayout
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" >
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="10px"
android:layout_y="20px"
android:text="進入" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="380px"
android:layout_y="20px"
android:text="離開" />
</AbsoluteLayout>
<menu
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context="com.example.golden.lab.MainActivity" >
<item
android:id="@+id/action_help"
android:title="@string/action_help"
android:icon="@drawable/ic_action_help"
android:showAsAction="ifRoom" />
<item
android:id="@+id/action_settings"
android:orderInCategory="100"
android:title="@string/action_settings"
android:showAsAction="always" />
</menu>
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main, menu);
return true;
}
<menu
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context="com.example.golden.lab.MainActivity" >
<item
android:id="@+id/action_help"
android:title="@string/action_help"
android:icon="@drawable/ic_action_help"
android:showAsAction="always" />
<item
android:id="@+id/action_settings"
android:orderInCategory="100"
android:title="@string/action_settings"
android:showAsAction="never" />
</menu>
public class MainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId()) {
case R.id.action_help:
Help();
return true;
case R.id.action_settings:
return true;
default:
return super.onOptionsItemSelected(item);
}
}
private void Help() {
Intent intent = new Intent(this,Help.class);
startActivity(intent);
}
}
public class Help extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_help);
ActionBar actionBar = getActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.help, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId()) {
case R.id.action_settings:
return true;
case android.R.id.home:
NavUtils.navigateUpFromSameTask(this);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_help);
ActionBar actionBar = getActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setIcon(R.drawable.help);
}
<activity
android:name=".MainActivity"
android:label="Android"
android:logo="@drawable/help" >
</activity>
android:label是設定ActionBar的標題,android:logo是設定ActionBar的icon ,如果要統一設定每個Activity的ActionBar的標題和icon在AndroidManifest.xml的<application></application>進行設定,請參考下面的範例:
<application
android:allowBackup="true"
android:icon="@drawable/help"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
</application>
設定每個Activity的icon和標題分別是android:icon和android:label,很簡單吧!!
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_help);
ActionBar actionBar = getActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setIcon(R.drawable.help);
actionBar.hide();
}
<TableLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:stretchColumns="*" >
<TableRow>
<TextView
android:text="帳號:"
android:padding="10px" />
<EditText
android:padding="10px"
android:layout_span="2" />
</TableRow>
<TableRow>
<TextView
android:text="密碼:"
android:padding="10px" />
<EditText
android:padding="10px"
android:layout_span="2" />
</TableRow>
<TableRow>
<Button
android:text="登入" />
<Button
android:text="取消" />
<Button
android:text="註冊" />
</TableRow>
</TableLayout>
<TableLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:stretchColumns="0" >
<TableRow>
<TextView
android:text="帳號:"
android:padding="10px" />
<EditText
android:padding="10px"
android:layout_span="2" />
</TableRow>
<TableRow>
<TextView
android:text="密碼:"
android:padding="10px" />
<EditText
android:padding="10px"
android:layout_span="2" />
</TableRow>
<TableRow>
<Button
android:text="登入" />
<Button
android:text="取消" />
<Button
android:text="註冊" />
</TableRow>
</TableLayout>
<TableLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:shrinkColumns="*" >
<TableRow>
<TextView
android:text="帳號:"
android:padding="10px" />
<EditText
android:padding="10px"
android:layout_span="2" />
</TableRow>
<TableRow>
<TextView
android:text="密碼:"
android:padding="10px" />
<EditText
android:padding="10px"
android:layout_span="2" />
</TableRow>
<TableRow>
<Button
android:text="登入" />
<Button
android:text="取消" />
<Button
android:text="註冊" />
</TableRow>
</TableLayout>
<TableLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:collapseColumns="0" >
<TableRow>
<TextView
android:text="帳號:"
android:padding="10px" />
<EditText
android:padding="10px"
android:layout_span="2" />
</TableRow>
<TableRow>
<TextView
android:text="密碼:"
android:padding="10px" />
<EditText
android:padding="10px"
android:layout_span="2" />
</TableRow>
<TableRow>
<Button
android:text="登入" />
<Button
android:text="取消" />
<Button
android:text="註冊" />
</TableRow>
</TableLayout>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/label"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="輸入帳號和密碼:" />
<EditText
android:id="@+id/inputAccount"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/label"
android:hint="帳號" />
<EditText
android:id="@+id/inputPassword"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/inputAccount"
android:hint="密碼" />
<Button
android:id="@+id/btnCancel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/inputPassword"
android:layout_alignParentRight="true"
android:layout_marginLeft="10px"
android:text="取消" />
<Button
android:id="@+id/btnLogin"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toLeftOf="@+id/btnCancel"
android:layout_alignTop="@+id/btnCancel"
android:text="登入" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:text="註冊" />
</RelativeLayout>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="1" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="2" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="3" />
</LinearLayout>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="1" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="2" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="3" />
</LinearLayout>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<EditText
android:layout_weight="2"
android:layout_width="0dp"
android:layout_height="wrap_content" />
<Button
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="Submit" />
</LinearLayout>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
<EditText
android:layout_weight="2"
android:layout_width="0dp"
android:layout_height="wrap_content" />
<Button
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="Submit" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
<EditText
android:layout_weight="2"
android:layout_width="0dp"
android:layout_height="wrap_content" />
<Button
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="Submit" />
</LinearLayout>
</LinearLayout>