這篇文章可以解決以及瞭解下列問題:
1. 如何從Dialog取得回傳值2. 如何在兩個Activity之間傳遞訊息
3. 如何使用startActivityForResult
4. 如何使用setResult
5. 如何在Dialog做出等寬的按鈕
5. 如何在Dialog做出等寬的按鈕
假設一個情境:某個MainActivity要叫出一個Login的對話框。使用者輸入後,從Login對話框中取得nickname以及password然後在MainActivity進行登入動作。
※這個方法是用Activity假裝Dialog來取得資料
首先先撰寫LoginActivity部份
import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.view.ViewGroup.LayoutParams; import android.widget.Button; import android.widget.EditText; public class LoginActivity extends Activity { // UI private EditText nickname; private EditText password; private Button okBtn; private Button cancelBtn; /** Called when the activity is first created. */ @Override public void onCreate(Bundle icicle) { super.onCreate(icicle); setContentView(R.layout.login_dialog); // Width to Fill Parent LayoutParams params = getWindow().getAttributes(); params.width = LayoutParams.FILL_PARENT; getWindow().setAttributes((android.view.WindowManager.LayoutParams) params); // UI Assign nickname = (EditText) findViewById(R.id.nickname); password = (EditText) findViewById(R.id.password); okBtn = (Button) findViewById(R.id.okBtn); cancelBtn = (Button) findViewById(R.id.cancelBtn); okBtn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View arg0) { // make intent Intent intent = new Intent(); intent.putExtra("nickname", nickname.getText().toString()); intent.putExtra("password", password.getText().toString()); // setResult(RESULT_OK, Intent) LoginActivity.this.setResult(RESULT_OK ,intent); // close activity LoginActivity.this.finish(); } }); cancelBtn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View arg0) { LoginActivity.this.setResult(RESULT_CANCELED); LoginActivity.this.finish(); } }); } }
然後是layout
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical"> <TextView android:layout_height="wrap_content" android:layout_width="fill_parent" android:layout_marginLeft="20dip" android:layout_marginRight="20dip" android:text="@string/nickname_lbl"> </TextView> <EditText android:id="@+id/nickname" android:layout_height="wrap_content" android:layout_width="fill_parent" /> <TextView android:layout_height="wrap_content" android:layout_width="fill_parent" android:layout_marginLeft="20dip" android:layout_marginRight="20dip" android:text="@string/password_lbl"> </TextView> <EditText android:id="@+id/password" android:layout_height="wrap_content" android:layout_width="fill_parent" /> <TableLayout android:id="@+id/row1" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="1"> <TableRow android:layout_weight="1"> <Button android:id="@+id/okBtn" android:layout_width="0dip" android:layout_height="wrap_content" android:text="@string/ok_lbl" android:layout_weight="1" /> <Button android:id="@+id/cancelBtn" android:layout_width="0dip" android:layout_height="wrap_content" android:text="@string/cancel_lbl" android:layout_weight="1" /> </TableRow> </TableLayout> </LinearLayout>
接下來開始寫主程式
public class MainActivity extends Activity { // Sub Activity Code protected static final int LOGIN_ACTIVITY_CODE = 1; private Button connectBtn; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); connectBtn = (Button) findViewById(R.id.connectBtn); connectBtn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View arg0) { // Start Login Activity Intent intent = new Intent(MainActivity.this, LoginActivity.class); startActivityForResult(intent, LOGIN_ACTIVITY_CODE); } } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { if (requestCode == LOGIN_ACTIVITY_CODE) { if (resultCode == RESULT_OK) { // Log Log.d(TAG, "Get Result From Login Activity"); Log.d(TAG, data.getStringExtra("nickname")); Log.d(TAG, data.getStringExtra("password")); // Login Setting cManager.setNickname(data.getStringExtra("nickname")); cManager.setPassword(data.getStringExtra("password")); // Connect connectToServer(); } } } }
記得要在AndroidManifest.xml加入這個才能以Dialog形式顯示Activity
<activity android:name=".LoginActivity" android:label="@string/login_activity" android:theme="@android:style/Theme.Dialog"> </activity>
其他我就不想多說了,有問題再說。
No comments:
Post a Comment