summaryrefslogtreecommitdiff
path: root/build/android/src/main/java/net.minetest.minetest/MinetestTextEntry.java
blob: cb7ba8d3befa8c771c2438ddc28738600f45b621 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
package net.minetest.minetest;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.text.InputType;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnKeyListener;
import android.widget.EditText;

public class MinetestTextEntry extends Activity {
	private final int MultiLineTextInput = 1;
	private final int SingleLineTextInput = 2;
	private final int SingleLinePasswordInput = 3;
	private AlertDialog mTextInputDialog;
	private EditText mTextInputWidget;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		Bundle b = getIntent().getExtras();
		String acceptButton = b.getString("EnterButton");
		String hint = b.getString("hint");
		String current = b.getString("current");
		int editType = b.getInt("editType");

		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		mTextInputWidget = new EditText(this);
		mTextInputWidget.setHint(hint);
		mTextInputWidget.setText(current);
		mTextInputWidget.setMinWidth(300);
		if (editType == SingleLinePasswordInput) {
			mTextInputWidget.setInputType(InputType.TYPE_CLASS_TEXT |
					InputType.TYPE_TEXT_VARIATION_PASSWORD);
		} else {
			mTextInputWidget.setInputType(InputType.TYPE_CLASS_TEXT);
		}

		builder.setView(mTextInputWidget);

		if (editType == MultiLineTextInput) {
			builder.setPositiveButton(acceptButton, new DialogInterface.OnClickListener() {
				public void onClick(DialogInterface dialog, int whichButton) {
					pushResult(mTextInputWidget.getText().toString());
				}
			});
		}

		builder.setOnCancelListener(new DialogInterface.OnCancelListener() {
			public void onCancel(DialogInterface dialog) {
				cancelDialog();
			}
		});

		mTextInputWidget.setOnKeyListener(new OnKeyListener() {
			@Override
			public boolean onKey(View view, int KeyCode, KeyEvent event) {
				if (KeyCode == KeyEvent.KEYCODE_ENTER) {

					pushResult(mTextInputWidget.getText().toString());
					return true;
				}
				return false;
			}
		});

		mTextInputDialog = builder.create();
		mTextInputDialog.show();
	}

	private void pushResult(String text) {
		Intent resultData = new Intent();
		resultData.putExtra("text", text);
		setResult(Activity.RESULT_OK, resultData);
		mTextInputDialog.dismiss();
		finish();
	}

	private void cancelDialog() {
		setResult(Activity.RESULT_CANCELED);
		mTextInputDialog.dismiss();
		finish();
	}
}