您好,登錄后才能下訂單哦!
這篇文章給大家分享的是有關Android中Activity和Intent怎么用的內容。小編覺得挺實用的,因此分享給大家做個參考,一起跟隨小編過來看看吧。
界面activity_main.xml
<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="com.example.activity02.MainActivity" > <!-- <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/hello_world" /> --> <EditText android:id="@+id/usernameEditText" android:layout_width="fill_parent" android:layout_height="wrap_content" android:hint="username"/> <Button android:id="@+id/myButton" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_below="@+id/usernameEditText"/> <Button android:id="@+id/sendButton" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_below="@+id/myButton"/> </RelativeLayout>
界面otheractivity.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <TextView android:id="@+id/myTextView" android:layout_width="fill_parent" android:layout_height="wrap_content" /> </LinearLayout>
MainActivity.java
public class MainActivity extends ActionBarActivity { private Button myButton; private Button sendButton; private EditText usernameEditText; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); myButton = (Button) findViewById(R.id.myButton); myButton.setText("傳值"); sendButton = (Button) findViewById(R.id.sendButton); sendButton.setText("發短信"); usernameEditText = (EditText) findViewById(R.id.usernameEditText); myButton.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { //Intent intent = new Intent(MainActivity.this, OtherActivity.class); Intent intent = new Intent(); intent.setClass(MainActivity.this, OtherActivity.class); String username = usernameEditText.getText().toString(); intent.putExtra("username", username); MainActivity.this.startActivity(intent); //startActivity(intent); } }); sendButton.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { //調用發端信的程序 Uri uri = Uri.parse("smsto:18513430780"); Intent intent = new Intent(Intent.ACTION_SENDTO, uri); intent.putExtra("sms_body", "The SMS text"); startActivity(intent); } }); } }
OtherActivity.java
public class OtherActivity extends Activity{ private TextView myTextView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); Toast.makeText(getApplicationContext(), "click", Toast.LENGTH_SHORT).show(); setContentView(R.layout.otheractivity); myTextView = (TextView) findViewById(R.id.myTextView); String username = getIntent().getStringExtra("username"); myTextView.setText("傳過來的值是:" + username); } }
感謝各位的閱讀!關于“Android中Activity和Intent怎么用”這篇文章就分享到這里了,希望以上內容可以對大家有一定的幫助,讓大家可以學到更多知識,如果覺得文章不錯,可以把它分享出去讓更多的人看到吧!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。