下面列出了android.widget.EditText#setLines ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public EditTextSettingsCell(Context context) {
super(context);
textView = new EditText(context);
textView.setTextColor(Theme.getColor(Theme.key_windowBackgroundWhiteBlackText));
textView.setHintTextColor(Theme.getColor(Theme.key_windowBackgroundWhiteHintText));
textView.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 16);
textView.setLines(1);
textView.setMaxLines(1);
textView.setSingleLine(true);
textView.setEllipsize(TextUtils.TruncateAt.END);
textView.setGravity((LocaleController.isRTL ? Gravity.RIGHT : Gravity.LEFT) | Gravity.CENTER_VERTICAL);
textView.setBackgroundDrawable(null);
textView.setPadding(0, 0, 0, 0);
textView.setInputType(textView.getInputType() |EditorInfo.TYPE_TEXT_FLAG_CAP_SENTENCES);
addView(textView, LayoutHelper.createFrame(LayoutHelper.MATCH_PARENT, LayoutHelper.MATCH_PARENT, (LocaleController.isRTL ? Gravity.RIGHT : Gravity.LEFT) | Gravity.TOP, 17, 0, 17, 0));
}
public EditTextSettingsCell(Context context) {
super(context);
textView = new EditText(context);
textView.setTextColor(Theme.getColor(Theme.key_windowBackgroundWhiteBlackText));
textView.setHintTextColor(Theme.getColor(Theme.key_windowBackgroundWhiteHintText));
textView.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 16);
textView.setLines(1);
textView.setMaxLines(1);
textView.setSingleLine(true);
textView.setEllipsize(TextUtils.TruncateAt.END);
textView.setGravity((LocaleController.isRTL ? Gravity.RIGHT : Gravity.LEFT) | Gravity.CENTER_VERTICAL);
textView.setBackgroundDrawable(null);
textView.setPadding(0, 0, 0, 0);
textView.setInputType(textView.getInputType() |EditorInfo.TYPE_TEXT_FLAG_CAP_SENTENCES);
addView(textView, LayoutHelper.createFrame(LayoutHelper.MATCH_PARENT, LayoutHelper.MATCH_PARENT, (LocaleController.isRTL ? Gravity.RIGHT : Gravity.LEFT) | Gravity.TOP, 17, 0, 17, 0));
}
private void validateMultilineFields(
@NonNull EditText editText,
int lines,
boolean singleLine
) {
editText.setSingleLine(singleLine);
editText.setMinLines(lines);
editText.setMaxLines(lines);
editText.setLines(lines);
}
@Override
public long measure(
YogaNode node,
float width,
YogaMeasureMode widthMode,
float height,
YogaMeasureMode heightMode) {
// measure() should never be called before setThemedContext()
EditText editText = Assertions.assertNotNull(mDummyEditText);
if (mLocalData != null) {
mLocalData.apply(editText);
} else {
editText.setTextSize(
TypedValue.COMPLEX_UNIT_PX,
mFontSize == UNSET ?
(int) Math.ceil(PixelUtil.toPixelFromSP(ViewDefaults.FONT_SIZE_SP)) : mFontSize);
if (mNumberOfLines != UNSET) {
editText.setLines(mNumberOfLines);
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M &&
editText.getBreakStrategy() != mTextBreakStrategy) {
editText.setBreakStrategy(mTextBreakStrategy);
}
}
// make sure the placeholder content is also being measured
editText.setHint(getPlaceholder());
editText.measure(
MeasureUtil.getMeasureSpec(width, widthMode),
MeasureUtil.getMeasureSpec(height, heightMode));
return YogaMeasureOutput.make(editText.getMeasuredWidth(), editText.getMeasuredHeight());
}
private View createMeetingLengthTextBox() {
meetingLengthEditText = new EditText(this);
meetingLengthEditText.setGravity(Gravity.CENTER);
meetingLengthEditText.setKeyListener(new DigitsKeyListener());
meetingLengthEditText.setRawInputType(InputType.TYPE_CLASS_PHONE);
meetingLengthEditText.setLayoutParams(new LayoutParams(dipsToPixels(60), LayoutParams.WRAP_CONTENT));
meetingLengthEditText.setText(Integer.toString(meetingLength));
meetingLengthEditText.setLines(1);
meetingLengthSpinner = null;
return meetingLengthEditText;
}
private View createMeetingLengthTextBox() {
meetingLengthEditText = new EditText(this);
meetingLengthEditText.setGravity(Gravity.CENTER);
meetingLengthEditText.setKeyListener(new DigitsKeyListener());
meetingLengthEditText.setRawInputType(InputType.TYPE_CLASS_PHONE);
meetingLengthEditText.setLayoutParams(new LayoutParams(dipsToPixels(60), LayoutParams.WRAP_CONTENT));
meetingLengthEditText.setText(Integer.toString(meetingLength));
meetingLengthEditText.setLines(1);
meetingLengthSpinner = null;
return meetingLengthEditText;
}
/**
* Ask the user for a file name, create this file and choose it.
* ({@link #onFileChosen(View)}).
*/
private void onNewFile() {
final Context cont = this;
// Ask user for filename.
final EditText input = new EditText(this);
input.setInputType(InputType.TYPE_CLASS_TEXT);
input.setLines(1);
input.setHorizontallyScrolling(true);
new AlertDialog.Builder(this)
.setTitle(R.string.dialog_new_file_title)
.setMessage(R.string.dialog_new_file)
.setIcon(android.R.drawable.ic_menu_add)
.setView(input)
.setPositiveButton(R.string.action_ok,
(dialog, whichButton) -> {
if (input.getText() != null
&& !input.getText().toString().equals("")) {
File file = new File(mDir.getPath(),
input.getText().toString());
if (file.exists()) {
Toast.makeText(cont,
R.string.info_file_already_exists,
Toast.LENGTH_LONG).show();
return;
}
Intent intent = new Intent();
intent.putExtra(EXTRA_CHOSEN_FILE, file.getPath());
setResult(Activity.RESULT_OK, intent);
finish();
} else {
// Empty name is not allowed.
Toast.makeText(cont, R.string.info_empty_file_name,
Toast.LENGTH_LONG).show();
}
})
.setNegativeButton(R.string.action_cancel,
(dialog, whichButton) -> {
// Do nothing.
}).show();
}
/**
* Check if it is a valid key file
* ({@link #isValidKeyFileErrorToast()}),
* ask user for a save name and then call
* {@link Common#checkFileExistenceAndSave(File, String[], boolean,
* Context, IActivityThatReactsToSave)}
* @see Common#checkFileExistenceAndSave(File, String[], boolean, Context,
* IActivityThatReactsToSave)
* @see #isValidKeyFileErrorToast()
*/
private void onSave() {
if (!isValidKeyFileErrorToast()) {
return;
}
final File path = Common.getFileFromStorage(Common.HOME_DIR + "/" +
Common.KEYS_DIR);
final Context cont = this;
final IActivityThatReactsToSave activity =
this;
// Ask user for filename.
final EditText input = new EditText(this);
input.setInputType(InputType.TYPE_CLASS_TEXT);
input.setLines(1);
input.setHorizontallyScrolling(true);
input.setText(mFileName);
input.setSelection(input.getText().length());
new AlertDialog.Builder(this)
.setTitle(R.string.dialog_save_keys_title)
.setMessage(R.string.dialog_save_keys)
.setIcon(android.R.drawable.ic_menu_save)
.setView(input)
.setPositiveButton(R.string.action_ok,
(dialog, whichButton) -> {
if (input.getText() != null
&& !input.getText().toString().equals("")) {
File file = new File(path.getPath(),
input.getText().toString());
Common.checkFileExistenceAndSave(file, mLines,
false, cont, activity);
} else {
// Empty name is not allowed.
Toast.makeText(cont, R.string.info_empty_file_name,
Toast.LENGTH_LONG).show();
}
})
.setNegativeButton(R.string.action_cancel,
(dialog, whichButton) -> mCloseAfterSuccessfulSave = false).show();
}
static NativeElementView createTextArea(final Context context, final Element element) {
int textSize = element.getScaledPx(Style.FONT_SIZE);
EditText editText = new EditText(context);
editText.setSingleLine(false);
editText.setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize);
editText.setGravity(Gravity.TOP);
// TODO: Calculate lines based on height if fixed.
editText.setLines(element.getAttributeInt("rows", 2));
editText.setVerticalScrollBarEnabled(true);
LayoutParams params = new LayoutParams(0, LayoutParams.WRAP_CONTENT);
editText.setLayoutParams(params);
NativeElementView result = new NativeElementView(context, element, false, editText);
result.reset();
return result;
}
@Override
public void onClick(View view) {
final DatabaseHandler dbHandler = TwistyTimer.getDBHandler();
switch (view.getId()) {
case R.id.editButton:
MaterialDialog dialog = ThemeUtils.roundDialog(mContext, new MaterialDialog.Builder(mContext)
.title(R.string.edit_algorithm)
.input("", algorithm.getAlgs(), (dialog1, input) -> {
algorithm.setAlgs(input.toString());
dbHandler.updateAlgorithmAlg(mId, input.toString());
algText.setText(input.toString());
updateList();
})
.inputType(InputType.TYPE_TEXT_FLAG_MULTI_LINE)
.positiveText(R.string.action_done)
.negativeText(R.string.action_cancel)
.build());
EditText editText = dialog.getInputEditText();
if (editText != null) {
editText.setSingleLine(false);
editText.setLines(5);
editText.setImeOptions(EditorInfo.IME_FLAG_NO_ENTER_ACTION);
editText.setImeOptions(EditorInfo.IME_FLAG_NO_EXTRACT_UI);
}
dialog.show();
break;
case R.id.progressButton:
final AppCompatSeekBar seekBar = (AppCompatSeekBar) LayoutInflater.from(mContext).inflate(R.layout.dialog_progress, null);
seekBar.setProgress(algorithm.getProgress());
ThemeUtils.roundAndShowDialog(mContext, new MaterialDialog.Builder(mContext)
.title(R.string.dialog_set_progress)
.customView(seekBar, false)
.positiveText(R.string.action_update)
.negativeText(R.string.action_cancel)
.onPositive((dialog12, which) -> {
int seekProgress = seekBar.getProgress();
algorithm.setProgress(seekProgress);
dbHandler.updateAlgorithmProgress(mId, seekProgress);
progressBar.setProgress(seekProgress);
updateList();
})
.build());
break;
case R.id.revertButton:
ThemeUtils.roundAndShowDialog(mContext, new MaterialDialog.Builder(mContext)
.title(R.string.dialog_revert_title_confirmation)
.content(R.string.dialog_revert_content_confirmation)
.positiveText(R.string.action_reset)
.negativeText(R.string.action_cancel)
.onPositive((dialog13, which) -> {
algorithm.setAlgs(AlgUtils.getDefaultAlgs(algorithm.getSubset(), algorithm.getName()));
dbHandler.updateAlgorithmAlg(mId, algorithm.getAlgs());
algText.setText(algorithm.getAlgs());
})
.build());
break;
}
}
public WithDelEditText(Context context, AttributeSet attrs) {
super(context, attrs);
// 方式1获取属性
TypedArray a = context.obtainStyledAttributes(attrs,
R.styleable.WithDelEditText);
hintString = a
.getString(R.styleable.WithDelEditText_WithDelEditText_hint);
hintColor = a.getColor(R.styleable.WithDelEditText_WithDelEditText_hint_color, getContext().getResources().getColor(R.color.gray_half_5));
WithDelEditTextIcon = a
.getDrawable(R.styleable.WithDelEditText_WithDelEditText_icon);
WithDelEditTextDeleteIcon = a
.getDrawable(R.styleable.WithDelEditText_WithDelEditText_delete_icon);
WithDelEditTextDeleteIcon = a
.getDrawable(R.styleable.WithDelEditText_WithDelEditText_delete_icon);
WithDelEditTextBackground = a
.getDrawable(R.styleable.WithDelEditText_WithDelEditText_background);
lines = a.getInteger(R.styleable.WithDelEditText_WithDelEditText_lines, 1);
maxLines = a.getInteger(R.styleable.WithDelEditText_WithDelEditText_maxLines, 1);
a.recycle();
View view = LayoutInflater.from(context).inflate(
R.layout.with_del_edittext, null);
ivWithDelEditTextDeleteIcon = (ImageView) view
.findViewById(R.id.iv_with_del_eidttext_delete);
if (WithDelEditTextDeleteIcon != null)
ivWithDelEditTextDeleteIcon
.setImageDrawable(WithDelEditTextDeleteIcon);
rl = (RelativeLayout) view
.findViewById(R.id.rl);
if (WithDelEditTextBackground != null)
rl.setBackgroundDrawable(WithDelEditTextBackground);
et = (EditText) view.findViewById(R.id.et_with_del_edittext);
if (!TextUtils.isEmpty(hintString))
et.setHint(hintString);
et.setHintTextColor(hintColor);
ivWithDelEditTextIcon = (ImageView) view.findViewById(R.id.iv_with_del_eidttext_icon);
if (WithDelEditTextIcon != null) {
ivWithDelEditTextIcon
.setImageDrawable(WithDelEditTextIcon);
ivWithDelEditTextIcon.setVisibility(VISIBLE);
}
ivWithDelEditTextDeleteIcon.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
et.setText("");
}
});
et.setLines(lines);
et.setMaxLines(maxLines);
// 给编辑框添加文本改变事件
et.addTextChangedListener(new MyTextWatcher());
et.setOnFocusChangeListener(new OnFocusChangeListener() {
@Override
public void onFocusChange(View arg0, boolean arg1) {
// TODO Auto-generated method stub
// Log.v("Steel", "arg1:"+arg1+";"+et.hasFocus());
if (et.hasFocus()) {
if (focusCheckListenter != null) {
focusCheckListenter.setOnFocusValue(true);
}
} else {
if (focusCheckListenter != null) {
focusCheckListenter.setOnFocusValue(false);
}
}
}
});
// 把获得的view加载到这个控件中
addView(view);
}
/**
* Check if the external storage is writable
* {@link Common#isExternalStorageWritableErrorToast(Context)},
* ask user for a save name and then call
* {@link Common#checkFileExistenceAndSave(File, String[], boolean,
* Context, IActivityThatReactsToSave)}.
* This is a helper function for {@link #saveDump()}
* and {@link #saveKeys()}.
* @param data Data to save.
* @param fileName Name of the file.
* @param isDump True if data contains a dump. False if data contains keys.
* @param titleId Resource ID for the title of the dialog.
* @param messageId Resource ID for the message of the dialog.
* @see Common#isExternalStorageWritableErrorToast(Context)
* @see Common#checkFileExistenceAndSave(File, String[], boolean,
* Context, IActivityThatReactsToSave)
*/
private void saveFile(final String[] data, final String fileName,
final boolean isDump, int titleId, int messageId) {
if (!Common.getPreferences().getBoolean(UseInternalStorage.toString(),
false) && !Common.isExternalStorageWritableErrorToast(this)) {
return;
}
String targetDir = (isDump) ? Common.DUMPS_DIR : Common.KEYS_DIR;
final File path = Common.getFileFromStorage(
Common.HOME_DIR + "/" + targetDir);
final Context context = this;
final IActivityThatReactsToSave activity = this;
// Ask user for filename.
final EditText input = new EditText(this);
input.setInputType(InputType.TYPE_CLASS_TEXT);
input.setLines(1);
input.setHorizontallyScrolling(true);
input.setText(fileName);
input.setSelection(input.getText().length());
new AlertDialog.Builder(this)
.setTitle(titleId)
.setMessage(messageId)
.setIcon(android.R.drawable.ic_menu_save)
.setView(input)
.setPositiveButton(R.string.action_save,
(dialog, whichButton) -> {
if (input.getText() != null
&& !input.getText().toString().equals("")) {
File file = new File(path.getPath(),
input.getText().toString());
Common.checkFileExistenceAndSave(file, data,
isDump, context, activity);
if (isDump) {
mDumpName = file.getName();
} else {
mKeysName = file.getName();
}
} else {
// Empty name is not allowed.
Toast.makeText(context, R.string.info_empty_file_name,
Toast.LENGTH_LONG).show();
}
})
.setNegativeButton(R.string.action_cancel,
(dialog, whichButton) -> mCloseAfterSuccessfulSave = false).show();
onUpdateColors(null);
}
/**
* @param context the context to use.
* @param attrs attributes.
* @param parentView parent
* @param label label
* @param value value
* @param type the text type.
* @param lines the lines num.
* @param constraintDescription constraints
* @param readonly if <code>false</code>, the item is disabled for editing.
*/
public GEditTextView(Context context, AttributeSet attrs, LinearLayout parentView, String label, String value, int type,
int lines, String constraintDescription, boolean readonly) {
super(context, attrs);
LinearLayout textLayout = new LinearLayout(context);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT,
LayoutParams.WRAP_CONTENT);
layoutParams.setMargins(10, 10, 10, 10);
textLayout.setLayoutParams(layoutParams);
textLayout.setOrientation(LinearLayout.VERTICAL);
parentView.addView(textLayout);
TextView textView = new TextView(context);
textView.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
textView.setPadding(2, 2, 2, 2);
textView.setText(label.replace(UNDERSCORE, " ").replace(COLON, " ") + " " + constraintDescription);
textView.setTextColor(Compat.getColor(context, R.color.formcolor));
textLayout.addView(textView);
editView = new EditText(context);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
params.setMargins(15, 25, 15, 15);
editView.setLayoutParams(params);
// editView.setPadding(15, 5, 15, 5);
editView.setText(value);
editView.setEnabled(!readonly);
switch (type) {
case 1:
editView.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL);
break;
case 2:
editView.setInputType(InputType.TYPE_CLASS_PHONE);
break;
case 3:
editView.setInputType(InputType.TYPE_CLASS_DATETIME);
break;
case 4:
editView.setInputType(InputType.TYPE_CLASS_NUMBER);
break;
default:
break;
}
if (lines > 0) {
editView.setLines(lines);
editView.setGravity(Gravity.TOP);
}
textLayout.addView(editView);
}